Swashbuckle.aspnetcore: Exception on run: No service for type 'Microsoft.AspNetCore.Mvc.ApiExplorer.IApiDescriptionGroupCollectionProvider' has been registered.

Created on 9 Feb 2017  路  5Comments  路  Source: domaindrivendev/Swashbuckle.AspNetCore

I can't seem to get swashbuckle to work for my .net core project.
"Swashbuckle.AspNetCore": "1.0.0-rc1"

I have had success with NSwag middleware but would like to get this working. Eventually adding in Auth0/OAuth2 support with AD for the swagger docs.
https://github.com/NSwag/NSwag

Some possible info from project.json

"dependencies": {
    "Microsoft.NETCore.App": {
      "version": "1.1.0",
      "type": "platform"
    },

  "frameworks": {
    "netcoreapp1.0": {
      "dependencies": {
        "Microsoft.NETCore.App": {
          "version": "1.1.0",
          "type": "platform"
        },
    "imports": [
        "dotnet5.6",
        "portable-net45+win8"
      ]
    }

Some possible relevant info from startup.cs, in order

services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new Info { Title = "My API", Version = "v1" });
            });

app.UseSwagger();
            app.UseSwaggerUi(c =>
            {
                c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
            });
app.UseMvcWithDefaultRoute();

Throws the following exception when you try dotnet run:

Unhandled Exception: System.InvalidOperationException: No service for type 'Microsoft.AspNetCore.Mvc.ApiExplorer.IApiDescriptionGroupCollectionProvider' has been registered.
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)
   at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenOptions.CreateSwaggerProvider(IServiceProvider serviceProvider)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitTransient(TransientCallSite transientCallSite, ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.<>c__DisplayClass16_0.<RealizeService>b__0(ServiceProvider provider)
   at Microsoft.Extensions.Internal.ActivatorUtilities.ConstructorMatcher.CreateInstance(IServiceProvider provider)
   at Microsoft.Extensions.Internal.ActivatorUtilities.CreateInstance(IServiceProvider provider, Type instanceType, Object[] parameters)
   at Microsoft.AspNetCore.Builder.UseMiddlewareExtensions.<>c__DisplayClass3_0.<UseMiddleware>b__0(RequestDelegate next)
   at Microsoft.AspNetCore.Builder.Internal.ApplicationBuilder.Build()
   at Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication()
   at Microsoft.AspNetCore.Hosting.WebHostBuilder.Build()
   at *.API.Program.Main(String[] args) in /Users/west/projects/*-backend-core/*.API/Program.cs:line 24

Program.cs

public class Program
    {
        private static readonly Dictionary<string, string> defaults = new Dictionary<string, string> {
            { WebHostDefaults.EnvironmentKey, "development" }
        };

        public static void Main(string[] args)
        {
            // Let environment variable be set from cli
            var config = new ConfigurationBuilder()
                .AddInMemoryCollection(defaults)
                .AddEnvironmentVariables("ASPNETCORE_")
                .AddCommandLine(args)
                .Build();

            var host = new WebHostBuilder()
            .UseConfiguration(config)
            .UseKestrel()
            .UseAzureAppServices()
            .UseContentRoot(Directory.GetCurrentDirectory())
            .UseUrls("http://localhost:5001")
            .UseStartup<Startup>()
            .Build();

            host.Run();
        }
    }

Most helpful comment

Any particular reason why you're using _AddMvcCore_ instead of _AddMvc_?

As the name implies, the former adds the bare bones MVC stack where as the latter adds the stack plus some additional services. One of the additional services is _ApiExplorer_, the built-in metadata layer that SB relies on. You can either switch to _AddMvc_ or you can use _AddMvcCore_ and then explicitly add the _ApiExplorer_ service:

services.AddMvcCore()
    .AddApiExplorer();

To be honest, I wasn't even aware of the _AddMvcCore_ option. I'll try get something into the docs as I'm sure others will encounter this. Let me know if this resolves your issue?

All 5 comments

In your _Startup.cs_, does the _ConfigureServices_ method contain the following line:

services.AddMvc();

Swashbuckle depends on one of the services (the one mentioned in the exception message you provided) that's registered with the Mvc stack.

We are using:

services.AddMvcCore()
                .AddJsonFormatters(jsonOptions => {
                    jsonOptions.DateTimeZoneHandling = DateTimeZoneHandling.Utc;
                })
                .AddAuthorization(options => {
stuff               });

Any particular reason why you're using _AddMvcCore_ instead of _AddMvc_?

As the name implies, the former adds the bare bones MVC stack where as the latter adds the stack plus some additional services. One of the additional services is _ApiExplorer_, the built-in metadata layer that SB relies on. You can either switch to _AddMvc_ or you can use _AddMvcCore_ and then explicitly add the _ApiExplorer_ service:

services.AddMvcCore()
    .AddApiExplorer();

To be honest, I wasn't even aware of the _AddMvcCore_ option. I'll try get something into the docs as I'm sure others will encounter this. Let me know if this resolves your issue?

Adding the following line made everything work, thank you!

services.AddMvcCore()
    .AddApiExplorer();
Was this page helpful?
0 / 5 - 0 ratings