aspnetcore: After adding NuGet Package Microsoft.Extensions.Apidescription.Server to ASP.NET Web Project, build failed with error: Unable to find service type 'Microsoft.Extensions.ApiDescriptions.IDocumentProvider' in loaded assemblies

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

To Reproduce

  1. Install .NET 9.0 preview 7 latest-released-build on win11-x64
  2. Create a MVC Individual project and build it, build succeeded now:
   dotnet new mvc -au Individual
   dotnet build
  1. Add a NuGet Package(Microsoft.Extensions.Apidescription.Server) and build it again
 dotnet add package Microsoft.Extensions.Apidescription.Server -v 9.0.0-preview.7.24406.2
 dotnet build

Actual Result

Build failed with below errors: image

Error Message:

Restore succeeded with 1 warning(s) in 0.9s C:\Users\v-qushi\source\repos\MVC\MVC.csproj : warning NU1904: Package ‘System.Drawing.Common’ 4.7.0 has a known critical severity vulnerability, https://github.com/advisories/GHSA-rxg9-xrhp-64gj You are using a preview version of .NET. See: https://aka.ms/dotnet-support-policy MVC failed with 2 error(s) and 1 warning(s) (9.4s) → bin\Debug\net9.0\MVC.dll C:\Users\v-qushi\source\repos\MVC\MVC.csproj : warning NU1904: Package ‘System.Drawing.Common’ 4.7.0 has a known critical severity vulnerability, https://github.com/advisories/GHSA-rxg9-xrhp-64gj C:\Users\v-qushi.nuget\packages\microsoft.extensions.apidescription.server\9.0.0-preview.7.24406.2\build\Microsoft.Extensions.ApiDescription.Server.targets(66,5): error : Unable to find service type ‘Microsoft.Extensions.ApiDescriptions.IDocumentProvider’ in loaded assemblies. C:\Users\v-qushi.nuget\packages\microsoft.extensions.apidescription.server\9.0.0-preview.7.24406.2\build\Microsoft.Extensions.ApiDescription.Server.targets(66,5): error MSB3073: The command "dotnet “C:\Users\v-qushi.nuget\packages\microsoft.extensions.apidescription.server\9.0.0-preview.7.24406.2\build../tools/dotnet-getdocument.dll” --assembly “C:\Users\v-qushi\source\repos\MVC\bin\Debug\net9.0\MVC.dll” --file-list “obj\MVC.OpenApiFiles.cache” --framework “.NETCoreApp,Version=v9.0” --output “obj” --project “MVC” --assets-file “C:\Users\v-qushi\source\repos\MVC\obj\project.assets.json” --platform “AnyCPU” " exited with code 10.

Build failed with 2 error(s) and 2 warning(s) in 10.9s

Expected Behavior

Build succeed.

Steps To Reproduce

No response

Exceptions (if any)

No response

.NET Version

9.0.100-preview.7.24407.12

Anything else?

No response

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 23 (10 by maintainers)

Most upvoted comments

@martincostello, sorry about that.

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>net9.0</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
    <UserSecretsId>aspnet-MVCTest1-7cbb08a3-0196-42e4-8f3a-5de650cb96e4</UserSecretsId>
  </PropertyGroup>

  <ItemGroup>
    <None Update="app.db" CopyToOutputDirectory="PreserveNewest" ExcludeFromSingleFile="true" />
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="9.0.0-preview.6.24328.4" />
    <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="9.0.0-preview.6.24328.4" />
    <PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="9.0.0-preview.6.24328.4" />
    <PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.0-preview.6.24328.4" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="9.0.0-preview.6.24327.4" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="9.0.0-preview.6.24327.4" />
    <PackageReference Include="Microsoft.Extensions.Apidescription.Server" Version="9.0.0-preview.6.24328.4">
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
  </ItemGroup>

</Project>

@captainsafia

@AbakumovAlexandr If you’re one .NET 8 and below, you’ll need to install either Swashbuckle.AspNetCore or NSwag.

Thank you so much! Your response cleared the whole thing to me.

Turns out that in my case it was because when PublishAoT=true is set, my code runs in a way (via checking RuntimeFeature.IsDynamicCodeSupported == true) that means that the Swashbuckle services aren’t registered with the service collection.

This then means the call here doesn’t include Swashbuckle.AspNetCore.SwaggerGen, so no implementations of Microsoft.Extensions.ApiDescriptions.IDocumentProvider are found:

https://github.com/dotnet/aspnetcore/blob/06155c05af89c957de20d2c53cee0e37171b9a09/src/Tools/GetDocumentInsider/src/Commands/GetDocumentCommandWorker.cs#L194

I can workaround this by only setting PublishAot=true during dotnet publish, rather than always setting it to true in the csproj file.