[Question] Can IMediatR be registered as Singleton?
See original GitHub issueI’m using a library that registers a type I define (that consumes IMediatr) as Singleton. Mediatr is registered in the ServiceCollection
extensions as Scoped, so I’m getting an InvalidOperationException: Cannot consume scoped service 'MediatR.IMediator' from singleton 'Xyz.Abc'
exception.
I can circumvent the ServiceCollection
extensions and do my own registration if need be, just wanted some advice on lifetime.
FYI: This is for an ASP.NET Core web api style app.
Thanks!
Issue Analytics
- State:
- Created 5 years ago
- Comments:20 (8 by maintainers)
Top Results From Across the Web
net core - Mediatr handlers are they singletons?
Net Core project and I was wondering if the handler's in the Mediatr are singleton's or are the new instances for every Send...
Read more >Singleton as Interface for testability via dependancy injection
I'll leave the Singleton discussion aside for now and focus on testability and dependency injection. This is a type of dependency injection ...
Read more >Singletons And Shared Instances - Alain Schlesser
In this article, I try to go over the reasoning of why the Singleton should never be used in your code, and what...
Read more >Singletons (Autoload) - Godot Docs
If you're creating an autoload as part of an editor plugin, consider registering it automatically in the Project Settings when the plugin is...
Read more >Frequently Asked Questions
- How much does a funeral cost? Funerals can cost as little as $1000 for a direct disposition. (Direct disposition includes registering the...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
The general rule of thumb is singletons can only depend on other singletons, all the way down. This is true really for any DI container. If you don’t want this behavior, you have two options:
This is also true for any DI container that supports scoped/nested containers. It’s exactly what ASP.NET Core does per request - so in your case, your choices are a bit limited.
There’s nothing preventing you from registering
IMediator
as a singleton, HOWEVER, this forces every handler, and every handler’s dependencies, to be singleton.You could also provide your own implementation of
IMediator
to do what the ASP.NET Core code does above - create a scope and resolve handlers from that scope. I don’t because I don’t want to couple theMediator
implementation to scopes, and I have the control of just registering all the dependencies asTransient
.You could create your own scope inside the singleton instance and retrieve instances from it. For example: