Steve
Steve

Reputation: 4453

How to add mediatr in .NET 6?

In .NET Core, we can add it in ConfigureServices method

services.AddMediatR(typeof(Startup));

But in .NET 6, there is only Program.cs. How to add mediatr in .NET 6?

Tried with this code but got build error

enter image description here

Upvotes: 21

Views: 38347

Answers (6)

Reza Rahgozar
Reza Rahgozar

Reputation: 117

for me with dotnet 6 this line working

builder.Services.AddMediatR(c => c.RegisterServicesFromAssemblyContaining<SaveProductCommand>());

SaveProductCommand : is the name of your Command or Query class

Upvotes: 0

Niels Engelhard
Niels Engelhard

Reputation: 121

Following the release of version 12, the previous code snippet (that a lot of tutorials provide) "builder.Services.AddMediatR(typeof(Program));" is no longer effective. However, the same or equivalent outcome can be achieved by implementing the following code:

builder.Services.AddMediatR(c => c.RegisterServicesFromAssemblyContaining<Program>());

It is important to note that version 12 incorporates the MediatR Dependency Injection NuGet package into the main package, which has consequently led to a number of breaking changes.

You can also add MediatR pipeline configuration in the Program.cs like this way:

builder.Services.AddMediatR(cfg =>
{
    cfg.RegisterServicesFromAssemblyContaining<Program>();
    cfg.Lifetime = ServiceLifetime.Scoped;

    ... more config here
});

Upvotes: 4

Cherno
Cherno

Reputation: 1

The most straight forward approach would be to use the following line code in your Program.cs file.

    builder.Services.AddMediatR(typeof(Program));

Upvotes: 0

Jim Ashbridge
Jim Ashbridge

Reputation: 91

If you are using MediatR 12.0.0 onwards, you no longer need the MediatR.Extensions.Microsoft.DependencyInjection package as it is deprecated and included in the MediatR package itself.

Also, if using version 12.0.0, you can no longer just pass a type and must specify an assembly, either by using the code suggested by @spyros__ or if you want to be specific to a particular type, you could use some reflection: -

builder.Services.AddMediatR(cfg => cfg.RegisterServicesFromAssemblyContaining(typeof(Product)));

Upvotes: 9

spyros__
spyros__

Reputation: 1571

Before MediatR 12.0.0

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

Since MediatR 12.0.0

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

Edit (Explanation): The AddMediatR extension method needs an assembly to scan so it can register all the handlers and mediator types. In previous versions of dotnet we used the typeof(Startup) to point to the assembly of our asp project. We can always do the same thing instead of getting the executing assembly by creating an interface in our asp project, which can also be helpful at testing. Just create an empty interface with a meaningful name, for example something like IProjectNameMarker and then you can use typeof(IProjectNameMarker).

Upvotes: 89

TWong
TWong

Reputation: 101

The above answer is correct, but you could also do

builder.Services.AddMediatR(typeof(Program));

Upvotes: 1

Related Questions