Home > front end >  The call is ambiguous between the following methods or properties. ASP.NET WEB API
The call is ambiguous between the following methods or properties. ASP.NET WEB API

Time:01-11

I try to make REST API by tutorial in youtube but i have the next error:

Error CS0121 The call is ambiguous between the following methods or properties: 'Application.Dependency.AddApplication(Microsoft.Extensions.DependencyInjection.IServiceCollection)' and 'Application.Dependency.AddApplication(Microsoft.Extensions.DependencyInjection.IServiceCollection)'

I understand that I have 2 identical methods and it does not understand which one to choose, but I cannot understand why this error occurs and how to fix it.

File Application.Dependency.cs:

using Application.Services.Authentication; 
using Microsoft.Extensions.DependencyInjection;
    
    
namespace Application; 
public static class Dependency {
        public static IServiceCollection AddApplication(this IServiceCollection services)
        {
            services.AddScoped<IAuthenticationService, AuthenticationService>();
    
            return services;
        } 
}

Program.cs:

using Infrastructure;
using Application;


var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
{
    builder.Services.AddApplication().AddInfrastructure();
    builder.Services.AddControllers();
}
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();

app.Run();

Code repository

CodePudding user response:

The problem is with how your projects are organized on the file system.

You've got BublerProject.csproj in a directory which itself contains all your other projects. That means all the C# files are being compiled in BublerProject and in the other projects - which is what's causing the conflict.

Instead, it would be better to have a directory structure like this:

- BublerSolution
  |
   - BublerSolution.sln
  |
   - BublerProject
     |
      - BublerProject.csproj
      - Program.cs (etc)
  |
   - Application
     |
      - Application.csproj
      - Dependency.cs
   - ... (other projects)

In other words, your top level directory just has the solution file, and a subdirectory per project. That way you never get one project nested inside another.

Unfortunately Visual Studio can easily end up creating a project and a solution next to each other, which leads to this sort of situation. If you always start with a new solution and then add projects to it (instead of starting with a new project and saving a solution from that) then you can usually avoid this.

  • Related