question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

CLI is not working with project referencing Autofac

See original GitHub issue

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)

Issue Analytics

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

github_iconTop GitHub Comments

3reactions
GhimpuLucianEduardcommented, Jan 25, 2021

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>
2reactions
shivanarucommented, Apr 23, 2020

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!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found