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