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

3

38

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.

Demonstrate answered 25/12, 2018 at 22:30 Comment(2)
Check documentation here github.com/domaindrivendev/Swashbuckle.AspNetCoreOratory
@Oratory Yeah, it was missing the API explorer. that's a new concept to me as I wasn't using it before (at least explicitly). During my struggle today I've found another manager for swaggering .NET Core projects - nSwag. It's doing the same job as the good old Swashbuckle but with the advantage that it's much easier to set up (basic version requires no arguments being passed in). The downside's that it's changing rapidly and a bit hard to find docs on non-obsolete methods as the software matures. But so is the case with both.Demonstrate
O
61

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();
Ophite answered 6/3, 2019 at 9:23 Comment(2)
Leaving a comment for the next person who has the same problem I had. All my 'other' projects I was copy-pasta-ing from used .AddMvc(), which does .AddApiExplorer() under the covers. I was trying to add swagger to a new project that only used .AddMvcCore() and didn't realise it omits the .AddApiExplorer() services.Dill
This fix also works in .NET Core 3. The migration docs say not to call services.AddMVC() (in ConfigureServices), but Swashbuckle seems to need it.Duff
T
5

.Net 7 Updated Solution:

I know that the answer of (T S Taylor) is correct but for guys who looking for an updated answer in .net 7:

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddMvcCore();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}
Tread answered 23/2, 2023 at 22:3 Comment(0)
B
1

You should register services.AddMvc(); in IServiceCollection, and configure app.UseMvc() in IApplicationBuilder in your application's Startup.cs

Blackleg answered 10/2, 2019 at 15:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.