asp.net core 3.1 service lifetime and Observable sobscriptions in Signalrhub RRS feed

  • Question

  • User540216268 posted

    I think I`m bit lost: I have a signalr hub which gets a "scoped" service ("subscription-service") injected via DI. This "subscription-service" service gets a hubxontext and additional "scoped" domain-specific services injected, and tries to subscribe to the different domain-specific observables, that the services provides. In the individual callbacks from the observables, I want to call methods on the hubcontext, to be able to notify the client when CRUD operations has been done.

    My proble is that the callbacks are only fired IF the injected domain-services are of "singletons" lifetime.

    In my Startup.cs I have configured all my services as "scoped":

    services.AddScoped<IMaterialTypeService, MaterialTypeService>();
    services.AddScoped<IMaterialTypeRepository, MaterialTypeDbRepository>();
    services.AddScoped<IEmailService, EmailService>();
    services.AddScoped<INotificationService, NotificationService>();

    The method for wiring up the subscriptions (in the "subscription-service") is as following:

     private void Startup()
                _notificationService.Notifications.Subscribe(notification =>
                _materialTypeService.MaterialTypesChanged.Subscribe(materialTypes =>
                    var matTypes = _mapper.Map<IList<MaterialTypeResponseDto>>(materialTypes);

    What am I missing in here?

    Also, is there a decent boilerplate web app that will show how this should be done?

    Tuesday, August 4, 2020 6:34 PM

All replies

  • User711641945 posted

    Hi testermax,

    Did you mean you could DI service as singleton but could not DI service as scoped?

    Not sure what is your detailed code like so that I could not reproduce your issue.Here are some reference about signalr:


    Best Regards,


    Wednesday, August 5, 2020 9:40 AM
  • User540216268 posted

    Hi Rena,

    I`m able to DI the services where they are needed, that seems to work as expected.

    My issue is that when I try to subscribe to an Observable from a service that is DI into another service (both are scoped as lifetime), then the callback that is supposed to be triggered from the Observable, is never called.

    My design is that I want to have n number of domain services, and when something happens (CRUD) inside any of the domain services, I want them to push an Observable message, som that the subscription-service mentioned above, can subscribe to this Observable, and in the callback, push the message via signalr. 

    Wednesday, August 5, 2020 10:00 AM