6

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.

pradeep
  • 175
  • 1
  • 13
  • Are you using MimeKit 3.4.1? https://www.nuget.org/packages/MimeKit Is v.4.5.3 the MimeKit version? What is your c# Target version? – jdweng Oct 13 '22 at 12:14
  • MimeKit got updated to 3.4.1 when I updated MailKit. .Net framework 4.8. – pradeep Oct 13 '22 at 12:17
  • I think you upgraded the Mimekit but VS is still looking for old version. Try deleting in solution explorer the MimeKit version and then add reference to fix version. Got clue from following : https://learn.microsoft.com/en-us/answers/questions/413435/the-located-assembly39s-manifest-definition-does-n.html – jdweng Oct 13 '22 at 12:20
  • Try cleanbuild or even better delete the bin folder (make copy). The Mimekit dll should be then copied to the bin folder. You can check date of the dll file. Also open csproj file with notepad and check the location of Mimekit and version. I think something failed with the upgrade to Net 4.8. – jdweng Oct 13 '22 at 12:37
  • This is apparently a known bug with System.Runtime.CompilerServices.Unsafe dependency resolution. Lots of projects are having this problem when multiple nuget dependencies depend on different versions of System.Runtime.CompilerServices.Unsafe because of their versioning scheme or something. It sounds like newer versions of MSBuild fix this, so perhaps the OP can try upgrading their Visual Studio installation. – jstedfast Nov 23 '22 at 14:57
  • I've submitted a bug report about this type of problem to dotnet/sdk github.com/dotnet/sdk/issues/31147 in case you'd like to follow that progress and/or chime in with any specifics that your project has – jstedfast Mar 15 '23 at 13:11

3 Answers3

6

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.

ThatShawGuy
  • 1,363
  • 1
  • 14
  • 27
  • 2
    I've submitted a bug report about this type of problem to dotnet/sdk https://github.com/dotnet/sdk/issues/31147 in case you'd like to follow that progress and/or chime in with any specifics that your project has – jstedfast Mar 15 '23 at 13:11
  • As per the latest comment on GitHub, with the latest System.Memory DLL, the issue should resolve. I tried upgrading to latest Mailkit and Mimekit today, unfortunately, I'm still getting the same System.Runtime.CompilerServices.Unsafe issue. – pradeep Jun 22 '23 at 06:57
0

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

jstedfast
  • 35,744
  • 5
  • 97
  • 110
  • I tried adding this to App.Config but it did not help. – Kirsten Dec 15 '22 at 20:32
  • I thought of trying an earlier version of Mimekit but it does not show in Nuget. My target framework is 4.8 – Kirsten Dec 15 '22 at 20:36
  • I had similar problem with MailKit 4.1.0 (.NET Framework 4.8), but on my machine everything worked ok, and on a client machine not (a problem as in subject). I found a reason - I didn't move all changes to app.config on client's machine. While installing MailKit in VS 2022 into my app.config was automatically added: assemblyBinding and dependentAssembly sections That's why on my machine it worked ok. When I added it also on my client's machine - now it works! So check your app.config – Ursula Aug 09 '23 at 07:53
0

Adding this to my csproj file fixed this

  <PropertyGroup>
    <FindDependenciesOfExternallyResolvedReferences>true</FindDependenciesOfExternallyResolvedReferences>
  </PropertyGroup>
rojobo
  • 476
  • 4
  • 16