Swashbuckle.AspNetCore: CLI is not working with project referencing Autofac
Repro steps:
-
Add swashbuckle nuget packages to the project:
- SwashBuckle.AspNetCore 4.0.1
- SwashBuckle.AspNetCore.Annotations 4.0.1
-
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
- Add DotNetCliToolReference to .csproj file:
<ItemGroup>
<DotNetCliToolReference Include="Swashbuckle.AspNetCore.Cli" Version="4.0.1" />
</ItemGroup>
- Run
dotnet swagger tofile --output swaggerexport.json path\to\project.dll v1
- 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)
Had the same issue and I got kinda confused by the replies here.
What fixed it for me was adding this:
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:
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!