2

I'm working on a large C# Asp.NetCore 3.1 project where we uses Nswag to generate swagger.json and a C# ProxyClient for our API. Suddenly I cannot generate any of these files for this project anymore, as NSwag fails with a "No service for type" as seen below.

I've tried to Git clone a new version of the solution, to a fresh folder on my machine, but he problem persists. When my colleagues do the same on their machines, they do not have the problem, thus we concluded that it has nothing to do with our code.

I've created a small Asp.NetCore 3.1 API and added the "openapi.nswag" file we use in the original project. I'm not experiencing the issue with this solution, so I havn't been able to reproduce the error.

I've tried everything that comes in to my mind: updated NSwag to newest version, restarted my Computer, removed Nswag and reinstalled it, cleaned up the registry for anything related to Nswag, tried creating the files using NSwagStudio.

I'm really running out of ideas here, so any help would be appreciated.

.\nswag run ..\..\openapi.nswag /runtime:NetCore31

NSwag command line tool for .NET Core NetCore31, toolchain v13.7.0.0 (NJsonSchema v10.1.24.0 (Newtonsoft.Json v12.0.0.0))
Visit http://NSwag.org for more information.
NSwag bin directory: C:\Source\MyProgram\src\B2B\B2B.API\node_modules\nswag\bin\binaries\NetCore31

Executing file '..\..\openapi.nswag' with variables ''...

System.InvalidOperationException: No service for type 'Microsoft.Extensions.DependencyInjection.IServiceProviderFactory<>[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.StartupLoader.ConfigureServicesDelegateBuilder<>.<>c__DisplayClass14_0.<ConfigureServices>g__ConfigureServicesWithContainerConfiguration|0(IServiceCollection services) at Microsoft.AspNetCore.Hosting.ConventionBasedStartup.ConfigureServices(IServiceCollection services) at Microsoft.AspNetCore.Hosting.WebHost.EnsureApplicationServices() at Microsoft.AspNetCore.Hosting.WebHost.Initialize() at Microsoft.AspNetCore.Hosting.WebHostBuilder.Build() at NSwag.Commands.Generation.OpenApiGeneratorCommandBase<>.CreateWebHostAsync(AssemblyLoader assemblyLoader) in C:\projects\nswag\src\NSwag.Commands\Commands\Generation\OpenApiGeneratorCommandBase.cs:line 342 at NSwag.Commands.Generation.AspNetCore.AspNetCoreToSwaggerCommand.RunIsolatedAsync(AssemblyLoader assemblyLoader) in C:\projects\nswag\src\NSwag.Commands\Commands\Generation\AspNetCore\AspNetCoreToOpenApiCommand.cs:line 310 at NSwag.Commands.IsolatedCommandBase<>.IsolatedCommandAssemblyLoader<>.Run(String commandType, String commandData, String[] assemblyPaths, String[] referencePaths) in C:\projects\nswag\src\NSwag.Commands\Commands\IsolatedCommandBase.cs:line 76 at NSwag.Commands.IsolatedCommandBase<>.<>c__DisplayClass17_0.<RunIsolatedAsync>b__0() in C:\projects\nswag\src\NSwag.Commands\Commands\IsolatedCommandBase.cs:line 61 at System.Threading.Tasks.Task<>.InnerInvoke() at System.Threading.Tasks.Task.<>c.<.cctor>b__274_0(Object obj) at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state) --- End of stack trace from previous location where exception was thrown --- at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot, Thread threadPoolThread) --- End of stack trace from previous location where exception was thrown --- at NSwag.Commands.IsolatedCommandBase<>.RunIsolatedAsync(String configurationFile) in C:\projects\nswag\src\NSwag.Commands\Commands\IsolatedCommandBase.cs:line 61 at NSwag.Commands.IsolatedSwaggerOutputCommandBase<>.RunAsync(CommandLineProcessor processor, IConsoleHost host) in C:\projects\nswag\src\NSwag.Commands\Commands\IsolatedSwaggerOutputCommandBase.cs:line 51 at NSwag.Commands.Generation.AspNetCore.AspNetCoreToSwaggerCommand.RunAsync(CommandLineProcessor processor, IConsoleHost host) in C:\projects\nswag\src\NSwag.Commands\Commands\Generation\AspNetCore\AspNetCoreToOpenApiCommand.cs:line 95 at NSwag.Commands.NSwagDocumentBase.GenerateSwaggerDocumentAsync() in C:\projects\nswag\src\NSwag.Commands\NSwagDocumentBase.cs:line 280 at NSwag.Commands.NSwagDocument.ExecuteAsync() in C:\projects\nswag\src\NSwag.Commands\NSwagDocument.cs:line 81 at NSwag.Commands.Document.ExecuteDocumentCommand.ExecuteDocumentAsync(IConsoleHost host, String filePath) in C:\projects\nswag\src\NSwag.Commands\Commands\Document\ExecuteDocumentCommand.cs:line 86 at NSwag.Commands.Document.ExecuteDocumentCommand.RunAsync(CommandLineProcessor processor, IConsoleHost host) in C:\projects\nswag\src\NSwag.Commands\Commands\Document\ExecuteDocumentCommand.cs:line 32 at NConsole.CommandLineProcessor.ProcessSingleAsync(String[] args, Object input) at NConsole.CommandLineProcessor.ProcessAsync(String[] args, Object input) at NConsole.CommandLineProcessor.Process(String[] args, Object input) at NSwag.Commands.NSwagCommandProcessor.Process(String[] args) in C:\projects\nswag\src\NSwag.Commands\NSwagCommandProcessor.cs:line 56child_process.js:669 throw err;

4
  • The error suggests you're not able to run the code at all. Are you dynamic loading DLL's on runtime by using Assembly.Load(..)? Because the error is pretty clear: you did not register a type with Autofac System.InvalidOperationException: No service for type 'Microsoft.Extensions.DependencyInjection.IServiceProviderFactory<>[Autofac.ContainerBuilder]' has been registered.
    – Stefan
    Mar 19, 2021 at 14:43
  • The code builds and executes as expected. It’s the exact same code as on my colleagues computer.
    – jhe
    Mar 19, 2021 at 17:51
  • Hmmm.. the type is Microsoft.Extensions.DependencyInjection.IServiceProviderFactory which is typically part of the framework. Do you have the same components/frameworks installed?
    – Stefan
    Mar 19, 2021 at 17:54
  • Yes... it’s exactly the same
    – jhe
    Mar 19, 2021 at 18:39

1 Answer 1

3

Finally after cloning NSwag and debugging it, I found the problem.

NSwag is looking for the program class in the assembly you want's to create a proxyclient for. It does that by refelction and then NSwag invokes it. Fortunally if program throws an exception it is surpressed by NSwag and it then runs a "fallback routin" where it instead creates the Webhost itself, by using the startup class from the Assembly. When doing this the Webhost wasn't created with AutofacServiceProviderFactory as ServiceProviderFactory, thus throwing the exception above instead of initial exeption.

In my case the problem was that the ASPNETCORE_ENVIRONMENT environment variable was not set, so it wouldn't read then correct settings. My colleauges had this environment variable set globally, but I didn't. It turned out that this setting can be set as a part of the .nswag configuration

enter image description here

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.