美文网首页
Could not load file or assembly

Could not load file or assembly

作者: gruan | 来源:发表于2023-04-10 16:28 被阅读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 无用, 其它的没有测试。

相关文章

网友评论

      本文标题:Could not load file or assembly

      本文链接:https://www.haomeiwen.com/subject/rghwddtx.html