Could not load file or assembly 'System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1
See original GitHub issueDescribe the bug I have System.Runtime.CompilerServices.Unsafe, Version=6.0.0.0 installed on my system, yet MailKit is referencing System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1 which is not installed.
In App.Config, I have:
<dependentAssembly> <assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" /> </dependentAssembly>
Platform (please complete the following information):
- OS: Window 11 Pro
- .NET Runtime: 4.8
- .NET Framework: 4.8
- MailKit Version: 3.4.2
Exception
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)’
Code Snippets var message = new MimeMessage(); // exception on this line
Additional context System.IO.FileLoadException HResult=0x80131040 Message=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) Source=System.Memory StackTrace: 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() at SharpLibTestApp.MailKitForm.button1_Click(Object sender, EventArgs e) in C:\Users\Jamal\Documents\Visual Studio 2022\Projects\SharpLib\SharpLibTestApp\MailKitForm.cs:line 75 at System.Windows.Forms.Control.OnClick(EventArgs e) at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.ButtonBase.WndProc(Message& m) at System.Windows.Forms.Button.WndProc(Message& m) at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData) at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) at LimoWizCSharpLibTestApp.Program.Main() in C:\Users\Jamal\Documents\Visual Studio 2022\Projects\SharpLib\CSharpLibTestApp\Program.cs:line 25
Issue Analytics
- State:
- Created 10 months ago
- Comments:7 (5 by maintainers)
@jstedfast, thanks for the response. I tried all suggested fixes from the various sources on to no avail. The good news that If I create a clean Winforms App using .NET Framework 4.0 (in VS 2022 Pro), then MailKit works fine. I also updated CompilerServices.Unsafe assembly to version 6.0 via NuGet Package Manager and MailKit continued to work fine. I am adding other 3rd party libs to see what would trigger the issue. So, definitely, it’s a conflict somewhere. I find out the real reason, I’ll give feedback.
@JamalAPI From what I can tell, your App.config file is wrong. It needs to be this: