当以下代码执行行 services.AddControllers().AddNewtonsoftJson() 时出现问题,它抛出此异常,如果删除该行,则服务执行正常。
Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: Microsoft.AspNetCore.Mvc.Infrastructure.IActionInvokerFactory Lifetime: Singleton ImplementationType: Microsoft.AspNetCore.Mvc.Infrastructure.ActionInvokerFactory': No constructor for type 'Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvokerProvider' can be instantiated using services from the service container and default values.) (Error while validating the service descriptor 'ServiceType: Microsoft.AspNetCore.Mvc.Abstractions.IActionInvokerProvider Lifetime: Transient ImplementationType: Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvokerProvider': No constructor for type 'Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvokerProvider' can be instantiated using services from the service container and default values.) (Error while validating the service descriptor 'ServiceType: Microsoft.AspNetCore.Mvc.Infrastructure.IActionResultExecutor`1 [Microsoft.AspNetCore.Mvc.VirtualFileResult] Lifetime: Singleton ImplementationType: Microsoft.AspNetCore.Mvc.Infrastructure.VirtualFileResultExecutor': Unable to resolve service for type 'Microsoft.AspNetCore.Hosting.IWebHostEnvironment ' while attempting to activate 'Microsoft.AspNetCore.Mvc.Infrastructure.VirtualFileResultExecutor'.) (Error while validating the service descriptor 'ServiceType: Microsoft.AspNetCore.Mvc.Routing.MvcRouteHandler Lifetime: Singleton ImplementationType: Microsoft.AspNetCore.Mvc.Routing.MvcRouteHandler': No constructor for type 'Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvokerProvider' can be instantiated using services from the service container and default values.) (Error while validating the service descriptor 'ServiceType: Microsoft.AspNetCore.Mvc.Routing.MvcAttributeRouteHandler Lifetime: Transient ImplementationType: Microsoft.AspNetCore.Mvc.Routing.MvcAttributeRouteHandler': No constructor for type 'Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvokerProvider' can be instantiated using services from the service container and default values.)
我在 .Net5 下运行并安装了最新的 Microsoft.AspNetCore.Mvc.NewtonsoftJson 包。关于 services.AddControllers().AddNewtonsoftJson() 为何抛出此错误的任何想法?
public static IHostBuilder CreateHostBuilder(string[] args)
{
return Host.CreateDefaultBuilder(args)
.UseWindowsService()
.ConfigureServices((hostContext, services) =>
{
services.AddControllers().AddNewtonsoftJson();
services.AddHostedService<Service>();
}).UseSerilog();
}
安装的包:
Microsoft.AspNetCore.Mvc.NewtonsoftJson
Microsoft.Extensions.ApiDescription.Client
Microsoft.Extensions.Hosting
Microsoft.Extensions.Hosting.WindowsServices
NSwag.ApiDescription.Client
Serilog.AspNetCore
Serilog.Sinks.RollingFile
System.ServiceModel.Http
System.ServiceModel.Primitives
最佳答案
此问题与 Newtonsoft 扩展无关。
您需要导入 ASP.NET Core 框架才能使用 Controller :
Microsoft.AspNetCore.Hosting;
Microsoft.Extensions.Hosting;
然后使用这个扩展方法:
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
// ...
});
您缺少 ASP.NET Core 运行时,因此它不知道如何处理 Controller 。
我强烈建议您(如果您想要 Controller )重新开始。选择项目类型时,选择“ASP.NET Core 网站”,然后选择“Web API”作为网站类型。
这将为您提供运行 Controller 所需的一切。
关于c# - 为什么 services.AddControllers().AddNewtonsoftJson() 使用 .Net5 核心抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66036999/