No constructor for type SwaggerGenerator can be instantiated using services from the service container and default values

DonkeyBanana picture DonkeyBanana · Dec 25, 2018 · Viewed 7.4k times · Source

I'm trying to add Swagger to my project. The error received is as follows.

No constructor for type 'Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator' can be instantiated using services from the service container and default values.

Since I haven't changed anything in Swagger binaries themselves, just installed the packages Swashbuckle.AspNetCore and Swashbuckle.AspNetCore.Swagger (both in version 4.0.1) I'm assuming that it's about the configuration. Following the suggestion here, I've set up the config shown below.

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

app.UseSwagger();
app.UseSwaggerUI(_ => { _.SwaggerEndpoint("/swagger/v1/swagger.json", "API docs"); });

I'm not sure if I'm missing a package, if one of those I have is the wrong version or if the set config I'm providing isn't sufficient.

Answer

T S Taylor picture T S Taylor · Mar 6, 2019

As the asker noted in a comment, what solved this for me was adding in a reference to Api Explorer in ConfigureServices.

Specifically the line required was:

services.AddMvcCore()
        .AddApiExplorer();