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.

Issue with NSwag.MSBuild and .NET 5

See original GitHub issue

We are using NSWag.MSBuild (v 13.7.4) to generate a TypeScript client from our ASP.NET Core Api. When I build the .NET Core app with .NET 5 RC 1, I get the following exception when executing NSWag:

System.InvalidOperationException: No service for type 'Microsoft.AspNetCore.Mvc.ApiExplorer.IApiDescriptionGroupCollectionProvider' has been registered.
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)
   at NSwag.Commands.Generation.AspNetCore.AspNetCoreToSwaggerCommand.GenerateDocumentWithApiDescriptionAsync(AssemblyLoader assemblyLoader, IServiceProvider serviceProvider, String currentWorkingDirectory) in C:\projects\nswag\src\NSwag.Commands\Commands\Generation\AspNetCore\AspNetCoreToOpenApiCommand.cs:line 296
   at NSwag.Commands.Generation.AspNetCore.AspNetCoreToSwaggerCommand.GenerateDocumentAsync(AssemblyLoader assemblyLoader, IServiceProvider serviceProvider, String currentWorkingDirectory) in C:\projects\nswag\src\NSwag.Commands\Commands\Generation\AspNetCore\AspNetCoreToOpenApiCommand.cs:line 280
   at NSwag.Commands.Generation.AspNetCore.AspNetCoreToSwaggerCommand.RunIsolatedAsync(AssemblyLoader assemblyLoader) in C:\projects\nswag\src\NSwag.Commands\Commands\Generation\AspNetCore\AspNetCoreToOpenApiCommand.cs:line 312
   at NSwag.Commands.IsolatedCommandBase`1.IsolatedCommandAssemblyLoader`1.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`1.<>c__DisplayClass17_0.<RunIsolatedAsync>b__0() in C:\projects\nswag\src\NSwag.Commands\Commands\IsolatedCommandBase.cs:line 61
   at System.Threading.Tasks.Task`1.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`1.RunIsolatedAsync(String configurationFile) in C:\projects\nswag\src\NSwag.Commands\Commands\IsolatedCommandBase.cs:line 61
   at NSwag.Commands.IsolatedSwaggerOutputCommandBase`1.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 56

I first use the aspNetCoreToOpenApi document generator, and based on that the openApiToTypeScriptClient code generator. This is my nswag.json file:

