- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当以下代码执行行 services.AddControllers().AddNewtonsoftJson() 时出现问题,它抛出此异常,如果删除该行,则服务执行正常。
Some services are not able to be constructed (Error while validatingthe service descriptor 'ServiceType: Microsoft.AspNetCore.Mvc.Infrastructure.IActionInvokerFactoryLifetime: Singleton ImplementationType: Microsoft.AspNetCore.Mvc.Infrastructure.ActionInvokerFactory':No constructor for type 'Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvokerProvider' can beinstantiated using services from the service container and default values.) (Error while validating theservice 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 beinstantiated using services from the service container and default values.) (Error while validating theservice 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.MvcRouteHandlerLifetime: 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.MvcAttributeRouteHandlerLifetime: 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/
当以下代码执行行 services.AddControllers().AddNewtonsoftJson() 时出现问题,它抛出此异常,如果删除该行,则服务执行正常。 Some services ar
我有一个使用 .netcore 3.1 的 ASP MVC 项目,我在其中覆盖序列化程序选项,如下所示 services .AddControllers() .AddNewtonsoftJson(op
我是一名优秀的程序员,十分优秀!