Files
WebSockets/yawaflua.WebSockets/ServiceBindings.cs
Dmitri Shimanski c851815a2c Refactor WebSocket routing and error handling logic
Replaces `Dictionary` with `ConcurrentDictionary` for thread-safe WebSocket route management and improves error logging with added debug assertions. Also fixes duplicate registrations, enhances dependency injection, updates package references, and adjusts WebSocket attribute structure for better extensibility and usage.
2025-05-25 03:32:33 +03:00

32 lines
1.1 KiB
C#

using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using yawaflua.WebSockets.Core;
using yawaflua.WebSockets.Core.Middleware;
using yawaflua.WebSockets.Models.Interfaces;
namespace yawaflua.WebSockets;
public static class ServiceBindings
{
public static IServiceCollection SettingUpWebSockets(this IServiceCollection isc,
WebSocketConfig? socketOptions = null)
{
isc.AddSingleton<WebSocketRouter>();
if (socketOptions != null) isc.AddSingleton(socketOptions);
if (isc.All(k => k.ServiceType != typeof(WebSocketConfig)))
isc.AddSingleton(new WebSocketConfig());
isc.AddScoped<IWebSocketManager, WebSocketManager>();
isc.AddSingleton<IWebSocketManager, WebSocketManager>();
isc.AddTransient<IWebSocketManager, WebSocketManager>();
isc.AddSingleton<WebSocketMiddleware>();
return isc;
}
public static IApplicationBuilder UseWebSocketWithSwagger(this IApplicationBuilder app)
{
app.UseWebSockets();
app.UseMiddleware<WebSocketMiddleware>();
return app;
}
}