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 无用, 其它的没有测试。

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

友情链接更多精彩内容