Could not load type 'MediatR.ServiceFactory'
Asked Answered
P

5

29

I'm creating an Api and I am also using mediatR, I have created my commands, queries, handlers, but when I try to launch my Api I get this error message in my program class:

Could not load type 'MediatR.ServiceFactory' from assembly 'MediatR, Version=12.0.0.0, Culture=neutral, PublicKeyToken=bb9a41a5e8aaa7e2'.

I'm registering my mediatR in my program class like this:

builder.Services.AddMediatR(Assembly.GetExecutingAssembly());

I have installed the MediatR (latest version: 12.0.0) and the MediatR.Extensions.Microsoft.DependencyInjection (version: 11.0.0 ) because the latest version(11.1.0) at this moment is deprecated. enter image description here

I'm using .Net 6, any possible solution? thanks in advance.

Philbin answered 22/2, 2023 at 1:43 Comment(0)
D
75

The update to MediatR 12 comes with a Migration Guide here.

To solve your issue, replace

builder.Services.AddMediatR(Assembly.GetExecutingAssembly());

with

services.AddMediatR(cfg=>cfg.RegisterServicesFromAssemblies(Assembly.GetExecutingAssembly()));

If you have a handler from another (single) assembly, you can try this:

 services.AddMediatR(cfg=>cfg.RegisterServicesFromAssemblies(typeof(MyCoolThingInAnotherAssemblyRequestHandler).GetTypeInfo().Assembly));

To use any of the above extension methods..you may need to add:

using MediatR;

From Migration guide

Service registration through configuration object exclusively Service registration through IServiceCollection previously had overloads for various options. These are now consolidated into the single MediatrServiceConfiguration object. Overloads that passed through the assemblies to scan need to register using methods on this configuration object instead:

Deciare answered 22/2, 2023 at 2:54 Comment(0)
F
9

If anyone using Autofac comes across this post, convert the deprecated ServiceFactory registration

builder.Register<ServiceFactory>(ctx =>
{
  var c = ctx.Resolve<IComponentContext>();
  return t => c.Resolve(t);
});

to a populated ServiceCollection based on the following example

var services = new ServiceCollection();
        
builder.Populate(services);
Fad answered 8/3, 2023 at 19:10 Comment(1)
Thanks. This is what I was looking for. I'm using the clean architecture template from ardalis and it's using this setup.Columbian
G
3

It is very simple. In order to solve your problem just add the following line in your Program.cs class.

builder.Services.AddMediatR(x => x.RegisterServicesFromAssemblies(typeof(LibraryEntrypoint).Assembly));

here "LibraryEntrypoint" is an empty class created in the project folder, this is created to just refer to the assembly of my project. You can add any of the class if you wish.

internal class LibraryEntrypoint
{

}

Thank you!

Graphitize answered 5/3, 2023 at 3:45 Comment(0)
B
0

In my case, it turned out that different versions of the MediatR nuget package were being used. In one assembly, it was <PackageReference Include="MediatR" Version="10.0.1" />, and in another, <PackageReference Include="MediatR" Version="12.1.1" /> The solution is to make the used nuget packages the same version.

Balkhash answered 23/8, 2023 at 12:15 Comment(0)
T
0

In my case I had several different projects but it was only necessary to make changes in Application and WebApi (in my case). In Application I had the handlers and classes with mediatr and in WebApi the Program.cs, my solution was to create a public and empty LibraryEntrypoint class at the root of the Application project, then I added the following in Program.cs:

builder.Services.AddScoped<ISomeRepository, SomeRepository>();
builder.Services.AddScoped<ISomeRepositoryN, SomeRepositoryN>();
...

builder.Services.AddMediatR(x => x.RegisterServicesFromAssemblies(typeof(LibraryEntrypoint).Assembly));

Notes:

  • Don't forget to add the necessary project references in the WebApi project.
  • MediatR version: 12.2.0
  • .NET version: 7
  • Answer based on Abdurrahman Noori
Thinskinned answered 3/4 at 15:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.