Nswag: AspNetCoreToSwaggerGenerator and TestServer (CLI): Cannot resolve IApiDescriptionGroupCollectionProvider (.NET Core 2.0)

Created on 22 Jun 2018  路  7Comments  路  Source: RicoSuter/NSwag

When executing the new AspNetCoreToSwaggerGenerator against a .NET Core 2.0 via CLI and TestServer, it cannot resolve IApiDescriptionGroupCollectionProvider:

image

But examining the services object, it clearly has a IApiDescriptionGroupCollectionProvider registration, even with the correct type ($1):

image

Also this works for the .NET Core 2.1 sample project.

Steps to reproduce:

  • Open NSwag.sln
  • Select NSwag.ConsoleCore as startup project and select 2.0:

image

  • In project debug settings, use the command line args: run "MyPath\NSwag\samples\WithoutMiddleware\Sample.AspNetCore20\nswag_assembly.nswag"
  • Run project with all exceptions enabled (break on all exceptions)

The same works for MyPath\NSwag\samples\WithoutMiddleware\Sample.AspNetCore21\nswag_assembly.nswag

/cc @pranavkm @rynowak any idea why this is happening?

NSwag.AspNetCore bug

Most helpful comment

Found that it is the same issue solved here.

When using services.AddMvcCore() instead of services.AddMvC() in Startup.cs:ConfigureServices(), you must do:
C# services.AddMvcCore().AddApiExplorer()

All 7 comments

cc @Tratcher
Maybe it's accessing host services rather than request services which is where the value needs to come from?

No, Host.Services is replaced by app services after Startup. And that service is pretty clearly in the Realized Services list.

Any news on / workarounds for this? I'm currently stuck at this issue while trying to generate documentation from my .NET Core 2.1 API.

@Ghostbird I think this problem here is only an issue with .NET Core <= 2.0, with 2.1+ it should work. What is your exception?

Exception

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
---> System.InvalidOperationException: No service for type 'Microsoft.AspNetCore.Mvc.ApiExplorer.IApiDescriptionGroupCollectionProvider' has been registered.

.NET Core version

dotnet --version
2.1.500

System info

uname -a
Linux gijsbertdebian 4.9.0-8-amd64 #1 SMP Debian 4.9.130-2 (2018-10-27) x86_64 GNU/Linux

MsBuild configuration excerpt

<Project Sdk="Microsoft.NET.Sdk.Web">
  <PropertyGroup>
    <TargetFramework>netcoreapp2.1</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="NSwag.MSBuild" Version="12.0.4"/>
    <PackageReference Include="NSwag.SwaggerGeneration.AspNetCore" Version="12.0.4"/>
  </ItemGroup>
  <Target Name="NSwag" AfterTargets="Build">
    <Copy SourceFiles="@(ReferencePath)" DestinationFolder="$(OutDir)References" />
    <Exec Command="$(NSwagExe_Core21) run nswag.json" />
    <RemoveDir Directories="$(OutDir)References" />
  </Target>
</Project>

nswag.json

{
  "runtime": "NetCore21",
  "defaultVariables": null,
  "swaggerGenerator": {
    "aspNetCoreToSwagger": {
      "project": "Api.csproj",
      "noBuild": true
    }
  },
  "codeGenerators": {}
}

Found that it is the same issue solved here.

When using services.AddMvcCore() instead of services.AddMvC() in Startup.cs:ConfigureServices(), you must do:
C# services.AddMvcCore().AddApiExplorer()

Nice find, @Ghostbird!

For Google's sake, if you get

InvalidOperationException: API Explorer not registered in DI.

on startup, the solution is to add

services.AddMvcCore().AddApiExplorer();

in public void ConfigureServices(IServiceCollection services)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Rui90 picture Rui90  路  3Comments

jjohs picture jjohs  路  3Comments

AnthonySteele picture AnthonySteele  路  3Comments

molszews picture molszews  路  4Comments

Rui90 picture Rui90  路  4Comments