radu-matei / websocket-manager

Real-Time library for ASP .NET Core
https://radu-matei.com/blog/real-time-aspnet-core/
MIT License
452 stars 183 forks source link

WebSocketHandler and WebSocketConnectionManager lifetime #73

Open okubiak opened 6 years ago

okubiak commented 6 years ago

Hi,

Any reason to set the WebSocketHandler service as a Singleton and WebSocketConnectionManager service as transient?

From my point of view WebSocketConnectionManager should be a Singleton and WebSocketHandler a transient or scoped service as the entry point for the websocket is the handler, we don't have to keep it as a singleton but allow other scoped DI to be used inside the handler (like for EF #68 - #72)

To be able to keep mapped route to specific handler, I tried to give to the middleware the handler type (like app.MapWebSocketManager<ConnectionHandler>("/connection");) and calling var webSocketHandler = (WebSocketHandler)serviceProvider.GetService(_handlerType); directly in the middleware Invoke function. This is working fine but I don't know if it's the best way to do runtime dependency injection setup.

Regards

sososu commented 4 years ago

I totally agree with you