How to add mediatr in .NET 6?
Asked Answered
P

6

20

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

Pedagogics answered 16/5, 2022 at 15:35 Comment(2)
Have you tried: builder.Services.AddMediatR(typeof(Startup));?Algia
I tried but got build errorPedagogics
S
84

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

Simplify answered 16/5, 2022 at 17:39 Comment(3)
Ah! That's what I missed. Thanks @YaroslavN.Wilma
@YaroslavN I didn't need MediatR.Extensions.Microsoft.DependencyInjection though. I'm using .NET 7 and Microsoft.Extensions.DependencyInjection.Abstractions is installed.Rozek
@YaroslavN. MediatR.Extensions.Microsoft.DependencyInjection is deprecated and not needed from v12.0.0 onwardsRodrich
C
7

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)));
Carbamate answered 6/4, 2023 at 4:38 Comment(0)
T
3

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
});
Tema answered 29/4, 2023 at 14:1 Comment(0)
K
1

The above answer is correct, but you could also do

builder.Services.AddMediatR(typeof(Program));
Kitchenette answered 5/11, 2022 at 21:48 Comment(0)
G
0

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

    builder.Services.AddMediatR(typeof(Program));
Gausman answered 26/4, 2023 at 12:0 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Stream
F
0

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

Flora answered 7/7 at 14:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.