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.

Injecting IRequestClient in singleton services with M.E.DI

See original GitHub issue

Hey, I’ve noticed that the registration of request clients with Microsoft.Extensions.DependencyInjection looks like this:

https://github.com/MassTransit/MassTransit/blob/43561c1bcb09f620ccefb7e6ae4bc5208bbcdd8a/src/Containers/MassTransit.ExtensionsDependencyInjectionIntegration/Registration/DependencyInjectionContainerRegistrar.cs#L125-L143

So an IRequestClient<T> is registered both as a scoped and a singleton service. Note that this does not work with M.E.DI and only the latter registration is used when providing a service. So since the scoped registration comes last, every injection happens based on this registration.

This can become problematic when injecting the request client into singleton services because then this is basically violating the service scoping. If you have enabled validateScope when building the service provider this will cause a runtime exception:

InvalidOperationException: Error while validating the service descriptor ‘ServiceType: ExampleProject.IExampleService Lifetime: Singleton ImplementationType: ExampleProject.ExampleService’: Cannot consume scoped service ‘MassTransit.IRequestClient`1[ExampleProject.ExampleRequestMessage]’ from singleton ‘ExampleProject.IExampleService’.

Unfortunately, there isn’t really a way to remedy this, apart from registering the singleton client using a different type but I’m not sure if that’s a nice solution here. I personally just inject the IClientFactory now and just create the client there.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:14 (13 by maintainers)

github_iconTop GitHub Comments

2reactions
phatboygcommented, Jan 20, 2021

The resulting clients are, in fact, thread safe.

2reactions
phatboygcommented, Feb 10, 2020

So, yeah, the singleton (non-scoped) version is going to have to be built on-demand using the IClientFactory as you are doing. I’m going to add a couple of extension methods to help out, but I don’t see any other way around it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Problem with integration of MassTransit.SimpleInjector in ...
Services; using SimpleInjector; using SimpleInjector. ... Injecting IRequestClient in singleton services with M.E.DI #1694.
Read more >
Untitled
Inject scoped service into singleton ... Injecting IRequestClient in singleton services with M.E.DI … How To Use a Scoped Service in a Singleton...
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