jbogard / MediatR

Simple, unambitious mediator implementation in .NET
Apache License 2.0
11.14k stars 1.18k forks source link

System.TypeLoadException on v12 #840

Closed ducanhtk5 closed 1 year ago

ducanhtk5 commented 1 year ago

I received an error: System.TypeLoadException: Could not load type 'MediatR.ServiceFactory' from assembly 'MediatR, Version=12.0.0.0, Culture=neutral, PublicKeyToken=bb9a41a5e8aaa7e2'.

ducanhtk5 commented 1 year ago

@jbogard Please check this urgent bug. My project information: .NET 7.0.3 (latest today). Type: Aspnetcore MVC

Thank you!

normandev92 commented 1 year ago

I also got this issue

jackthip commented 1 year ago

@normandev92 @ducanhtk5 Please try.

builder.Services.AddMediatR(cfg =>
{
    cfg.RegisterServicesFromAssemblies(typeof(Startup).Assembly, typeof(YourHandler).Assembly);
});

Ref: https://github.com/jbogard/MediatR/releases

alex289 commented 1 year ago

@normandev92 @ducanhtk5 Please try.

builder.Services.AddMediatR(cfg =>
{
    cfg.RegisterServicesFromAssemblies(typeof(Startup).Assembly, typeof(YourHandler).Assembly);
});

Ref: https://github.com/jbogard/MediatR/releases

Hey, this works for me but what if I dont have a Startup?

deeprobin commented 1 year ago

Can confirm. Since upgrading my application crashes.

petre-c commented 1 year ago

Same issue here

RoelVerhees commented 1 year ago

Same issue, was working before

andyamacdonald commented 1 year ago

@normandev92 @ducanhtk5 Please try.

builder.Services.AddMediatR(cfg =>
{
    cfg.RegisterServicesFromAssemblies(typeof(Startup).Assembly, typeof(YourHandler).Assembly);
});

Ref: https://github.com/jbogard/MediatR/releases

That sort of worked for me however due to change breaking changes in v12 I've ported any handlers which implemented AsyncRequestHandler to implement IRequestHandler. Those handlers are still breaking with the following error: ~~

An example command would be:

REMOVED

With a corresponding handler:

REMOVED

Previously the handler would have been defined as:

REMOVED

I've probably missed something in the upgrade process or I'm doing something unrecommended. I'm looking to see if I can fix it.

I should point out that I'm using the MediatR.Extensions.Microsoft.DependencyInjection package to manage DI.

So it turns out I'm an idiot and had implemented IRequest<T> instead of IRequestHandler<T> in one of my handlers.

Using the suggestion above resolves the initial issue.

ducanhtk5 commented 1 year ago

@normandev92 @ducanhtk5 Please try.

builder.Services.AddMediatR(cfg =>
{
    cfg.RegisterServicesFromAssemblies(typeof(Startup).Assembly, typeof(YourHandler).Assembly);
});

Ref: https://github.com/jbogard/MediatR/releases

Good now. Thank you!

jamiewinder commented 1 year ago

@normandev92 @ducanhtk5 Please try.

builder.Services.AddMediatR(cfg =>
{
    cfg.RegisterServicesFromAssemblies(typeof(Startup).Assembly, typeof(YourHandler).Assembly);
});

Ref: https://github.com/jbogard/MediatR/releases

Hey, this works for me but what if I dont have a Startup?

Here you're just referencing a type that contains MediatR's services (handlers, etc.). It doesn't need to be the Startup class.

FaizulHussain commented 1 year ago

I've had the same problem today after upgrading to version 12.0

this worked for me too:

builder.Services.AddMediatR(cfg => { cfg.RegisterServicesFromAssemblies(typeof(BaseHandler).Assembly); });

jbogard commented 1 year ago

See the release notes:

https://github.com/jbogard/MediatR/releases/tag/v12.0.0

And upgrade guide:

https://github.com/jbogard/MediatR/wiki/Migration-Guide-11.x-to-12.0

Spylak commented 1 year ago

In case someone finds this helpful what I did is use

builder.Services.AddMediatR(cfg => cfg.RegisterServicesFromAssembly(Assembly.Load("NameOfLibraryThatHasHandlers"))); for dotnet 7 and it worked.