Today I was working on a sample Asp.NET Core 3.1 WebAPI project I wrote some time ago which uses the library Swashbuckle.AspNetCore.Swagger. I opened Visual Studio and without taking too much care, I updated all the NuGet Dependecies. I forgot to not have toggled the option to use the pre-release version: first “error”.
Anyway, I compiled the project, executed it and then I got an HTTP404 error while accessing the swagger.json file. Error displayed in the browser: “undefined /swagger/v1/swagger.json”
I tried to review the code, but all was fine.
The idea I had, was to host the project not using IISExpress but the console host application generated directly from Visual Studio and then here I had in clear the problem (not visible anywhere else):
fail: Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware An unhandled exception has occurred while executing the request. System.MissingMethodException: Method not found: ‘Void Microsoft.OpenApi.Writers.OpenApiJsonWriter..ctor(System.IO.TextWriter)’. at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.RespondWithSwaggerJson(HttpResponse response, OpenApiDocument swagger) at System.Runtime.CompilerServices.AsyncMethodBuilderCore.Start[TStateMachine](TStateMachine& stateMachine) at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.RespondWithSwaggerJson(HttpResponse response, OpenApiDocument swagger) at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider) at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context) at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context) at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.g__Awaited|6_0(ExceptionHandlerMiddleware middleware, HttpContext context, Task task)
Thanks to this stack trace, I arrived after a superfast Bing research at this blog entry: https://stackoverflow.com/questions/59992762/asp-net-core-3-1-and-swashbuckle-aspnetcore-swagger where it’ stated that: Microsoft.OpenApi 1.2.0-preview isn’t compactibile with .net core 3.1 and swagger v5
Switching back to Microsoft.OpenApi 1.1.4 solved the issue.