question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Question] Can IMediatR be registered as Singleton?

See original GitHub issue

I’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:closed
  • Created 5 years ago
  • Comments:20 (8 by maintainers)

github_iconTop GitHub Comments

9reactions
jbogardcommented, Apr 18, 2018

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:

  • Don’t make your service a singleton
  • Resolve dynamically through a scoped container on-demand (inside a method or otherwise)

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 the Mediator implementation to scopes, and I have the control of just registering all the dependencies as Transient.

5reactions
henkmollemacommented, Apr 18, 2018

You could create your own scope inside the singleton instance and retrieve instances from it. For example:

public MySingletonService(IServiceProvider serviceProvider)
{
    _serviceProvider = serviceProvider;
}

public void Foo()
{
    using (var scope = _serviceProvider.CreateScope())
    {
        var scopedProvider = scope.ServiceProvider;
    }
}
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found