Could not load file or assembly System.Runtime, Version=7.0.0.0

錯誤詳情:

System.IO.FileNotFoundException: Could not load file or assembly 'System.Runtime, Version=7.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. 系統找不到指定的文件。
File name: 'System.Runtime, Version=7.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
at System.Reflection.RuntimeAssembly.GetExportedTypes()

問題產生的原因:

  • .NET6 和 .NET7 同時存在,并編譯到不同的文件夾中去。
  • 由于 .NET7 的程序, 在WIN2008 R2 上會占用雙倍內存(不信自己試), 所以在 WIN2008 R2 上部署的是 .NET 6 的程序。
  • .NET7 的程序部署在高版本的服務器上, 沒有這個問題。
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
。。。
    <PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Release|net6.0|AnyCPU'">
        <OutputPath>..\XXX\bin\Release\net6.0\</OutputPath>
        <!--移除輸出目錄中的 netXXX.0 子文件夾, 直接輸出到 debug, release 目錄中-->
        <AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
        <!--將引用的相關DLL一并輸出, 否則, 不會輸出-->
        <CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
    </PropertyGroup>
。。。
    <PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Release|net7.0|AnyCPU'">
        <OutputPath>..\XXX\bin\Release\net7.0\</OutputPath>
        <AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
        <CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
    </PropertyGroup>

解決方法:--roll-forward LatestMajor

參考:.NET選擇要使用的版本 - .NET | Microsoft Learn

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <location path="." inheritInChildApplications="false">
    <system.webServer>
      <handlers>
        <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
      </handlers>
      <aspNetCore processPath="dotnet" arguments="--roll-forward LatestMajor XXX.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="inprocess" />
    </system.webServer>
  </location>
</configuration>

注意 --roll-forward LatestMajor xxx.dll 的順序,寫反了, 是不起作用的。

global.json

至少對 WIN 2008 R2 的 IIS 無用, 其它的沒有測試。

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容