Registering Mediator source generator, Ambigious call between mediator Microsoft.Extensions.DependencyInjection

110 Views Asked by At

I am trying to register the following .NET implementation of the Mediator pattern using the source generators feature introduced in .NET mediatrsourcegenerator.

I installed the required packages dotnet add package Mediator.SourceGenerator --version 2.0. dotnet add package Mediator.Abstractions --version 2.0.**

var builder = WebApplication.CreateBuilder(args);
    
//DI
builder.Services.AddScoped<IRepository, Repository>();
builder.Services.AddScoped<IDocumentService, DocumentService>();
builder.Services.AddScoped<ICacheService, CacheService>();


builder.Services.AddScoped<IValidationService, ValidationService>();

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

builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

builder.Services.AddMediator(options =>
{
    options.ServiceLifetime = ServiceLifetime.Transient;
});

I get the following error when building application even though the source generator successfully generates the file.

Severity Code Description Project File Line Suppression State Error CS0121 The call is ambiguous between the following methods or properties: 'Microsoft.Extensions.DependencyInjection.MediatorDependencyInjectionExtensions.AddMediator(Microsoft.Extensions.DependencyInjection.IServiceCollection, System.Action<Mediator.MediatorOptions>)' and 'Microsoft.Extensions.DependencyInjection.MediatorDependencyInjectionExtensions.AddMediator(Microsoft.Extensions.DependencyInjection.IServiceCollection, System.Action<Mediator.MediatorOptions>)

I have not got package Microsoft.Extensions.DependencyInjection installed at all, I tried to add it then build(did not work) and then remove it and build(did not work) but the problem persist so it has got nothing to do with that.

I tried something like

builder.Services.Microsoft.Extensions.DependencyInjection.MediatorDependencyInjectionExtensions.AddMediator(options =>
{
    options.ServiceLifetime = ServiceLifetime.Transient;
});

but that doesnt work, also restarted VS. how to resolve this ambiguity. The error message is a bit confusing it looks like the reference is completely identical, someone has ever experienced a similar issue.

1

There are 1 best solutions below

1
derpedcatto On BEST ANSWER

Got this from Mediator GitHub readme, this fixed this issue for me:

  1. Usage and abstractions ... You install the source generator package into your edge/outermost project (i.e. ASP.NET Core application, Background worker project), and then use the Mediator.Abstractions package wherever you define message types and handlers.