Why does services.AddControllers().AddNewtonsoftJson() throws exception using .Net5 core

Viewed 2835

I have a problem when the following code executes line services.AddControllers().AddNewtonsoftJson() It throws this exception, if the line is removed the service executes fine it appears.

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.)

I'm running under .Net5 and have the latest Microsoft.AspNetCore.Mvc.NewtonsoftJson package installed. Any ideas on why services.AddControllers().AddNewtonsoftJson() throws this error?

public static IHostBuilder CreateHostBuilder(string[] args)
{
    return Host.CreateDefaultBuilder(args)
        .UseWindowsService()
        .ConfigureServices((hostContext, services) =>
        {
            services.AddControllers().AddNewtonsoftJson(); 
            services.AddHostedService<Service>();
        }).UseSerilog();
}

Packages installed:

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
1 Answers

This issue has nothing to do with the Newtonsoft extension.

You need to import the ASP.NET Core framework to use controllers:

Microsoft.AspNetCore.Hosting;
Microsoft.Extensions.Hosting;

Then use this extension method:

Host.CreateDefaultBuilder(args)
    .ConfigureWebHostDefaults(webBuilder =>
    {
        // ...
    });

You are missing the ASP.NET Core runtimes, so it has no idea how to handle controllers.

What I would highly suggest you do (if you want controllers) is start over. When choosing your project type, choose "ASP.NET Core Website", then choose "Web API" as the type of website.

That will give you everything you need to run controllers.

Related