CLI is not working with project referencing Autofac
See original GitHub issueRepro 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)
Issue Analytics
- State:
- Created 4 years ago
- Reactions:6
- Comments:17 (3 by maintainers)
Top Results From Across the Web
Could not load file or assembly Autofac
There is no mismatch between Autofac versions (4.8.1) or missing references in the CLI project. I have looked at Could not load file...
Read more >Getting Started — Autofac 7.0.0 documentation
The first step is to add Autofac references to your project. ... It is not recommended to resolve from the container directly, however....
Read more >Autofac 7.1.0
Autofac is an IoC container for Microsoft .NET. It manages the dependencies between classes so that applications stay easy to change as they...
Read more >Autofac Documentation
The first step is to add Autofac references to your project. For this example, we're only using core Autofac. Other.
Read more >Unable to resolve dependencies of NuGet packages
Solution · Clear the NuGet cache files. You do this in the following way: In the toolbar of Visual Studio, navigate to Tools...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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!