.net 未能加载文件或程序集System.Runtime.InteropServices.运行时信息,版本=4.0.1.0

amrnrhlw  于 2023-03-04  发布在  .NET
关注(0)|答案(1)|浏览(349)

我发现自己在一个奇怪的情况与System.Runtime.InteropServices.RuntimeInformation NuGet包。
我的应用程序面向.NET 4.6.2(必须从4.8降级到4.6.2,因为这是一个客户端应用程序,而一些客户端没有安装4.8),当我尝试在可用的最高.NET版本为4.6.2的计算机上运行它时,我收到以下错误:

IO.FileLoadException: Could not load file or assembly ‘System.Runtime.InteropSer
vices.RuntimeInformation, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b03f5
f7f11d50a3a’ or one of its dependencies. The located assembly’s manifest definit
ion does not match the assembly reference. (Exception from HRESULT: 0x80131040)
File name: ‘System.Runtime.InteropServices.RuntimeInformation, Version=4.0.1.0,
Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a’
   at Topshelf.HostConfigurators.HostConfiguratorImpl.CreateHost()
   at Topshelf.HostFactory.New(Action`1 configureCallback)
WRN: Assembly binding logging is turned OFF.
To enable assembly bind failure logging, set the registry value [HKLM\Software\M
icrosoft\Fusion!EnableLog] (DWORD) to 1.
Note: There is some performance penalty associated with assembly bind failure lo
gging.
To turn this feature off, remove the registry value [HKLM\Software\Microsoft\Fus
ion!EnableLog].
Topshelf.HostFactory Error: 0 : The service terminated abnormally, System.IO.Fil
eLoadException: Could not load file or assembly ‘System.Runtime.InteropServices.
RuntimeInformation, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d
50a3a’ or one of its dependencies. The located assembly’s manifest definition do
es not match the assembly reference. (Exception from HRESULT: 0x80131040)
File name: ‘System.Runtime.InteropServices.RuntimeInformation, Version=4.0.1.0,
Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a’
   at Topshelf.HostConfigurators.HostConfiguratorImpl.CreateHost()
   at Topshelf.HostFactory.New(Action`1 configureCallback)
   at Topshelf.HostFactory.Run(Action`1 configureCallback)
WRN: Assembly binding logging is turned OFF.
To enable assembly bind failure logging, set the registry value [HKLM\Software\M
icrosoft\Fusion!EnableLog] (DWORD) to 1.
Note: There is some performance penalty associated with assembly bind failure lo
gging.
To turn this feature off, remove the registry value [HKLM\Software\Microsoft\Fus
ion!EnableLog].

我在同一台机器上使用Fusion++得到了程序集绑定失败,得到的结果是:

=== Pre-bind state information ===
LOG: DisplayName = System.Runtime.InteropServices.RuntimeInformation, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
 (Fully-specified)
LOG: Appbase = file:///C:/Program Files (x86)/VS/
LOG: Initial PrivatePath = NULL
LOG: Dynamic Base = NULL
LOG: Cache Base = NULL
LOG: AppName = VS.Service.exe
Calling assembly : Topshelf, Version=4.3.0.0, Culture=neutral, PublicKeyToken=b800c4cfcdeea87b.
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: C:\Program Files (x86)\VS\VS.Service.exe.Config
LOG: Using host configuration file: 
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
LOG: Post-policy reference: System.Runtime.InteropServices.RuntimeInformation, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
LOG: GAC Lookup was unsuccessful.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/VS/System.Runtime.InteropServices.RuntimeInformation.DLL.
LOG: Assembly download was successful. Attempting setup of file: C:\Program Files (x86)\VS\System.Runtime.InteropServices.RuntimeInformation.dll
LOG: Entering run-from-source setup phase.
LOG: Assembly Name is: System.Runtime.InteropServices.RuntimeInformation, Version=4.0.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
WRN: Comparing the assembly name resulted in the mismatch: Build Number
ERR: The assembly reference did not match the assembly definition found.
ERR: Run-from-source setup phase failed with hr = 0x80131040.
ERR: Failed to complete setup of assembly (hr = 0x80131040). Probing terminated.

尽管如此,我不知道在这种情况下应该怎么做,我安装的依赖于System.Runtime.InteropServices.RuntimeInformation的包有TopShelfCliWrapNETStandard.Library
当我在IDE中检查该引用时,它显示程序集版本为4.0.1.0,但在输出文件夹中,DLL的程序集版本为4.0.2.0
任何帮助都将不胜感激。谢谢。

oknwwptz

oknwwptz1#

如果输出文件夹中存在DLL,但其版本与依赖程序包所需的版本不同,则可以添加一个名为“PackageName.dll.config”的文件(对于每个依赖程序包),其中包含以下内容:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Runtime.InteropServices.RuntimeInformation" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.0.2.0" newVersion="4.0.2.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

将文件放在项目的根文件夹中,并将其“生成操作”设置为“内容”,将“复制到输出目录”设置为“如果更新则复制”。这将指向可以接受DLL现有版本的依赖包。
(P.S.:我也遇到过类似的错误,但在我的情况下,尽管项目显式引用了System.Runtime.InteropServices.RuntimeInformation DLL,但在生成项目时它并没有被复制到输出文件夹中🙁)

相关问题