Search code examples
c#asp.net-coredependency-injectionmediatr

Adding MediatR service


I want to add MediatR to my services.

Here is the code:

public class Program
{
   public static async Task Main(string[] args)
   {
      var builder = WebApplication.CreateBuilder(args);
      builder.Services.AddMediatR(Assembly.GetExecutingAssembly());
   }
   // rest of codes ...
}

Here is the Error:

Error CS1503 Argument 2: cannot convert from 'System.Reflection.Assembly' to 'System.Action<Microsoft.Extensions.DependencyInjection.MediatRServiceConfiguration>'


Solution

  • If you are using [email protected] you can use this:

    builder.Services.AddMediatR(cfg => cfg.RegisterServicesFromAssembly(Assembly.GetExecutingAssembly()));
    

    If you still want to use your version of code, then you should install MediatR.Extensions.Microsoft.DependencyInjection package but you might need to downgrade MediatR version to 11