Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Microsoft.Extensions.ApiDescription.Server Unable to find service type 'Microsoft.Extensions.ApiDescriptions.IDocumentProvider' in dependency injection container. #57939

Open
1 task done
JTeeuwissen opened this issue Sep 18, 2024 · 3 comments
Labels
area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates feature-openapi Needs: Author Feedback The author of this issue needs to respond in order for us to continue investigating this issue.

Comments

@JTeeuwissen
Copy link

JTeeuwissen commented Sep 18, 2024

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

I ran into this bug while trying to use Microsoft.Extensions.ApiDescription.Server for openapi file generation.

Given this code (see repro for .sln)

using NSwag.AspNetCore;

var builder = WebApplication.CreateBuilder(args);

MethodThatBreaksBuild(builder);

var app = builder.Build();

app.MapGet("/hello", () => "Hello World!");

app.UseSwaggerUi(settings => settings.SwaggerRoutes.Add(new SwaggerUiRoute("v1", "/openapi/v1.json")));
app.MapOpenApi();

app.Run();

void MethodThatBreaksBuild(WebApplicationBuilder webApplicationBuilder)
{
  webApplicationBuilder.Services.AddOpenApi();
}

and dependencies

<ItemGroup>
  <PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.0-rc.1.24452.1" />
  <PackageReference Include="Microsoft.Extensions.ApiDescription.Server" Version="9.0.0-rc.1.24452.1">
    <PrivateAssets>all</PrivateAssets>
    <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
  </PackageReference>
  <PackageReference Include="NSwag.AspNetCore" Version="14.0.2" />
</ItemGroup>

the generation during build fails on resolving the IDocumentProvider, although it is registered using AddOpenApi. Whenever I inline MethodThatBreaksBuild generation works as expected. Similar behavior occurs for MapOpenApi and conditional execution.

Expected Behavior

I expect an Example.json to be generated during build, without any errors.

Steps To Reproduce

Build this solution

Example.zip

Exceptions (if any)

dotnet build Example.sln

Restore complete (1,4s)
You are using a preview version of .NET. See: https://aka.ms/dotnet-support-policy
  Example failed with 2 error(s) (9,3s) → Example\bin\Debug\net9.0\Example.dll
    C:\Users\x\.nuget\packages\microsoft.extensions.apidescription.server\9.0.0-rc.1.24452.1\build\Microsoft.Extensions.ApiDescription.Server.targets(68,5): error : Unable to find service type 'Microsoft.Extensions.ApiDescriptions.IDocumentProvider' in dependency injection container. Update the 'Startup' class to register a document.
    C:\Users\x\.nuget\packages\microsoft.extensions.apidescription.server\9.0.0-rc.1.24452.1\build\Microsoft.Extensions.ApiDescription.Server.targets(68,5): error MSB3073: The command "dotnet "C:\Users\x\.nuget\packages\microsoft.extensions.apidescription.server\9.0.0-rc.1.24452.1\build\../tools/dotnet-getdocument.dll" --assembly "C:\Users\x\Desktop\Example
\Example\bin\Debug\net9.0\Example.dll" --file-list "obj\Example.OpenApiFiles.cache" --framework ".NETCoreApp,Version=v9.0" --output "C:\Users\x\Desktop\Example\Example" --project "Example" --assets-file "C:\Users\x\Desktop\Example\Example\obj\project.assets.json" --platform "AnyCPU" " exited with code 10.

Build failed with 2 error(s) in 11,7s

.NET Version

9.0.100-rc.1.24452.12

Anything else?

No response

@dotnet-issue-labeler dotnet-issue-labeler bot added the area-web-frameworks *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels label Sep 18, 2024
@PaskalSunari
Copy link

downgrade to previous version if needed

@captainsafia captainsafia added area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc and removed area-web-frameworks *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels labels Sep 19, 2024
@captainsafia
Copy link
Member

@JTeeuwissen Thanks for reporting this issue!

I believe the issue here might actually be related to the fact that you reference both NSwag.AspNetCore and Microsoft.AspNetCore.OpenApi in your project.

The IDocumentProvider interface is internal and it's up to each OpenAPI implementation to define it in their sources. The lookup logic that we use to resolve the IDocumentProvider interface through private reflection does so by querying for it across all assemblies that have been loaded in tho the application domain:

Type serviceType = null;
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
{
serviceType = assembly.GetType(DocumentService, throwOnError: false);
if (serviceType != null)
{
break;
}
}

I suspect that when the AddOpenApi method is moved to its own method, it's shifted lower in the list of loaded assemblies and so the codepath above ends up resolving the IDocumentProvider interface from NSwag's assemblies. Since you never called NSwag's registration method, it fails to resolve one that has been registered.

If you're only using NSwag to serve Swagger UI, I would recommend using Swashbuckle's Swashhbuckle.AspNetCore.SwaggerUi package if you're able to. It splits up the Swagger UI-related APIs into their own assembly so you don't run into any issues with IDocumentProvider discovery failing.

Can you try swapping out Swashbuckle in your sample app and verifying if that solves the issue for you?

P.S.: I realize the lookup logic in ApiDescription.Server is super spooky given that you can run into issues like this. I'm hoping we can do something drastic to improve the package in the future. See my comment on this issue for more info.

@captainsafia captainsafia added the Needs: Author Feedback The author of this issue needs to respond in order for us to continue investigating this issue. label Sep 19, 2024
Copy link
Contributor

Hi @JTeeuwissen. We have added the "Needs: Author Feedback" label to this issue, which indicates that we have an open question for you before we can take further action. This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates feature-openapi Needs: Author Feedback The author of this issue needs to respond in order for us to continue investigating this issue.
Projects
None yet
Development

No branches or pull requests

4 participants