Closed srollinet closed 6 years ago
Is this a bad idea to try to use an existing singleton?
Yes π
Generally, it just makes the world easier to live in, when handlers and injected services are transient.
And then, once in a while, you need to register a singleton, but then please remember that everything that singleton gets injected gets scoped to the lifestyle of the singleton, meaning that the singleton must never have transient stuff injected.
I suggest you register the state that you need to share between handlers in a dedicated component, and then you let a reference to that be injected in your - transient! π - handlers.
I hope it makes sense π
Hello,
I try to make an existing registered singleton implement IHandleMessages<> How to register it correctly?
If I register it via
services.AutoRegisterHandlersFromAssemblyOf<MyService>();
a new instance is created everytime. If I register it manually viaservices.AddSingleton<IHandleMessages<MyMessage>>(x => x.GetService<MyService>());
I have an exception when the message is sentIs this a bad idea to try to use an existing singleton? I can probably add a new class that implements the handler and where I inject my singleton service. But if it is possible I would like to avoid it.