gpt4 book ai didi

asp.net-core - 构造函数是不明确的错误.NET Core Web API

转载 作者:行者123 更新时间:2023-12-04 04:11:26 25 4
gpt4 key购买 nike

我是 Web API 的新手。在我的 .NET Core 3.1 web api 中,我定义了 3 个相信重载的构造函数。

[Route("api/[controller]")]
[ApiController]
public class KipController : ControllerBase
{
private readonly IConfiguration _configuration;
private readonly IMapper _mapper;
private readonly dbLNePMODev1Context _context;
public KipController(IConfiguration configuration)
{
_configuration = configuration;
}
public KipController(IMapper mapper)
{
_mapper = mapper;
}

public KipController(dbLNePMODev1Context context)
{
_context = context;
}
......

但是在执行应用程序时,它会说以下错误

System.AggregateException HResult=0x80131500 Message=Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: epmoAPI.Models.dbLNePMODev1Context Lifetime: Scoped ImplementationType: epmoAPI.Models.dbLNePMODev1Context': Unable to activate type 'epmoAPI.Models.dbLNePMODev1Context'. The following constructors are ambiguous: Void .ctor(Microsoft.EntityFrameworkCore.DbContextOptions) Void .ctor(Microsoft.EntityFrameworkCore.DbContextOptions1[epmoAPI.Models.dbLNePMODev1Context]))
Source=Microsoft.Extensions.DependencyInjection StackTrace: at
Microsoft.Extensions.DependencyInjection.ServiceProvider..ctor(IEnumerable
1 serviceDescriptors, ServiceProviderOptions options) at Microsoft.Extensions.DependencyInjection.ServiceCollectionContainerBuilderExtensions.BuildServiceProvider(IServiceCollection services, ServiceProviderOptions options) at Microsoft.Extensions.DependencyInjection.DefaultServiceProviderFactory.CreateServiceProvider(IServiceCollection containerBuilder) at Microsoft.Extensions.Hosting.Internal.ServiceFactoryAdapter`1.CreateServiceProvider(Object containerBuilder) at Microsoft.Extensions.Hosting.HostBuilder.CreateServiceProvider() at Microsoft.Extensions.Hosting.HostBuilder.Build() at epmoAPI.Program.Main(String[] args) in C:\Projects\Enterprise PMO\ePMOHub\epmoAPI\Program.cs:line 16

This exception was originally thrown at this call stack: [External Code]

Inner Exception 1: InvalidOperationException: Error while validating the service descriptor 'ServiceType: epmoAPI.Models.dbLNePMODev1Context Lifetime: Scoped ImplementationType: epmoAPI.Models.dbLNePMODev1Context': Unable to activate type 'epmoAPI.Models.dbLNePMODev1Context'. The following constructors are ambiguous: Void .ctor(Microsoft.EntityFrameworkCore.DbContextOptions) Void .ctor(Microsoft.EntityFrameworkCore.DbContextOptions`1[epmoAPI.Models.dbLNePMODev1Context])

Inner Exception 2: InvalidOperationException: Unable to activate type 'epmoAPI.Models.dbLNePMODev1Context'. The following constructors are ambiguous: Void .ctor(Microsoft.EntityFrameworkCore.DbContextOptions) Void .ctor(Microsoft.EntityFrameworkCore.DbContextOptions`1[epmoAPI.Models.dbLNePMODev1Context])



请帮我。我不知道我的代码有什么问题

最佳答案

您的 IConfiguration (可能 IMapper )使用依赖注入(inject),而 dbLNePMODev1Context才不是。这就是 ASP.NET Core 无法实例化它的原因。

更清洁的解决方案:

public KipController(IConfiguration configuration = null, IMapper mapper = null, dbLNePMODev1Context context = null)
{
_configuration = configuration;
_mapper = mapper;
_context = context;
}

关于asp.net-core - 构造函数是不明确的错误.NET Core Web API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61656802/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com