2024-09-30 17:10:43 +08:00

70 lines
2.5 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using JiShe.CollectBus.Core.Plugins;
using JiShe.CollectBus.Core.Services;
using TouchSocket.Core;
using TouchSocket.Sockets;
namespace Microsoft.Extensions.DependencyInjection
{
public static class ServiceCollectionExtensions
{
/// <summary>
/// 添加TcpService服务。
/// </summary>
public static IServiceCollection AddTcp(this IServiceCollection services)
{
services.AddTcpService(config =>
{
config.SetListenIPHosts(10500)
.ConfigureContainer(a => //容器的配置顺序应该在最前面
{
a.AddConsoleLogger(); //添加一个控制台日志注入注意在maui中控制台日志不可用
})
.ConfigurePlugins(a =>
{
a.UseCheckClear()
.SetCheckClearType(CheckClearType.All)
.SetTick(TimeSpan.FromSeconds(60))
.SetOnClose((c, t) =>
{
c.TryShutdown();
c.SafeClose("超时无数据");
});
a.Add<TcpClosePlugin>();
a.Add<TcpServiceReceivedPlugin>();
a.Add<BusService>();
});
});
return services;
}
/// <summary>
/// 添加UdpService服务。
/// </summary>
public static IServiceCollection AddUdp(this IServiceCollection services)
{
services.AddUdpSession(config =>
{
config.SetBindIPHost(10500)
.ConfigureContainer(a => //容器的配置顺序应该在最前面
{
a.AddConsoleLogger(); //添加一个控制台日志注入注意在maui中控制台日志不可用
})
.ConfigurePlugins(a =>
{
a.Add<UdpServiceReceivedPlugin>();
a.Add<BusService>();
})
.UseBroadcast()
.SetUdpDataHandlingAdapter(() => new NormalUdpDataHandlingAdapter());
});
return services;
}
}
}