c# - 为什么 services.AddControllers().AddNewtonsoftJson() 使用 .Net5 核心抛出异常

标签 c# nuget-package .net-5

当以下代码执行行 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/

相关文章:

c# - AssemblyInitialize 方法在测试前不运行

c# - Ninject multi-injection 并没有我想象的那么贪心!怎么会?

command-line - nuget pack 引发未设置为对象实例的对象引用。异常(exception)

c# - 带有 .Net5.0 的 Microsoft Interop Excel 无法正常工作

asp.net-core - 找不到指定的 SDK 'Microsoft.NET.SDK.WorkloadAutoImportPropsLocator'。 .net 核心 5

c# - access vba 中的 .net5 com 库

c# - 从 SignalR Hub 类中重定向用户

c# - 预置到 C# 数组

continuous-integration - 如何配置 NuGet.exe 命令工具行查找包的位置

nuget - 在 NuGet-Package Install 上修改 global.asax