dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.29k stars 9.96k forks source link

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 #56481

Closed v-qushi closed 1 month ago

v-qushi commented 3 months ago

Is there an existing issue for this?

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

v-qushi commented 3 months ago

It can also repro with .NET 8.0 project.

Not sure if it's a reasonable scenario. Why I try to add Nuget Package 'Microsoft.Extensions.Apidescription.Server' : when I do config 'Microsoft Power Power Platform', I met this issue: BUG 2110144. After doing the workaround adding Nuget package can resolve this issue. I aslo added the error message comment for doing publish using VS to BUG 2110144. The error message is different. image

@mkArtakMSFT, can you please take a look at if it is a real issue for .NET SDK? CC: @balachir

captainsafia commented 3 months ago

@v-qushi Is the Microsoft.AspNetCore.OpenApi package installed in your project? If not, try installing it and see if this issue continues to repro.

 dotnet add package Microsoft.AspNetCore.OpenApi -v 9.0.0-preview.6.24324.2
v-qushi commented 3 months ago

Checked my project, the package isn't installed. I tried to install it, the issue still can repro. publish PNG

martincostello commented 3 months ago

Add an explicit PackageReference for a newer version of System.Drawing.Common, or if you use central package management add an explicit PackageVersion of the same and set CentralPackageTransitivePinningEnabled to true.

v-qushi commented 3 months ago

I can work around the system drawing common warnings by installing a newer version of it, but that does not take care of the actual publish failure itself

image
captainsafia commented 3 months ago

@v-qushi Can you share the contents of the project file for your MvcTest1.csproj?

v-qushi commented 3 months ago

@captainsafia Create new MVC Individual project, MvcTest1.csproj as below: image

After add package Microsoft.Extensions.Apidescription.Server and Microsoft.AspNetCore.OpenApi: image

martincostello commented 3 months ago

@v-qushi Can you provide the actual XML rather than a screenshot of it please? It's a lot easier for people to use, rather than needing to manually transcribe and type it out.

v-qushi commented 3 months ago

@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>
matus-gazo commented 2 months ago

Hi all, any news on this please? I am having same issue on .NET 8. While I can build and run the sub-project using OpenAPI/Swashbuckle, publish on the solution fails with same error for.

1>C:\Users\matgazo\.nuget\packages\microsoft.extensions.apidescription.server\8.0.7\build\Microsoft.Extensions.ApiDescription.Server.targets(66,5): error : Unable to find service type 'Microsoft.Extensions.ApiDescriptions.IDocumentProvider' in dependency injection container. Update the 'Startup' class to register a document.

EDIT: I worked around by setting <OpenApiGenerateDocuments>false</OpenApiGenerateDocuments> in my .csproj as Swagger UI still works just fine with this.

captainsafia commented 2 months ago

@matus-gazo If you're running into this issue in .NET 8, I'd recommend upgrading to the latest version of the Swashbuckle package that targets the .NET 8 TFM and seeing if you still repro.

@v-qushi Can you email me a ZIP folder of the project that you are reproing this issue on?

v-qushi commented 2 months ago

@captainsafia , I already sent you the email.

v-qushi commented 2 months ago

@captainsafia , 'dotnet build' failed with the same 2 errors after adding the package.

martincostello commented 2 months ago

If you're running into this issue in .NET 8, I'd recommend upgrading to the latest version of the Swashbuckle package that targets the .NET 8 TFM and seeing if you still repro.

I can repro this with Swashbuckle.AspNetCore 6.6.2 and ASP.NET Core 8.0.7.

martincostello commented 2 months ago

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.

v-qushi commented 1 month ago

Since dotnet build failed with same errors, update the title and steps to use build instead of publish

captainsafia commented 1 month ago

@v-qushi I haven't received the email you mentioned. Can you verify the repro steps again?

v-qushi commented 1 month ago

@captainsafia , Project is here: MVC.zip. My previous emails should be blocked as I put an attached file. I verified the repro steps again and got same errors, so I updated the steps using .NET 9.0 Preview 7(latest-released-build)

captainsafia commented 1 month ago

@v-qushi The project that you shared doesn't have the Microsoft.AspNetCore.OpenApi dependency in the project file. Here's what I see.

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

  <PropertyGroup>
    <TargetFramework>net9.0</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
    <UserSecretsId>aspnet-MVC-23909ff8-acca-4820-89b7-b7b40b7db1b0</UserSecretsId>
  </PropertyGroup>

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

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

</Project>

Adding it via:

$ dotnet add package Microsoft.AspNetCore.OpenApi

Then updating the Program.cs to include:

builder.Services.AddOpenApi();

resolves the issue on my local box.

Did you discover this issue as part of some exploratory testing or was there a test scenario you are going through?

v-qushi commented 1 month ago

@captainsafia , I tried your solution: add package Microsoft.AspNetCore.OpenApi and update Program.cs, the issue was resolved. So I closed this bug.

When did our test case, I met an issue Bug 2110144: [WebToolsE2E][Power Platform] Fails to configure Power Platform when select 'Auto-generate the OpenAPI V2 specification' in 9.0 Web API project: A failure occurred downloading NuGet package Microsoft.Extensions.Apidescription.Server9.0.0-preview.6.24324.2. As this bug has a workaround: add package Microsoft.Extensions.Apidescription.Server. So I directly use command line to try this scenario (without VS).