调用在以下方法或属性之间是二义性的.ASP.Net web api



我尝试通过youtube教程制作REST API,但我有下一个错误:

错误CS0121调用在以下方法或方法之间有二义性属性:"Application.Dependency.AddApplication (Microsoft.Extensions.DependencyInjection.IServiceCollection)"one_answers"Application.Dependency.AddApplication (Microsoft.Extensions.DependencyInjection.IServiceCollection) '

我知道我有两个相同的方法,它不知道选择哪一个,但我不明白为什么会发生这个错误以及如何修复它。

文件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();

代码存储库

问题在于你的项目在文件系统上的组织方式。

您已经在包含所有其他项目的目录中获得了BublerProject.csproj。这意味着所有的c#文件都在其他项目的BublerProject中编译-这就是导致冲突的原因。

相反,最好使用这样的目录结构:
- BublerSolution
|
+- BublerSolution.sln
|
+- BublerProject
|
+- BublerProject.csproj
+- Program.cs (etc)
|
+- Application
|
+- Application.csproj
+- Dependency.cs
+- ... (other projects)

换句话说,您的顶级目录包含解决方案文件,以及每个项目的子目录。这样你就不会让一个项目嵌套在另一个项目中。

不幸的是,Visual Studio很容易创建一个项目和一个解决方案,这导致了这种情况。如果你总是从一个新的解决方案开始,然后向其中添加项目(而不是从一个新的项目开始,然后保存一个解决方案),那么你通常可以避免这种情况。

最新更新