{
    "runtime": "$(runtime)",
    "defaultVariables": null,
    "documentGenerator": {
      "aspNetCoreToOpenApi": {
        "project": null,
        "msBuildProjectExtensionsPath": null,
        "configuration": null,
        "runtime": null,
        "targetFramework": null,
        "noBuild": false,
        "verbose": true,
        "workingDirectory": null,
        "requireParametersWithoutDefault": false,
        "apiGroupNames": null,
        "defaultPropertyNameHandling": "Default",
        "defaultReferenceTypeNullHandling": "NotNull",
        "defaultDictionaryValueReferenceTypeNullHandling": "NotNull",
        "defaultResponseReferenceTypeNullHandling": "NotNull",
        "defaultEnumHandling": "Integer",
        "flattenInheritanceHierarchy": false,
        "generateKnownTypes": true,
        "generateEnumMappingDescription": false,
        "generateXmlObjects": false,
        "generateAbstractProperties": false,
        "generateAbstractSchemas": true,
        "ignoreObsoleteProperties": false,
        "allowReferencesWithProperties": false,
        "excludedTypeNames": [],
        "serviceHost": null,
        "serviceBasePath": null,
        "serviceSchemes": [],
        "infoTitle": "Title",
        "infoDescription": null,
        "infoVersion": "1.0.0",
        "documentTemplate": null,
        "documentProcessorTypes": [],
        "operationProcessorTypes": [],
        "typeNameGeneratorType": null,
        "schemaNameGeneratorType": null,
        "contractResolverType": null,
        "serializerSettingsType": null,
        "useDocumentProvider": false,
        "documentName": "v1",
        "aspNetCoreEnvironment": null,
        "createWebHostBuilderMethod": null,
        "startupType": null,
        "allowNullableBodyParameters": true,
        "output": "swagger.json",
        "outputType": "Swagger2",
        "assemblyPaths": ["myAssembly.dll"],
        "assemblyConfig": null,
        "referencePaths": [],
        "useNuGetCache": false
      }
    },
    "codeGenerators": {
      "openApiToTypeScriptClient": {
        "className": "{controller}ApiProxy",
        "moduleName": "",
        "namespace": "",
        "typeScriptVersion": 4.0,
        "template": "Angular",
        "promiseType": "Promise",
        "httpClass": "HttpClient",
        "withCredentials": false,
        "useSingletonProvider": true,
        "injectionTokenType": "InjectionToken",
        "rxJsVersion": 6.6,
        "dateTimeType": "Date",
        "nullValue": "Undefined",
        "generateClientClasses": true,
        "generateClientInterfaces": false,
        "generateOptionalParameters": true,
        "exportTypes": true,
        "wrapDtoExceptions": false,
        "exceptionClass": "SwaggerException",
        "clientBaseClass": "ApiProxyBase",
        "wrapResponses": false,
        "wrapResponseMethods": [],
        "generateResponseClasses": true,
        "responseClass": "SwaggerResponse",
        "protectedMethods": [],
        "configurationClass": null,
        "useTransformOptionsMethod": true,
        "useTransformResultMethod": true,
        "generateDtoTypes": true,
        "operationGenerationMode": "MultipleClientsFromOperationId",
        "markOptionalProperties": true,
        "generateCloneMethod": false,
        "typeStyle": "Class",
        "classTypes": [],
        "extendedClasses": [],
        "extensionCode": "nswag.extensions.ts",
        "generateDefaultValues": true,
        "excludedTypeNames": [],
        "excludedParameterNames": [],
        "handleReferences": false,
        "generateConstructorInterface": true,
        "convertConstructorInterfaceData": false,
        "importRequiredTypes": true,
        "useGetBaseUrlMethod": false,
        "baseUrlTokenName": "API_BASE_URL",
        "queryNullValue": "",
        "inlineNamedDictionaries": false,
        "inlineNamedAny": false,
        "templateDirectory": null,
        "typeNameGeneratorType": null,
        "propertyNameGeneratorType": null,
        "enumNameGeneratorType": null,
        "serviceHost": null,
        "serviceSchemes": null,
        "output": "api.ts"
      }
    }
  }

The command to execute the generation is as follow:

dotnet ./packages/NSwag.MSBuild/tools/NetCore31/dotnet-nswag.dll run nswag.json "/variables:runtime=NetCore31"

I saw that there is a related issue #3026, but the workaround with NSwag.AspNetCore doesn’t work for us, as our Api doesn’t know anything about Swagger and the client generation. The client generation is fully part of a build script.

If I generate the swagger.json file with NSwag.AspNetCore and only execute the openApiToTypeScriptClient based on the file, it works, so the issue seems to be in aspNetCoreToOpenApi. We use the same setup in multiple projects targeting .NET Core 3.1 without any issues.

Is there a problem with my setup or is this an issue with NSwag.MSBuild and .NET 5?

Thanks for looking into it 😃

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:11 (6 by maintainers)

github_iconTop GitHub Comments

3reactions
RicoSutercommented, Nov 11, 2020

NSwag v13.9.0 released with .NET 5.0 support - please try again with this version.

0reactions
kevinbrechbuehlcommented, Nov 20, 2020

I tested it and it’s works perfectly with the 5.0.100 sdk. Thanks for the update 👍

Read more comments on GitHub >

github_iconTop Results From Across the Web

Circular problem when using NSwag.MSBuild
I currently have two projects: 1. ASP.NET Core 3.1 Web API + Xamarin. I am using NSwag to automatically generate the C# client...
Read more >
Get started with NSwag and ASP.NET Core
Learn how to use NSwag to generate documentation and help pages for an ASP.NET Core web API.
Read more >
NSwag.MSBuild 13.20.0
Version Downloads Last updated 13.20.0 10,544 4 days ago 13.19.0 717,881 4 months ago 13.18.5 16,098 4 months ago
Read more >
Issue with NSwag.MSBuild and .NET 5
We are using NSWag.MSBuild (v 13.7.4) to generate a TypeScript client from our ASP.NET Core Api. When I build the .NET Core app...
Read more >
Generate C# client for OpenAPI - Revisited - Kaylumah
msbuild \13.0.5\tools\NetCore21 Code has been successfully written to file. Do you see the issue? The CLI variant differs from the NSwag version ...
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