Files
TouchSocket/src/TouchSocket.Hosting/Extensions/ServiceCollectionExtensions.cs
若汝棋茗 0cd88f1a6f 更新Hosting机制。
新增交换大小端序
优化内存池写入和读取端序
2023-12-15 20:29:01 +08:00

326 lines
14 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.

//------------------------------------------------------------------------------
// 此代码版权除特别声明或在XREF结尾的命名空间的代码归作者本人若汝棋茗所有
// 源代码使用协议遵循本仓库的开源协议及附加协议若本仓库没有设置则按MIT开源协议授权
// CSDN博客https://blog.csdn.net/qq_40374647
// 哔哩哔哩视频https://space.bilibili.com/94253567
// Gitee源代码仓库https://gitee.com/RRQM_Home
// Github源代码仓库https://github.com/RRQM
// API首页http://rrqm_home.gitee.io/touchsocket/
// 交流QQ群234762506
// 感谢您的下载和使用
//------------------------------------------------------------------------------
using System;
using System.ComponentModel;
using TouchSocket.Core;
using TouchSocket.Core.AspNetCore;
using TouchSocket.Hosting;
using TouchSocket.Hosting.Sockets.HostService;
using TouchSocket.Sockets;
namespace Microsoft.Extensions.DependencyInjection
{
/// <summary>
/// ServiceCollectionExtensions
/// </summary>
public static class ServiceCollectionExtensions
{
#region SetupConfig
/// <summary>
/// 添加SingletonSetupConfigObject服务。
/// </summary>
/// <typeparam name="TObjectService"></typeparam>
/// <typeparam name="TObjectImpService"></typeparam>
/// <param name="services"></param>
/// <param name="actionConfig"></param>
/// <returns></returns>
public static IServiceCollection AddSingletonSetupConfigObject<TObjectService, TObjectImpService>(this IServiceCollection services, Action<TouchSocketConfig> actionConfig)
where TObjectService : class, ISetupConfigObject
where TObjectImpService : class, TObjectService
{
var aspNetCoreContainer = new AspNetCoreContainer(services);
var config = new TouchSocketConfig();
config.SetRegistrator(aspNetCoreContainer);
actionConfig.Invoke(config);
if (config.GetValue(TouchSocketCoreConfigExtension.ConfigureContainerProperty) is Action<IRegistrator> actionContainer)
{
actionContainer.Invoke(aspNetCoreContainer);
}
services.AddSingleton<TObjectService>(privoder =>
{
var imp = privoder.ResolveWithoutRoot<TObjectImpService>();
config.RemoveValue(TouchSocketCoreConfigExtension.ConfigureContainerProperty);
aspNetCoreContainer.BuildResolver(privoder);
config.SetResolver(aspNetCoreContainer);
imp.Setup(config);
return imp;
});
return services;
}
/// <summary>
/// 添加TransientSetupConfigObject服务。
/// </summary>
/// <typeparam name="TObjectService"></typeparam>
/// <typeparam name="TObjectImpService"></typeparam>
/// <param name="services"></param>
/// <param name="actionConfig"></param>
/// <returns></returns>
public static IServiceCollection AddTransientSetupConfigObject<TObjectService, TObjectImpService>(this IServiceCollection services, Action<TouchSocketConfig> actionConfig)
where TObjectService : class, ISetupConfigObject
where TObjectImpService : class, TObjectService
{
var aspNetCoreContainer = new AspNetCoreContainer(services);
var config = new TouchSocketConfig();
config.SetRegistrator(aspNetCoreContainer);
actionConfig.Invoke(config);
if (config.GetValue(TouchSocketCoreConfigExtension.ConfigureContainerProperty) is Action<IRegistrator> actionContainer)
{
actionContainer.Invoke(aspNetCoreContainer);
}
services.AddTransient<TObjectService>(privoder =>
{
var imp = privoder.ResolveWithoutRoot<TObjectImpService>();
config.RemoveValue(TouchSocketCoreConfigExtension.ConfigureContainerProperty);
aspNetCoreContainer.BuildResolver(privoder);
config.SetResolver(aspNetCoreContainer);
imp.Setup(config);
return imp;
});
return services;
}
/// <summary>
/// 添加ScopedSetupConfigObject服务。
/// </summary>
/// <typeparam name="TObjectService"></typeparam>
/// <typeparam name="TObjectImpService"></typeparam>
/// <param name="services"></param>
/// <param name="actionConfig"></param>
/// <returns></returns>
public static IServiceCollection AddScopedSetupConfigObject<TObjectService, TObjectImpService>(this IServiceCollection services, Action<TouchSocketConfig> actionConfig)
where TObjectService : class, ISetupConfigObject
where TObjectImpService : class, TObjectService
{
var aspNetCoreContainer = new AspNetCoreContainer(services);
var config = new TouchSocketConfig();
config.SetRegistrator(aspNetCoreContainer);
actionConfig.Invoke(config);
if (config.GetValue(TouchSocketCoreConfigExtension.ConfigureContainerProperty) is Action<IRegistrator> actionContainer)
{
actionContainer.Invoke(aspNetCoreContainer);
}
services.AddScoped<TObjectService>(privoder =>
{
var imp = privoder.ResolveWithoutRoot<TObjectImpService>();
config.RemoveValue(TouchSocketCoreConfigExtension.ConfigureContainerProperty);
aspNetCoreContainer.BuildResolver(privoder);
config.SetResolver(aspNetCoreContainer);
imp.Setup(config);
return imp;
});
return services;
}
#endregion
#region HostedService
/// <summary>
/// 添加Service类型的HostedService服务。这类服务必须实现<see cref="ISetupConfigObject"/>与<see cref="IService"/>
/// </summary>
/// <typeparam name="TObjectService"></typeparam>
/// <typeparam name="TObjectImpService"></typeparam>
/// <param name="services"></param>
/// <param name="actionConfig"></param>
/// <returns></returns>
public static IServiceCollection AddServiceHostedService<TObjectService, TObjectImpService>(this IServiceCollection services, Action<TouchSocketConfig> actionConfig)
where TObjectService : class, ISetupConfigObject, IService
where TObjectImpService : class, TObjectService
{
return AddSetupConfigObjectHostedService<ServiceHost<TObjectService>, TObjectService, TObjectImpService>(services, actionConfig);
}
/// <summary>
/// 添加SetupConfigObjectHostedService服务。
/// </summary>
/// <typeparam name="THostService"></typeparam>
/// <typeparam name="TObjectService"></typeparam>
/// <typeparam name="TObjectImpService"></typeparam>
/// <param name="services"></param>
/// <param name="actionConfig"></param>
/// <returns></returns>
public static IServiceCollection AddSetupConfigObjectHostedService<THostService, TObjectService, TObjectImpService>(this IServiceCollection services, Action<TouchSocketConfig> actionConfig)
where THostService : SetupConfigObjectHostedService<TObjectService>, new()
where TObjectService : class, ISetupConfigObject
where TObjectImpService : class, TObjectService
{
var aspNetCoreContainer = new AspNetCoreContainer(services);
var config = new TouchSocketConfig();
config.SetRegistrator(aspNetCoreContainer);
actionConfig.Invoke(config);
if (config.GetValue(TouchSocketCoreConfigExtension.ConfigureContainerProperty) is Action<IRegistrator> actionContainer)
{
actionContainer.Invoke(aspNetCoreContainer);
}
services.AddSingleton<TObjectService, TObjectImpService>();
services.AddHostedService(privoder =>
{
var serviceHost = new THostService();
serviceHost.SetObject(privoder.GetService<TObjectService>());
serviceHost.SetConfig(config);
serviceHost.SetProvider(privoder, aspNetCoreContainer);
return serviceHost;
});
return services;
}
#endregion HostedService
#region TcpService
/// <summary>
/// 添加TcpService服务。
/// </summary>
/// <typeparam name="TService"></typeparam>
/// <typeparam name="TImpService"></typeparam>
/// <param name="services"></param>
/// <param name="actionConfig"></param>
/// <returns></returns>
public static IServiceCollection AddTcpService<TService, TImpService>(this IServiceCollection services, Action<TouchSocketConfig> actionConfig)
where TService : class, ITcpServiceBase
where TImpService : class, TService
{
return AddServiceHostedService<TService, TImpService>(services, actionConfig);
}
/// <summary>
/// 添加TcpService服务。并使用<see cref="ITcpService"/>注册服务。
/// </summary>
/// <param name="services"></param>
/// <param name="actionConfig"></param>
/// <returns></returns>
public static IServiceCollection AddTcpService(this IServiceCollection services, Action<TouchSocketConfig> actionConfig)
{
return services.AddTcpService<ITcpService, TcpService>(actionConfig);
}
#endregion TcpService
#region UdpSession
/// <summary>
/// 添加UdpSession服务。
/// </summary>
/// <typeparam name="TService"></typeparam>
/// <typeparam name="TImpService"></typeparam>
/// <param name="services"></param>
/// <param name="actionConfig"></param>
/// <returns></returns>
public static IServiceCollection AddUdpSession<TService, TImpService>(this IServiceCollection services, Action<TouchSocketConfig> actionConfig)
where TService : class, IUdpSession
where TImpService : class, TService
{
return AddServiceHostedService<TService, TImpService>(services, actionConfig);
}
/// <summary>
/// 添加Udp服务。并使用<see cref="IUdpSession"/>注册服务。
/// </summary>
/// <param name="services"></param>
/// <param name="actionConfig"></param>
/// <returns></returns>
public static IServiceCollection AddUdpSession(this IServiceCollection services, Action<TouchSocketConfig> actionConfig)
{
return services.AddUdpSession<IUdpSession, UdpSession>(actionConfig);
}
#endregion UdpSession
#region TcpClient
/// <summary>
/// 添加单例TcpClient服务。
/// </summary>
/// <typeparam name="TClient"></typeparam>
/// <typeparam name="TImpClient"></typeparam>
/// <param name="services"></param>
/// <param name="actionConfig"></param>
/// <returns></returns>
public static IServiceCollection AddSingletonTcpClient<TClient, TImpClient>(this IServiceCollection services, Action<TouchSocketConfig> actionConfig)
where TClient : class, ITcpClient
where TImpClient : class, TClient
{
return AddSingletonSetupConfigObject<TClient, TImpClient>(services, actionConfig);
}
/// <summary>
/// 添加单例TcpClient服务。并使用<see cref="ITcpClient"/>注册服务。
/// </summary>
/// <param name="services"></param>
/// <param name="actionConfig"></param>
/// <returns></returns>
public static IServiceCollection AddSingletonTcpClient(this IServiceCollection services, Action<TouchSocketConfig> actionConfig)
{
return services.AddSingletonTcpClient<ITcpClient, TcpClient>(actionConfig);
}
/// <summary>
/// 添加瞬态TcpClient服务。
/// </summary>
/// <typeparam name="TClient"></typeparam>
/// <typeparam name="TImpClient"></typeparam>
/// <param name="services"></param>
/// <param name="actionConfig"></param>
/// <returns></returns>
public static IServiceCollection AddTransientTcpClient<TClient, TImpClient>(this IServiceCollection services, Action<TouchSocketConfig> actionConfig)
where TClient : class, ITcpClient
where TImpClient : class, TClient
{
return AddTransientSetupConfigObject<TClient, TImpClient>(services, actionConfig);
}
/// <summary>
/// 添加瞬态TcpClient服务。并使用<see cref="ITcpClient"/>注册服务。
/// </summary>
/// <param name="services"></param>
/// <param name="actionConfig"></param>
/// <returns></returns>
public static IServiceCollection AddTransientTcpClient(this IServiceCollection services, Action<TouchSocketConfig> actionConfig)
{
return services.AddTransientTcpClient<ITcpClient, TcpClient>(actionConfig);
}
/// <summary>
/// 添加Scoped TcpClient服务。
/// </summary>
/// <typeparam name="TClient"></typeparam>
/// <typeparam name="TImpClient"></typeparam>
/// <param name="services"></param>
/// <param name="actionConfig"></param>
/// <returns></returns>
public static IServiceCollection AddScopedTcpClient<TClient, TImpClient>(this IServiceCollection services, Action<TouchSocketConfig> actionConfig)
where TClient : class, ITcpClient
where TImpClient : class, TClient
{
return AddScopedSetupConfigObject<TClient, TImpClient>(services, actionConfig);
}
/// <summary>
/// 添加Scoped TcpClient服务。并使用<see cref="ITcpClient"/>注册服务。
/// </summary>
/// <param name="services"></param>
/// <param name="actionConfig"></param>
/// <returns></returns>
public static IServiceCollection AddScopedTcpClient(this IServiceCollection services, Action<TouchSocketConfig> actionConfig)
{
return services.AddScopedTcpClient<ITcpClient, TcpClient>(actionConfig);
}
#endregion TcpClient
}
}