Closed tihomir-kit closed 1 year ago
You shouldn't be registering IMediator
like that. First, IMediator
should absolutely not be singleton in a web application. It should be transient. You're also capturing probably the wrong IServiceCollection
there. I don't know what your other Register
methods do.
AddMediatR
does TryAdd
so your RegisterSingleton<IMediator>
is messing it up.
Thanks for your reply.
I changed the IMediator
registration from singleton to transient but I still get the same error. IServiceCollection
comes from Program.cs from the first few lines:
var builder = WebApplication.CreateBuilder(args);
var services = builder.Services;
I'm not sure what other IServiceCollection
I could be capturing.
I will try to create a simplified solution of what I have and put it on GitHub over the weekend if you or someone else is willing and have the time to perhaps look at it at some point.
When you bring in other DI containers, they'll often have their own implementations of things. In general, don't build the service provider yourself. But you shouldn't be registering IMediator
at all, AddMediatR
already does that. I would look at the docs for AddMediatR
and what it registers, and ONLY register anything else that it doesn't.
Is this sample up to date?
I'm obviously misunderstanding something but that's where I got the IMediator
singleton bit from (probably.. as the initial setup was with MediatR 8 maybe 2 years ago or something like that).
After the upgrade from 11 to 12 and after some googling around I found container.Register<IMediator>(() => new Mediator(services.BuildServiceProvider()));
as a suggested fix for container.Register<IMediator, Mediator>();
no longer working. So that explains it (to me) why I was even doing that registraiton).
AddMediatR is also only being used in AspNetCore sample. Perhaps that's where I should focus on.
No, none of those samples are up to date. It takes me a bit to update those since I really only use either the vanilla container or Lamar. Probably I'll come back through and update them to use the various extensions of Ms.Ext.DI. I'm sure there are folks NOT using MS.Ext.DI but I'm not one of them.
Ok, that helps. At least I know not to use that sample for now. :D Thnx!
Hi, I recently tried upgrading a solution to MediatR 12 which was previously working fine with with MediatR 11. Now I'm getting this error and I can't figure out what's wrong:
System.InvalidOperationException: Unable to resolve service for type 'XXX.Core.Auth.Token.IJwtTokenProvider' while attempting to activate 'XXX.Core.Application.Handlers.Account.Login+Handler'.
IJwtTokenProvider
is just a simple service registered in DI:container.Register<IJwtTokenProvider, JwtTokenProvider>();
The
Login
command is in theXXX.Core.Application
project.Program.cs
is inXXX.WebAPI
project.If I try to inject and access
IJwtTokenProvider
insideAccountController
(which normally dispatches the Login command), I can access theIJwtTokenProvider
without problems. However, it seems there's some issue between MediatR and SimpleInjector when I injectIJwtTokenProvider
inside the Login handler. Also, it's notIJwtTokenProvider
specifically that's the problem, it's other injected services as well.This is my Program.cs (.net6)
This is my MediatR 11 config which works fine:
Mediatr 12 changes:
Any rough ideas on what I might be doing wrong?
Thank you.