Search code examples
c#asp.net-coreexceptionasp.net-core-mvcswagger

Failed to create instance of type 'Microsoft.AspNetCore.Mvc.ApiExplorer.IApiDescriptionProvider'


I cloned an ASP.NET Core project based on .NET 8. The IDE is VisualStudio 22 17.9, it compiles fine. But I got an error when I open the swagger page after startup, and the program doesn't end up running

InvalidOperationException: Failed to create instance of type 'Microsoft.AspNetCore.Mvc.ApiExplorer.IApiDescriptionProvider'. Possible reason is cannot match the best constructor of type 'Microsoft.AspNetCore.Mvc.ApiExplorer.EndpointMetadataApiDescriptionProvider'.

The project author is a colleague of mine who was able to run it correctly. I am using the same environment with him.

I checked Nuget and it should not be missing. I tried reinstalling VisualStudio, but that didn't solve the problem. I looked up the official Microsoft documentation and it looks like the IApiDescriptionProvider is all about attribute in the Controller, like [HttpGet], [HttpPost]. (I'm not sure) I tried to run the front program and the request reported service exception.


Solution

  • After I upgraded VisualStudio (17.9.2 -> 17.9.3), the problem seems to have disappeared and now Swagger displays properly! But I don't know what happened, maybe refer to enter link description here.

    Before version 17.9.3 came out, I commented out the part about Swagger in Program.cs in order to get the program to work. Although Swagger doesn't work, the backend handles network requests fine.

    // Comment out swagger-related code in Program.cs
    builder.Services.AddSwaggerGen(); 
    app.UseSwagger(); 
    app.UseSwaggerUI(); 
    ...