Swashbuckle.AspNetCore: Unhandled exception. System.IO.FileLoadException: Could not load file or assembly 'System.Runtime, Version=7.0.0.0

I ran into an issue when I was trying to generate swagger.json file using swashbuckle.aspnetcore.cli in version <string>6.5.0 on machine with .net sdk installed in both versions (6 and 7)

Unhandled exception. System.IO.FileLoadException: Could not load file or assembly 'System.Runtime, Version=7.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
The located assembly's manifest definition does not match the assembly reference. (0x80131040) 
File name: 'System.Runtime, Version=7.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'

dotnet --info:

PS C:\Users\my_user_name> dotnet --info
.NET SDK:
 Version:   7.0.101
 Commit:    bb24aafa11

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.19042
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\7.0.101\

Host:
  Version:      7.0.1
  Architecture: x64
  Commit:       97203d38ba

.NET SDKs installed:
  6.0.404 [C:\Program Files\dotnet\sdk]
  7.0.100 [C:\Program Files\dotnet\sdk]
  7.0.101 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 5.0.17 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 6.0.11 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 6.0.12 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 7.0.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 7.0.1 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 5.0.17 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 6.0.11 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 6.0.12 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 7.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 7.0.1 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 5.0.17 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 6.0.11 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 6.0.12 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 7.0.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 7.0.1 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

Other architectures found:
  x86   [C:\Program Files (x86)\dotnet]
    registered at [HKLM\SOFTWARE\dotnet\Setup\InstalledVersions\x86\InstallLocation]

Environment variables:
  Not set

global.json file:
  Not found

.csproj:

    <PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
    <PackageReference Include="Swashbuckle.AspNetCore.Swagger" Version="6.5.0" />

After uninstalling .net 7 sdk it started working properly. Compilation succeeded. swagger.json file were properly re-built. Putting global.json file didnt help much

About this issue

  • Original URL
  • State: open
  • Created a year ago
  • Reactions: 13
  • Comments: 15 (1 by maintainers)

Most upvoted comments

@Meligy I can confirm that your approach helped. I did sth like this and it started build without any problems.

dotnet new globaljson --sdk-version 6.0.404 --roll-forward latestMinor

Maybe I had wrong file name and this is why it was not working for me initially, but now after using this command I made sure that this file was created properly and my project is building without any issues. Thanks for the hint…

I got the same problem also running on a .NET 6 project, but only when running using .NET 7 SDK.

My project is a .NET 6 project that I’m looking forward to upgrading to .NET 7 later. I have .NET 6 and 7 installed on the machines. And I have a global.json file to control the .NET SDK version rollover to .NET 7. Also I wanted to take advantage of slightly improved dotnet watch command in .NET 7 SDK even before I upgrade the actual project.

When the global.json version is allowed to roll over to .NET 7, "rollForward": "latestMajor", it fails to work with the same error above.

When I set the version to only allow .NET 6 in global.json, like "version": "6.0.202", "rollForward": "latestMinor", the generation seems to work fine.

So, the failing scenario is building .NET 7 SDK while targeting .NET 6 runtime.

> dotnet --version
7.0.203

is also affected and downgrading to swashbuckle.aspnetcore.cli 6.4.0 is still a workaround.

53 days later since @danielloganking question. Any news on 6.5.0?

To help testing, make sure that you run dotnet clean before every build, and also including the --no-incremental flag in the build command, like dotnet build --no-incremental.

I’m currently facing the same issue - my problem is, that I have to have the .Net 7 SDK in my global.json file, as I have two APIs in the solution, one for .Net 6 and one for .Net 7. So in order to build all projects (which works fine) I have to use version 7 of the SDK, but when generating the swagger for the .Net 6 API with the .Net 7 SDK and swashbuckle cli version 6.5.0 I will get this exact exception - so it seems that I either have to separate those two APIs into individual solutions (and repositories) or align both APIs to use either Net 6 or Net 7 in both, a mixture seems to not be possible.