tpeczek/Lib.AspNetCore.ServerSentEvents

OnClientConnected/Disconnected not firing over Dependency Injection

Doidel opened this issue · 1 comments

Hi,

I just started giving this library a try, it seems great so far, thanks for all the effort!

The issue I have seems related to #33. OnClientConnect and OnClientDisconnect fires well when handled in my custom implementation:

works

public class ChatServerSentEvents : ServerSentEventsService, IChatServerSentEvents
    {
        public ChatServerSentEvents(IOptions<ServerSentEventsServiceOptions<ChatServerSentEvents>> options)
            : base(options.ToBaseServerSentEventsServiceOptions())
        {
            ClientConnected += (service, args) => { System.Diagnostics.Debug.WriteLine(nameof(ClientConnected)); };
            ClientDisconnected += (service, args) => { System.Diagnostics.Debug.WriteLine(nameof(ClientDisconnected)); };
        }
    }

But it doesn't work well when I do the same over Dependency Injection:

doesn't work

public class ChatController : ControllerBase
    {
        private readonly IChatServerSentEvents _sseService;

        public ChatController(IChatServerSentEvents sseService)
        {
            _sseService = sseService;

            sseService.ClientConnected += SseService_ClientConnected;
            sseService.ClientDisconnected += SseService_ClientDisconnected;
        }

        private void SseService_ClientDisconnected(object sender, Lib.AspNetCore.ServerSentEvents.ServerSentEventsClientDisconnectedArgs e)
        {
            System.Diagnostics.Debug.WriteLine("Disconnected 2");
        }

        private void SseService_ClientConnected(object sender, Lib.AspNetCore.ServerSentEvents.ServerSentEventsClientConnectedArgs e)
        {
            System.Diagnostics.Debug.WriteLine("Connected 2");
        }
    }

I am using version 5.1 of the library.

Please let me know if there's any additional info I can provide.
It is not an urgent issue - for now, I will just attach them like the working example.

Thanks!

On second thought, my question makes no sense because a Controller is constructed for every request and you don't want to attach handlers on every request. Sending makes sense, but not attaching/detaching handlers.