1

I need a scoped service A that reacts on events from service B, and then does something on service C.

In my architecture I have marked those kinds of services with a marker interface. The service subscribes to event sources from the injected service B in the constructor.

The problem is that I have to force the resolution of the service when the scope opens, because the service A has to subscribe to service B`s event source.

Is there a way to force certain scoped services to be resolved immediately after the scope has been opened?

Or maybe is there another way to program this?

I have tried to force the resolution. For example, in a Blazor Application I have to add a "scope bootstrap service" to force the resolution of certain services that needs to react on events. But I have to add it to every single page. I think there has to be a more elegant solution.

onixion
  • 11
  • 2
  • can you add it to the constructor of the class itself or am I missing something? – Joe_DM Nov 05 '22 at 13:47
  • The first though jumped to my mind is to create a singleton which the services subscribe to its events and have methods to raise those events -or queue them if you are waiting for other services to subscribe-. This is a quick thought, not an endorsement to the architecture. – M. Elghamry Nov 05 '22 at 14:04
  • theres a lot of options. hard to say what is right and wrong. maybe worth googling around for some different patterns. e.g. pub sub with IoC. I found this example though hard to know if it suites your case https://stackoverflow.com/questions/44091083/publish-subscriber-using-simpleinjector-ioc – Joe_DM Nov 05 '22 at 14:32
  • @M.Elghamry Yes I have those kind of services already. Both Service B and C are like that. But they both live in another assembly. Service B is the local storage service and Service C is my language service. When the local storage data is updated, I need to grab the language name and set it on the language service (and visa vesa). The language service should not know about the local storage service and the storage service should not know anything about the language service. So I was thinking about a "Mediator" in between. It is just observing the services and mediating between them. – onixion Nov 05 '22 at 21:37
  • @M.Elghamry All services have to be scoped to the user, so no singletons. Because different users can have different languages. – onixion Nov 05 '22 at 21:38
  • If DryIoc had a way to allow me to define a function to be called after the scope opens and before it is returned, I could simply resolve certain scoped services with my marker interface (e.g. IObserverMediator or something like that). Then they can register to the event source and done. – onixion Nov 05 '22 at 21:42
  • @onixion Are you controlling the call to `OpenScope` or is it controlled by framework, and then does it have the extension point for opening scope? Then, you may create your own opening scope which additionally resolves some marker interface or collection of the marker interfaces. – dadhi Nov 06 '22 at 11:44
  • @dadhi unfortunately I have no control over the call to OpenScope. I am using DryIoc via the DryIoc.Microsoft.DependencyInjection library for a Blazor Server application. In a Blazor Server application the lifetime of the scope matches the lifetime of the circuit handler (the connected client). I could resolve those "special" services at the birth of the circuit handler, but I am trying to find a solution that will work independently of the "hosting" technology. I would also need this for AspNetCore application, Maui, and maybe more. – onixion Nov 06 '22 at 17:00
  • @dadhi Is it possible to register a service (singleton or scoped) and let it be created immediately instead of being created on resolution? Or is this some kind of Dependency Injection anti-pattern? Thanks – onixion Nov 06 '22 at 17:03
  • @onixion It is not possible in the current version of DryIoc. But I will think how to address it. You can track the issue here https://github.com/dadhi/DryIoc/issues/539 – dadhi Nov 07 '22 at 11:41

0 Answers0