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.

Unable to resolve service for type 'System.Object' while attempting to activate 'MediatR.ServiceFactory'

See original GitHub issue

Hi,

I’m upgrading my web app from ASP.NET Core 2.0 to ASP.NET Core 3.1. Also, I upgraded to Mediatr 8.0.1 and Mediatr.Extensions.Microsoft.DependencyInjection 8.0.0.

On startup, this exception is thrown:

Some services are not able to be constructed (Error while validating the service descriptor ‘ServiceType: System.ICloneable Lifetime: Transient ImplementationType: MediatR.ServiceFactory’: Unable to resolve service for type ‘System.Object’ while attempting to activate ‘MediatR.ServiceFactory’.) (Error while validating the service descriptor ‘ServiceType: System.Runtime.Serialization.ISerializable Lifetime: Transient ImplementationType: MediatR.ServiceFactory’: Unable to resolve service for type ‘System.Object’ while attempting to activate ‘MediatR.ServiceFactory’.)

I used to have this in Startup.cs to add Mediatr to dependency injection:

services.AddScoped<ServiceFactory>(p => p.GetService);

I also tried

services.AddMediatR(Assembly.GetExecutingAssembly());

but the exception still occurs.

Any idea why DI is not registering the ServiceFactory type correctly? How should I do this in ASP.NET Core 3.1? Downgrading again to Mediatr 7.0.0 didn’t help.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

5reactions
ngrusoncommented, Apr 21, 2020

Problem solved. I used to scan assemblies using the Scrutor package to register my Mediatr handlers.

Now I’m doing:

services.AddMediatR(Assembly.GetExecutingAssembly());
services.AddMediatR(typeof(MyCommandHandler).Assembly);
0reactions
aliChavoshicommented, Feb 12, 2022

yes, I solved this problem. check all DI in the project, my problem in the UnitOfWork

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unable to resolve service for type 'MediatR.IMediator'
I try to make .NET Core API with CQRS, but i cannot build it because of MediatR error: System.AggregateException: 'Some services are not...
Read more >
[Solved]-Unable to resolve service for type 'MediatR.IMediator'
Startup]' while attempting to activate 'Application.Startup'.' How to solve "Unable to resolve service for type 'Microsoft.AspNetCore.Identity.UserManager"?
Read more >
No service for type 'MediatR.Mediator' has been registered.
Solution. I changed the mediatR registeration in my startup file to services.AddMediatR(typeof(Startup).GetTypeInfo().Assembly); from services.
Read more >
Can't resolve Service which is registered at .Net Core IOC
I have a problem to resolve a Service that is registered at the .Net Core IOC. I would like to use MediatR within...
Read more >
Diagnosing and Fixing MediatR Container Issues
To fix, I can try to: Extend the container registration to allow it to be resolved. Alter the handler type to allow it...
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