-
Notifications
You must be signed in to change notification settings - Fork 89
Migration from DependencyInjection 7.0 to 9.0 - Error constructing handlers #90
Comments
IPublisher and ISender will need to be registered if you want to use those interfaces. The DI extension project for dotnet core has not had this updated yet if you are using that as far as I know. |
I am indeed working in dotnet core. What would be my best move? rollback to 8.0 for now, register the new interfaces or migrate to dotnet 5? If I were to register IPublisher & ISender, would I need an implementation of them? |
Actually it looks like the dependency injection project was updated to include the new interfaces, so you should just have to update the DI package to 9.0 |
With my DI package at 9.0 I still get the same error though Output gives me: Error is thrown in the Handle() of ValidationBehaviour |
Do you have a minimal repro?
… On Oct 15, 2020, at 3:59 PM, BH4NG ***@***.***> wrote:
With my DI package at 9.0 I still get the same error though
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub, or unsubscribe.
|
@jbogard could it be because of these lines services.TryAdd(new ServiceDescriptor(typeof(ISender), sp => sp.GetService<IMediator>(), serviceConfiguration.Lifetime));
services.TryAdd(new ServiceDescriptor(typeof(IPublisher), sp => sp.GetService<IMediator>(), serviceConfiguration.Lifetime)); are the |
Couldn't make a min repro, but did some more investigating:
so I Add
and again I get 'Error constructing handler for request of type MediatR.IRequestHandler`2[Query,Item]. Register your handlers with the container. See the samples in GitHub for examples.' |
Did you try registering them as transient? I don't think the mediator(s) should be singletons. |
I kept typeof(IMediator) singleton and changed ISender & IPublisher to transient and indeed things work again. Big thanks for the help gentlemen. I do need to add, if I update the DependencyInjection package from 7.0 to 9.0, I again get Not a big issue as I can keep using 7.0, but seems like something is not quite right with the registering of handlers moving from 7.0 to 9.0 |
@BH4NG the only time I am able to get an error similar to your original one was when the |
Sure, before and after upgrade I used:
I also added the following, but I don't think it's necessary
On Upgrading I just changed the injected interface from IMediator to ISender & IPublisher respectively |
you shouldn't be making two calls to |
works with DependencyInjection at 7.0 but not with 9.0 |
I wouldn't expect AsSingleton to work at all so that is surprising. I wouldn't recommend it being singleton anyway so if registering that as scoped or transient fixes it for you that is probably best. I'm not able to get a repro of the issue between 7 and 9, both have the same behavior for me (throwing exceptions as expected when singleton, and not when transient) |
Can I register the handlers asScoped() along as registering something asSingleton()? I cannot used two lambdas, no? |
that configuration only dictates how the mediator implementation itself is registered, all pipeline behaviors and handlers are registered as transient. |
The Handlers are not registered by default are they? because when I don't register them myself I get I read somewhere I just needed to register one handler to add them all to the services, but because you can't add one specific implementation of 'the handlers' I did it like And that worked with DependencyInjection at 7.0. Seeing as I can't make a min rep, I could invite you to my devops to have a look if you're interested. |
Using that extension method for Microsoft DI searches the given assemblies and registers all the ones it finds as transient. You aren't registering one handler when you are using the |
Yeah somehow the extension method alone does not find the handlers it would seem to me. |
I had this same problem. Do you have Microsoft.Azure.AppConfiguration.AspNetCore installed and did you also recently upgrade that from 3.02 to 4.0.0? If so that will cause this problem. Downgrading or uninstalling that package solves the issue. |
Hi Paul, Glad I'm not the only one haha ;-) Unfortunately I don't Microsoft.Azure.AppConfiguration.AspNetCore installed. Do you have more insight as to why exaclty that package causes trouble? The only Azure package I have is Microsoft.Azure.ServiceBus. Before I start fiddling with it, I'm curious about your findings! Kind regards |
It took a while to figure out the culprit, but there is no doubt that is what caused my problem. As to why - that I don't know! My first thought is that the DI extension method used in the MediatR DI package is conflicting with a newly defined DI extension method elsewhere. In your case, if you also recently upgraded Microsoft.Azure.ServiceBus you could try rolling that back to the previous version you had and see if that solves the problem. Good luck. |
Hi Paul, |
We're having similar issues. Using the below as part of .Net Core 3.1 causes a DI error.
The above causes the below error: This exception was originally thrown at this call stack: Replacing with the below. resolves the DI issue but then on executing a command we get the below error: 'Handler was not found for request of type MediatR.IRequestHandler`2' The Handlers sit in another project and it worked fine when the application was a .Net Core 2.2 application. I can fix the problem by manually adding each Handler as a As the same code worked fine in .Net Core 2.2 is there some migration steps I missed? It did not work with Mediatr 7.0 nor Mediatr 9.0 |
Is this using the MediatR DI package? |
@jbogard Yes, as far as I can tell it is using the installed MediatR Di package. |
Yeah I'm not sure, I've upgraded a few applications without any issues. I would look at the service collection before/after each call in Startup to make sure it has everything and what is overwriting it. |
@jbogard - found our issue and all is working as intended now. The previous developer of the source had not registered one of the IEntityRepository in the DI so the handler that was dependent on it was correctly failing. |
Hi All,
Been using MediatR since version 7, yesterday I migrated from 8.1 to 9.0.
Basically I had to inject IPublisher in DbContext, EventDispatcher and CommandHandlers that trigger Events and inject ISender in EventHandlers & Controllers?
I get following error:
System.InvalidOperationException: Error constructing handler for request of type MediatR.IRequestHandler
2, Register your handlers with the container. Cannot resolve 'MediatR.IRequestHandler
2[]' from root provider because it requires scoped service 'xxxRepo'.the operation continues, the request gets handled, the correct data is found, but my API-call returns 500StatusCode and subsequent calls to the same endpoint are not executed.
Had no issues with unregistered handlers of any kind prior to migrating to 9.0
In my startup I register Handlers first, then the repos, services and finally validators.
Do I need to register IPublisher & ISender as well?
Thanks in advance
The text was updated successfully, but these errors were encountered: