question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

Describe 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:closed
  • Created 10 months ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
JamalAPIcommented, Nov 17, 2022

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

0reactions
jstedfastcommented, Nov 17, 2022

@JamalAPI From what I can tell, your App.config file is wrong. It needs to be this:

<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>
Read more comments on GitHub >

github_iconTop Results From Across the Web

Could not load file or assembly System.Runtime. ...
CompilerServices.Unsafe. I created a Visual Studio (Community 2019) project with C# using ServiceStack.Redis. Since it is C#, I use Windows 10 ( ...
Read more >
Error finding System.Runtime.CompilerServices.Unsafe ...
I'm getting this in dotnet core 2.1 (netcoreapp2.1) Error is: System.IO.FileLoadException:` Could not load file or assembly 'System.Runtime.
Read more >
Is there any good way to deal with this dependency conflict?
I'm working on a large, legacy .NET Framework project that is not easily updateable to Core. After a recent change, it's failing to...
Read more >
Inability to Open PKHeX due to Unhandled Exception Error
FileNotFoundException : Could not load file or assembly 'System. ... System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1, Culture=neutral ...
Read more >
Pyhton intellisense (in django project) stopped working
This enables the web server commands specified in the project file, and launches a browser window when the server has started. Web launcher....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found