@ -2,129 +2,121 @@ using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection ;
using Microsoft.Extensions.DependencyInjection ;
using Microsoft.Extensions.Logging ;
using Microsoft.Extensions.Logging ;
using Microsoft.Extensions.Options ;
using Microsoft.Extensions.Options ;
using Microsoft.Extensions.Caching.Memory ;
using CoreAgent.WebSocketTransport.Interfaces ;
using CoreAgent.WebSocketTransport.Interfaces ;
using CoreAgent.WebSocketTransport.Models ;
using CoreAgent.WebSocketTransport.Services ;
using CoreAgent.WebSocketTransport.Services ;
using CoreAgent.WebSocketTransport.Middleware ;
using CoreAgent.WebSocketTransport.Middleware ;
using Microsoft.Extensions.Caching.Memory ;
using CoreAgent.WebSocketTransport.Models ;
namespace CoreAgent.WebSocketTransport.Extensions ;
/// <summary>
namespace CoreAgent.WebSocketTransport.Extensions
/// WebSocket 传输服务注册扩展
/// 遵循依赖注入原则,注册所有组件
/// </summary>
public static class WebSocketTransportExtensions
{
{
/// <summary>
/// <summary>
/// 添加 WebSocket 传输服务
/// WebSocket 传输服务扩展
/// </summary>
/// </summary>
/// <param name="services">服务集合</param>
public static class WebSocketTransportExtensions
/// <param name="configuration">配置</param>
/// <param name="configSection">配置节名称</param>
/// <returns>服务集合</returns>
public static IServiceCollection AddWebSocketTransport (
this IServiceCollection services ,
IConfiguration configuration ,
string configSection = "WebSocket" )
{
{
if ( services = = null )
/// <summary>
throw new ArgumentNullException ( nameof ( services ) ) ;
/// 添加 WebSocket 传输服务
if ( configuration = = null )
/// </summary>
throw new ArgumentNullException ( nameof ( configuration ) ) ;
/// <param name="services">服务集合</param>
/// <param name="configuration">配置</param>
// 注册配置
/// <param name="configSection">配置节名称</param>
services . Configure < WebSocketConfig > ( options = >
/// <returns>服务集合</returns>
public static IServiceCollection AddWebSocketTransport (
this IServiceCollection services ,
IConfiguration configuration ,
string configSection = "WebSocket" )
{
{
configuration . GetSection ( configSection ) . Bind ( options ) ;
if ( services = = null )
} ) ;
throw new ArgumentNullException ( nameof ( services ) ) ;
if ( configuration = = null )
throw new ArgumentNullException ( nameof ( configuration ) ) ;
// 注册核心服务
// 注册配置
RegisterCoreServices ( services ) ;
services . Configure < WebSocketConfig > ( options = >
{
// 注册默认中间件
configuration . GetSection ( configSection ) . Bind ( options ) ;
RegisterDefaultMiddleware ( services ) ;
} ) ;
return services ;
}
// 注册默认中间件(在核心服务之前)
RegisterDefaultMiddleware ( services ) ;
// 注册核心服务
RegisterCoreServices ( services ) ;
/// <summary>
return services ;
/// 添加 WebSocket 中间件
}
/// </summary>
/// <typeparam name="T">中间件类型</typeparam>
/// <param name="services">服务集合</param>
/// <returns>服务集合</returns>
public static IServiceCollection AddWebSocketMiddleware < T > ( this IServiceCollection services )
where T : class , IMessageMiddleware
{
if ( services = = null )
throw new ArgumentNullException ( nameof ( services ) ) ;
services . AddScoped < IMessageMiddleware , T > ( ) ;
/// <summary>
return services ;
/// 添加 WebSocket 中间件
}
/// </summary>
/// <typeparam name="T">中间件类型</typeparam>
/// <param name="services">服务集合</param>
/// <returns>服务集合</returns>
public static IServiceCollection AddWebSocketMiddleware < T > ( this IServiceCollection services )
where T : class , IMessageMiddleware
{
if ( services = = null )
throw new ArgumentNullException ( nameof ( services ) ) ;
/// <summary>
services . AddScoped < IMessageMiddleware , T > ( ) ;
/// 注册核心服务
return services ;
/// </summary>
}
/// <param name="services">服务集合</param>
private static void RegisterCoreServices ( IServiceCollection services )
{
// 注册核心组件
services . AddSingleton < IWebSocketConnection , WebSocketConnection > ( ) ;
services . AddSingleton < IMessageSerializer , JsonMessageSerializer > ( ) ;
// 注册消息通道管理器
/// <summary>
services . AddSingleton < IMessageChannelManager > ( provider = >
/// 注册核心服务
/// </summary>
/// <param name="services">服务集合</param>
private static void RegisterCoreServices ( IServiceCollection services )
{
{
var logger = provider . GetRequiredService < ILogger < MessageChannelManager > > ( ) ;
// 注册核心组件
var config = provider . GetRequiredService < IOptions < WebSocketConfig > > ( ) . Value ;
services . AddSingleton < IWebSocketConnection , WebSocketConnection > ( ) ;
services . AddSingleton < IMessageSerializer , JsonMessageSerializer > ( ) ;
return new MessageChannelManager (
logger ,
config . QueueCapacity ,
config . QueueCapacity ,
1 0 0 ) ;
} ) ;
// 注册 WebSocket 传输
// 注册消息通道管理器
services . AddSingleton < IWebSocketTransport > ( provider = >
services . AddSingleton < IMessageChannelManager > ( provider = >
{
{
var logger = provider . GetRequiredService < ILogger < CoreAgent . WebSocketTransport . Services . WebSocketTransport > > ( ) ;
var logger = provider . GetRequiredService < ILogger < MessageChannelManager > > ( ) ;
var connection = provider . GetRequiredService < IWebSocketConnection > ( ) ;
var config = provider . GetRequiredService < IOptions < WebSocketConfig > > ( ) . Value ;
var serializer = provider . GetRequiredService < IMessageSerializer > ( ) ;
var middlewares = provider . GetServices < IMessageMiddleware > ( ) ;
return new MessageChannelManager (
var config = provider . GetRequiredService < IOptions < WebSocketConfig > > ( ) . Value ;
logger ,
var channelManager = provider . GetRequiredService < IMessageChannelManager > ( ) ;
config . QueueCapacity ,
config . QueueCapacity ,
return new CoreAgent . WebSocketTransport . Services . WebSocketTransport (
1 0 0 ) ;
logger ,
} ) ;
connection ,
serializer ,
middlewares ,
config ,
channelManager ) ;
} ) ;
}
/// <summary>
// 注册 WebSocket 传输
/// 注册默认中间件
services . AddSingleton < IWebSocketTransport > ( provider = >
/// </summary>
{
/// <param name="services">服务集合</param>
var logger = provider . GetRequiredService < ILogger < CoreAgent . WebSocketTransport . Services . WebSocketTransport > > ( ) ;
private static void RegisterDefaultMiddleware ( IServiceCollection services )
var connection = provider . GetRequiredService < IWebSocketConnection > ( ) ;
{
var serializer = provider . GetRequiredService < IMessageSerializer > ( ) ;
// 注册日志中间件
var middlewares = provider . GetServices < IMessageMiddleware > ( ) ;
services . AddWebSocketMiddleware < LoggingMiddleware > ( ) ;
var config = provider . GetRequiredService < IOptions < WebSocketConfig > > ( ) . Value ;
var channelManager = provider . GetRequiredService < IMessageChannelManager > ( ) ;
return new CoreAgent . WebSocketTransport . Services . WebSocketTransport (
logger ,
connection ,
serializer ,
middlewares ,
config ,
channelManager ) ;
} ) ;
}
// 注册缓存中间件(可选)
/// <summary>
services . AddScoped < IMessageMiddleware > ( provider = >
/// 注册默认中间件
/// </summary>
/// <param name="services">服务集合</param>
private static void RegisterDefaultMiddleware ( IServiceCollection services )
{
{
var cache = provider . GetRequiredService < IMemoryCache > ( ) ;
// 注册日志中间件
var logger = provider . GetRequiredService < ILogger < CacheMiddleware > > ( ) ;
services . AddWebSocketMiddleware < LoggingMiddleware > ( ) ;
var config = provider . GetRequiredService < IOptions < WebSocketConfig > > ( ) . Value ;
return new CacheMiddleware ( cache , logger , config ) ;
// 注册缓存中间件(使用正确的注册方式)
} ) ;
services . AddWebSocketMiddleware < CacheMiddleware > ( ) ;
}
}
}
}
}