System.IO.FileLoadException:无法加载文件或程序集'System.Runtime.CompilerServices.Unsafe,版本= 4.0.4.1

问题描述 投票:0回答:3

我一直在使用 Mailkit 2.15,现在尝试升级到 v3.4.1。当我升级时,它的所有依赖项都已安装,包括 System.Runtime.CompilerServices.Unsafe v4.5.3。但是当我执行代码时,出现以下异常。

13-Oct-2022 16:33:19,303 [INFO ] Mail SendEmail       - System.IO.FileLoadException: Could not load file or assembly 'System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
File name: 'System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
   at System.Span`1..ctor(T[] array)
   at MimeKit.Utils.ValueStringBuilder..ctor(Int32 initialCapacity)
   at MimeKit.Utils.Rfc2047.Encode(FormatOptions options, Encoding charset, String text, Boolean phrase)
   at MimeKit.Header.EncodeUnstructuredHeader(ParserOptions options, FormatOptions format, Encoding encoding, String field, String value)
   at MimeKit.Header.EncodeAddressHeader(ParserOptions options, FormatOptions format, Encoding encoding, String field, String value)
   at MimeKit.Header.FormatRawValue(FormatOptions format, Encoding encoding, String value)
   at MimeKit.Header.SetValue(FormatOptions format, Encoding encoding, String value)
   at MimeKit.Header..ctor(Encoding encoding, HeaderId id, String value)
   at MimeKit.HeaderList.set_Item(HeaderId id, String value)
   at MimeKit.MimeMessage..ctor()

当我进一步调查时,我注意到 MimeKit 需要 System.Memory,而 System.Memory 安装了 System.Runtime.CompilerServices.Unsafe v4.5.3。但我不确定为什么仍然需要 [System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1]。

注意:在Mailkit 3.4.1升级之前,我的项目不依赖于System.Runtime.CompilerServices.Unsafe,并且它没有在参考文献中列出。

c# .net mailkit mimekit
3个回答
6
投票

我在 .NET 4.7.2 项目中遇到了完全相同的问题。就我而言,我们将外部电子邮件功能纳入我们的解决方案中。我们正在添加一个新项目,它将使用 MailKit 处理电子邮件。我遇到了和你一样的错误,并尝试降级到 MailKit 3.1.1,这似乎是没有依赖链(包括 MimeKit)的最后一个版本,需要

System.Runtime.CompilerServices.Unsafe

我完全卸载了 MailKit 及其所有依赖项并安装了 3.1.1 版本。到目前为止它对我有用。我不需要使用最新版本的 MailKit,所以现在使用 3.1.1 就可以了。

就我而言,错误的根源似乎是解决方案中的其他项目之一正在使用旧版本的

System.Runtime.CompilerServices.Unsafe
,这会产生冲突。我必须做更多的研究才能弄清楚如何将此包的不同版本集成到单个解决方案中。


0
投票

这个问题显然是核心 MSBuild 目标中的一个已知错误,可能是因为 System.Runtime.CompilerServices.Unsafe 使用的版本控制方案不一致(?)。我不完全清楚问题是什么。听起来这个问题也已在较新版本的 MSBuild 目标(较新版本的 Visual Studio?)中得到解决。

无论如何,将以下 App.Config 文件添加到您的项目中应该可以解决程序集解析问题:

<dependentAssembly>
    <assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="4.0.4.1" />
</dependentAssembly>

另一种可能性是尝试升级 Visual Studio(尽管我不确定这是否有效)。


0
投票

将其添加到我的 csproj 文件中修复了此问题

  <PropertyGroup>
    <FindDependenciesOfExternallyResolvedReferences>true</FindDependenciesOfExternallyResolvedReferences>
  </PropertyGroup>

© www.soinside.com 2019 - 2024. All rights reserved.