为什么 services.AddControllers().AddNewtonsoftJson() 使用 .Net5 核心抛出异常
Why does services.AddControllers().AddNewtonsoftJson() throws exception using .Net5 core
当以下代码执行 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 核心框架才能使用控制器:
Microsoft.AspNetCore.Hosting;
Microsoft.Extensions.Hosting;
然后使用这个扩展方法:
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
// ...
});
您缺少 ASP.NET 核心 运行 次,因此它不知道如何处理控制器。
我强烈建议您(如果您想要控制器)重新开始。选择项目类型时,选择“ASP.NET核心网站”,然后选择“Web API”作为网站类型。
这将为您提供 运行 控制器所需的一切。
当以下代码执行 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 核心框架才能使用控制器:
Microsoft.AspNetCore.Hosting;
Microsoft.Extensions.Hosting;
然后使用这个扩展方法:
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
// ...
});
您缺少 ASP.NET 核心 运行 次,因此它不知道如何处理控制器。
我强烈建议您(如果您想要控制器)重新开始。选择项目类型时,选择“ASP.NET核心网站”,然后选择“Web API”作为网站类型。
这将为您提供 运行 控制器所需的一切。