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
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
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).
MediatR.Extensions.Microsoft.DependencyInjection
is deprecated and not needed from v12.0.0 onwards –
Rodrich 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)));
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
});
The above answer is correct, but you could also do
builder.Services.AddMediatR(typeof(Program));
The most straight forward approach would be to use the following line code in your Program.cs file.
builder.Services.AddMediatR(typeof(Program));
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
© 2022 - 2024 — McMap. All rights reserved.
builder.Services.AddMediatR(typeof(Startup));
? – Algia