Swashbuckle.aspnetcore: Upgrading to ASP.NET Core 2.0 Preview 2 breaks Swagger UI

Created on 4 Jul 2017  路  25Comments  路  Source: domaindrivendev/Swashbuckle.AspNetCore

After upgrading to .NET Core 2.0 Preview 2 Navigating to the default /Swagger route results in the following exception:

System.MissingMethodException: Method not found: 'Void Microsoft.Net.Http.Headers.EntityTagHeaderValue..ctor(System.String)'.
   at Microsoft.AspNetCore.StaticFiles.StaticFileContext.LookupFileInfo()
   at Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.StaticFiles.DefaultFilesMiddleware.Invoke(HttpContext context)
   at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.<Invoke>d__6.MoveNext()

Steps to reproduce

  1. Upgrade .NET Core to 2.0 preview 2. Install latest VS to work with new API.
  2. Create new Web API Project.

Change TargetFramework to netcoreap2.0. Manually edit the project file to update the packages as follows so the site runs:

<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.0.0" />
<PackageReference Include="Microsoft.AspNetCore" Version="2.0.0-preview2-final" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.0.0-preview2-final" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="2.0.0-preview2-final" />

Configure Swagger as per the documentation in this repo's README.md

  1. Navigate to the /swagger route.

Expected outcome

The Swagger UI for the default API in the web project

Actual outcome

The Exception above

Most helpful comment

I had the same issue and the solution for me was to install the Microsoft.AspNetCore.StaticFiles Nuget package.

All 25 comments

The exception looks like it comes from the static files middleware rather than Swashbuckle. Have you tried specifically adding a reference to the StaticFiles package to ensure you have the latest pinned? There's all sorts of weird stuff that happens when you try to mix release with preview bits. Happened when they were first coming out with netcore also. Work in progress that has breaking changes they may or may not keep in the next release... Painful.

I'd look at all the packages Swashbuckle references and manually add them all to my project. Ensure they're the preview versions. That will force the latest to be used.

If it still doesn't work I'd recommend debugging into Swashbuckle to at least get an exact location of where it's exploding. I'm sure when .NET Core 2 is closer to stable the will be a better compatibility story. Until then, it'd be super helpful to get folks trying out the bleeding edge bits to help with the troubleshooting, too.

Hi @tillig,

Explicitly adding a NuGet reference to StaticFiles preview2 worked. Thanks very much for your help. The following are the package references that will make preview 2 work with Swagger UI for anyone who Googles their way to this issue:

<ItemGroup>
    <PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.0.0" />
    <PackageReference Include="Microsoft.AspNetCore" Version="2.0.0-preview2-final" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.0.0-preview2-final" />
    <PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="2.0.0-preview2-final" />
    <PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="2.0.0-preview2-final" />
    <PackageReference Include="Swashbuckle.AspNetCore" Version="1.0.0" />
</ItemGroup>

I did not experienced that with latest ASP.NET Core 2.0 packages from CI

I had the same issue and the solution for me was to install the Microsoft.AspNetCore.StaticFiles Nuget package.

Aetherix, thanks a lot... it's fixed my problem.

I had all the same, uninstall/reinstall of Swagger fixed me.

I think it makes sense that the "Swashbuckle.AspNetCore" NuGet package has an explicit dependency on "Microsoft.AspNetCore.StaticFiles".

Adding "Microsoft.AspNetCore.StaticFiles" explicitly didn't work for me. Uninstalling "Swashbuckle.AspNetCore" and reinstalling it DID fix the problem.

What did the trick for me:
1) Delete all old files from your deployment folder (I tried with IIS, also works for other hosting types)
2) Make sure you have these packages in your project file

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0" />
    <PackageReference Include="Swashbuckle.AspNetCore" Version="1.0.0" /> 
  </ItemGroup>
  • Replace all Microsoft.AspNetCore.* with Microsoft.AspNetCore.All meta package - see this post for the details.
  • Just case of the side effects, reinstall Swashbuckle.AspNetCore package (no other Swashbuckle.AspNetCore.* needed)
    3) Publish (or deploy by copying the files) to your deployment folder.
    4) Now it should work. 馃憤

