Swashbuckle.AspNetCore: CLI is not working with project referencing Autofac

Repro steps:

  1. Add swashbuckle nuget packages to the project:

    • SwashBuckle.AspNetCore 4.0.1
    • SwashBuckle.AspNetCore.Annotations 4.0.1
  2. Configure swashbuckle:

  • In ConfigureServices:
services.AddSwaggerGen(options =>
{
    var projectName = GetType().Assembly.GetName().Name;
    var path = Path.Combine(AppContext.BaseDirectory, $"{projectName}.xml");

    options.SwaggerDoc("v1", new Info
    {
        Title = $"{projectName} API Reference",
        Version = "v1"
    });

    options.IncludeXmlComments(path, true);
    options.EnableAnnotations();
});
  • In Configure:
app.UseSwagger();
app.UseSwaggerUI(options => options.SwaggerEndpoint("/swagger/v1/swagger.json", "v1"));

Swashbuckle & Swagger UI now works fine

  1. Add DotNetCliToolReference to .csproj file:
  <ItemGroup>
    <DotNetCliToolReference Include="Swashbuckle.AspNetCore.Cli" Version="4.0.1" />
  </ItemGroup>
  1. Run
dotnet swagger tofile --output swaggerexport.json path\to\project.dll v1
  1. Got an error
Unhandled Exception: System.InvalidOperationException: No service for type 'Microsoft.Extensions.DependencyInjection.IServiceProviderFactory`1[Autofac.ContainerBuilder]' has been registered.
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)
   at Microsoft.AspNetCore.Hosting.Internal.StartupLoader.ConfigureServicesDelegateBuilder`1.<>c__DisplayClass14_0.<ConfigureServices>g__ConfigureServicesWithContainerConfiguration|0(IServiceCollection services)
   at Microsoft.AspNetCore.Hosting.ConventionBasedStartup.ConfigureServices(IServiceCollection services)
   at Microsoft.AspNetCore.Hosting.Internal.WebHost.EnsureApplicationServices()
   at Microsoft.AspNetCore.Hosting.Internal.WebHost.Initialize()
   at Microsoft.AspNetCore.Hosting.WebHostBuilder.Build()
   at Swashbuckle.AspNetCore.Cli.Program.<>c.<Main>b__0_3(IDictionary`2 namedArgs)
   at Swashbuckle.AspNetCore.Cli.CommandRunner.Run(IEnumerable`1 args)
   at Swashbuckle.AspNetCore.Cli.CommandRunner.Run(IEnumerable`1 args)
   at Swashbuckle.AspNetCore.Cli.Program.Main(String[] args)

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 6
  • Comments: 17 (3 by maintainers)

Most upvoted comments

Had the same issue and I got kinda confused by the replies here.

What fixed it for me was adding this:

public class SwaggerHostFactory
{
    public static IHost CreateHost()
    {
        return Program.CreateHostBuilder(new string[0]).Build();
    }
}

This is a separate file from my normal Program.cs, just added it, and seems to work fine.

My use case was that I needed the OpenApi generated at build time so I could generate Typescript services from it (just in case someone is having the same issue).

The above code and this did the trick:

<Target Name="PostBuild" AfterTargets="PostBuildEvent">
   <Exec Command="swagger tofile --output swagger.json $(OutputPath)\$(AssemblyName).dll v1" />
</Target>

Hi all,

Doesn’t look like i can add Swashbuckle.AspNetCore.Cli to 3.1 projects, therefore i can’t reference ISwaggerWebHostFactory. Do I have another option?

Thanks!

I moved to using NSwag and it works well with Open API/Swagger v3 spec. We also needed to generate the TypeScript interfaces and NSwag does that as well. Was easy to configure as well. Hope that helps!