jstedfast / MailKit

A cross-platform .NET library for IMAP, POP3, and SMTP.
http://www.mimekit.net
MIT License
6.04k stars 809 forks source link

Dependencies ERROR #1760

Closed CordaMaurizio closed 4 weeks ago

CordaMaurizio commented 4 weeks ago

I have a C# project that uses MailKit and MimeKit version 3.0.0, but I need to upgrade the System.Runtime.CompilerServices.Unsage package to version 6.0.0. So I updated MailKit and MimeKit to the latest version 4.6.0, but now, as soon as I create a MimeMessage object, the error comes out:

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 it's dependences. The found Assembly's manifest definition does not match the Assembly reference. (Exception from HRESULT: 0x80131040)"

even if the dependency for the package is 6.0.0 this is the packages.config <?xml version="1.0" encoding="utf-8"?>

CordaMaurizio commented 4 weeks ago

"BouncyCastle" version="1.8.1" targetFramework="net40" "BouncyCastle.Cryptography" version="2.3.1" targetFramework="net48" "MailKit" version="4.6.0" targetFramework="net48" "Microsoft.Bcl.AsyncInterfaces" version="7.0.0" targetFramework="net48" "MimeKit" version="4.6.0" targetFramework="net48" "Newtonsoft.Json" version="13.0.3" targetFramework="net48" "OpenPop" version="2.0.6.2" targetFramework="net40" "Portable.BouncyCastle" version="1.9.0" targetFramework="net48" "System.Buffers" version="4.5.1" targetFramework="net48" "System.Formats.Asn1" version="8.0.0" targetFramework="net48" "System.Memory" version="4.5.5" targetFramework="net48" "System.Numerics.Vectors" version="4.5.0" targetFramework="net48" "System.Runtime.CompilerServices.Unsafe" version="6.0.0" targetFramework="net48" "System.Text.Encodings.Web" version="7.0.0" targetFramework="net48" "System.Text.Json" version="7.0.2" targetFramework="net48" "System.Threading.Tasks.Extensions" version="4.5.4" targetFramework="net48" "System.ValueTuple" version="4.5.0" targetFramework="net48"

jstedfast commented 4 weeks ago

You should get rid of the following conflicting packages:

The error you are getting is not anything I can fix. You may need to update your project to use PackageReferences instead of packages.config, but I'm not even sure that would solve it for you.

The issue is that System.Runtime.CompilerServices.Unsafe v6.0.0 is really v4.0.4.1, but there is a bug in the MSBuild build targets files that generate assembly mappings.

This issue comes up over and over and over again and my GitHub issue tracker is full of dozens of these reports.

StackOverflow also has this same question (and not all of them are for MimeKit/MailKit). That's how I discovered that it's an MSBuild issue.

I started shipping MimeKit.dll.config files for each framework target which is what was recommended by the Microsoft engineers who diagnosed this issue,and that seemed to fix it for most people, but obviously isn't working for you.

I can only guess it's because you've got an old version of Visual Studio and/or because you are using packages.config instead of PackageReferences.

jstedfast commented 4 weeks ago

Here's some of the issues reported against MimeKit (note that S.R.CS.Unsafe is not the only assembly that causes this issue):

I would say this one probably has the most useful info: https://github.com/jstedfast/MailKit/issues/1462

...and from this dotnet/sdk bug report: https://github.com/dotnet/sdk/issues/31147

quote:

Our guidance for end applications is usually to use PackageReference if they are not in order to have RAR be able to see transitive dependencies, and to make sure they have auto-binding redirects turned on (which are by default). I suppose main issue here is that IN the case of MimeKit and MailKit they are libraries, not end applications, which don't have binding redirects on by default. One advise that could help would be to a) cross-compile for a .NET Framework TFM in case they are currently only targeting .NET Standard, and B) turn on generation of binding redirects for the library project and pack the .dll.config with the NuGet package.

I've already implemented both of their solutions in MimeKit and MailKit:

You'll notice that each MimeKit and MailKit package ships with assemblies for net462, net47, and net48 and also the *.dll.config files.