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

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

Also this works for the .NET Core 2.1 sample project.
Steps to reproduce:

run "MyPath\NSwag\samples\WithoutMiddleware\Sample.AspNetCore20\nswag_assembly.nswag"The same works for MyPath\NSwag\samples\WithoutMiddleware\Sample.AspNetCore21\nswag_assembly.nswag
/cc @pranavkm @rynowak any idea why this is happening?
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)
Most helpful comment
Found that it is the same issue solved here.
When using
services.AddMvcCore()instead ofservices.AddMvC()inStartup.cs:ConfigureServices(), you must do:C# services.AddMvcCore().AddApiExplorer()