Aetherix, like wsun4ipipeline your solution did the trick. It fixed my problem.

For me, installing Microsoft.AspNetCore.Mvc and Microsoft.AspNetCore.StaticFiles made it working. Thank you all.

Remember to add:

public void Configure(IApplicationBuilder app)
{
    app.UseStaticFiles();
}

I'm still struggling with this issue, any other ideas? Thanks.

Hi @tillig,

By adding staticFiles nuget it worked for me.Thanks for your help.

Thanks Guys,

Installing **

Install-Package Swashbuckle.AspNetCore.SwaggerUI

** worked for me.

I have installed Swashbuckle.AspNetCore.SwaggerUI and am still having this issue. I believe we are off the preview of ASP.NET Core 2.0 though right?

Thanks Hikalkan. Installing Microsoft.AspNetCore.StaticFiles worked for me on vs on mac os high sierra

nuget-shit

Let me tell that as polite as possible: I FUCKING HATE THAT FUCKING TRANSITIVE NUGET REFERENCES! [1]

Please anyone tell me which NuGet/VS-Tool will show me that there are old packages (like StaticFiles 1.0.4) which could be updated. Or how I can change that silly "most minor version" preference of transitive package restore.

Ranting some more: In this special case (aspnetcore) I could use Microsoft.AspNetCore.All if it wasn't tied to netcoreapp2.0 -- I am using full .net framework so I cannot use this package.

[1] Of course I don't like the old style either: Where 1001 packages are listed in packages.config and no one knows why any of them is listed there.

@springy76 transitive nuget references aren't the problem here. You're just experiencing a version mismatch and a breaking change. This is a just an honest incompatiblity and nothing saves you here.

Here's what could have been done and what still could be done to make this issue fail better (becuse it'll still fail it'll just fail better).

ASP.NET 1.x should set upperbounds on our packages for the next major version. If we did that, you'd get an error that StaticFiles 1.0.x cannot function with ASP.NET Core 2.x.

Hi there !

Regarding support to aspnet core 2.1... As you can see on the print screen Im having this issue:

image

Do I real need to add the *all metapackages ?

Does the namespace change ? Why is it broke ?

Same here, just like vpaulino - after upgrading to 2.1 build was broken with the same runtime exception:

MissingMethodException: Nie odnaleziono metody: 'Void Microsoft.Net.Http.Headers.EntityTagHeaderValue..ctor(System.String)'.
    Microsoft.AspNetCore.StaticFiles.StaticFileContext.LookupFileInfo()
    Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context)
    Microsoft.AspNetCore.StaticFiles.DefaultFilesMiddleware.Invoke(HttpContext context)
    Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext)

It turns out that the ctor really ceased to exist. It now takes the new StringSegment instead of plain String, see https://docs.microsoft.com/en-us/dotnet/api/microsoft.net.http.headers.entitytagheadervalue?view=aspnetcore-2.1 . While StringSegment and String are convertible, that's only during compilation. On the assembly side the signature of the constructor has changed, so without recompiling it cannot just be wired up. I think it all boils down to something in your project pulling the static-files in 2.0 instead of 2.1?

Back on topic - yup - Swagger.UI 1.1.0 bases on StaticFiles 1.0.4 and that collides with asp-core 2.1 that upgrades that to static-files-2.1 which has the new construtor style. Well.. that ref wasn't updated for a long time (--"). However, even after updating Swagger to the newest 2.5.0, it still depends on StaticFiles 1.0.4 and still crashes.

So far I've NOT managed to force my projects to use the old library by

<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="[1.0.4,2.1)" />

FYI: I cloned SwaggerUI, updated the ref to 2.1.1, recompiled, and everything works fine.

Swashbuckle.AspNetCore v3 still has this problem. Any workarounds (apart from what @quetzalcoatl did)?

What I had to do for V2 and V3 of Swagger was to add a NuGet reference to Microsoft.AspNetCore.StaticFiles 2.1.1. Hope this help!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

alasvant picture alasvant  路  3Comments

mrmartan picture mrmartan  路  3Comments

NinoFloris picture NinoFloris  路  3Comments

govin picture govin  路  3Comments

rgelb picture rgelb  路  3Comments