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<WebApiSubscriptionService>();
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 =>
{
_hubContext.Clients.All.Notify(notification);
});
_materialTypeService.MaterialTypesChanged.Subscribe(materialTypes =>
{
var matTypes = _mapper.Map<IList<MaterialTypeResponseDto>>(materialTypes);
_hubContext.Clients.All.MaterialTypesChanged(matTypes);
});
}
What am I missing here?
Why wouldnt the callback inside the subscription being called? All services ar scoped and triggered by t he same web request.
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.