Ray
Ray

Reputation: 12441

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

I have an Asp.net Core 2.2 app that I'm trying to upgrade to 3.0. I'm using Mediatr and now I got this error after upgrading to 3.0

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'.

Here is my code in the Startup.cs

services.AddMediatR(typeof(BlogPost));

and I'm using Scrutor to scan IMediator

services.Scan(scan => scan
          .FromAssembliesOf(typeof(ISettingService), typeof(IMediator), typeof(BlogPost), typeof(IHomeHelper))
          .AddClasses()
          .UsingRegistrationStrategy(RegistrationStrategy.Skip) 
          .AsImplementedInterfaces()
          .WithScopedLifetime());

The above code works in 2.2, but now I cannot get the program to even launch due to the error. Could someone shed some light on what is going wrong please?

Upvotes: 1

Views: 755

Answers (1)

Disable validate scope.

// Program.cs
public static IWebHost BuildWebHost(string[] args) =>
 WebHost.CreateDefaultBuilder(args)
 .UseStartup<Startup>()
 .UseDefaultServiceProvider(options => options.ValidateScopes = false) // <<<<<<<<<<<
 .Build();

Upvotes: 1

Related Questions