简体   繁体   中英

System.IO.FileLoadException: Could not load file or assembly 'System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1

I've been using Mailkit 2.15 and now trying to upgrade to v3.4.1. When I upgraded, all its dependencies got installed including System.Runtime.CompilerServices.Unsafe v4.5.3. But when I execute the code, the following exception occurs.

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()

When I investigated further, I noticed that the MimeKit requires System.Memory and the System.Memory installed System.Runtime.CompilerServices.Unsafe v4.5.3. But I'm not sure why [System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1] is still required.

Note: Prior to Mailkit 3.4.1 upgrade, my project does not depend on System.Runtime.CompilerServices.Unsafe and it is not listed in the references.

I'm running into this exact same issue with a .NET 4.7.2 project. In my case we are pulling external email functionality into our solution. We are adding a new project that will process email using MailKit. I got the same error as you and tried downgrading to MailKit 3.1.1 which appears to be the last version without the chain of dependencies (including MimeKit) that requires System.Runtime.CompilerServices.Unsafe .

I completely uninstalled MailKit and all of its dependencies and installed the 3.1.1 version. It is working for me so far. I don't have any need to use the latest version of MailKit, so using 3.1.1 for now works.

In my case, the source of the error appears to be that one of the other projects in the solution is using an older version of System.Runtime.CompilerServices.Unsafe which is generating a conflict. I'd have to do more research in order to figure out how to integrate separate versions of this package into a single solution.

This issue is apparently a known bug in the core MSBuild targets, perhaps because of the inconsistent versioning scheme that System.Runtime.CompilerServices.Unsafe uses(?). I'm not entirely clear on what the problem is. It also sounds like the issue has been fixed in newer versions of the MSBuild targets (newer versions of Visual Studio?).

In any case, adding the following App.Config file to your project should resolve the problem with assembly resolution:

<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>

Another possibility is to try upgrading Visual Studio (although I'm not sure if that would work or not).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM