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.

System.Buffers, System.Runtime.CompilerServices.Unsafe, etc cannot be loaded

See original GitHub issue

Describe the bug

I keep getting reports of my MimeKit and MailKit libraries throwing Exceptions trying to load System.Buffers and System.Runtime.CompilerServices.Unsafe.

Some examples:

To Reproduce

I haven’t been able to reproduce it myself, but apparently, it’s enough to add the MimeKit nuget package to a project and then create a new MimeMessage:

Install-Package MimeKit
void Main()
{
    var message = new MimeKit.MimeMessage ();
}

Exceptions (if any)

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

Further technical details

  • Include the output of dotnet --info
  • The IDE (VS / VS Code/ VS4Mac) you’re running on, and its version

Issue Analytics

  • State:open
  • Created 6 months ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
KalleOlaviNiemitalocommented, Mar 31, 2023

To generate binding redirects for a DLL, you’d have to set GenerateBindingRedirectsOutputType=true as well. That’s the default for EXEs but not for DLLs. https://github.com/dotnet/msbuild/blob/4c9e8aa8a981899b046beba9c290dbfc0a6d88ae/src/Tasks/Microsoft.Common.CurrentVersion.targets#L362

0reactions
KalleOlaviNiemitalocommented, Apr 2, 2023

I don’t believe that generating MimeKit.dll.config or MailKit.dll.config with binding redirects in it would fix the problem. Applications are more likely to reference those libraries directly than to load them in dedicated application domains.

Earlier, I filed https://github.com/dotnet/msbuild/issues/7702 about a similar problem with System.Runtime.CompilerServices.Unsafe. That was resolved as a duplicate of https://github.com/dotnet/sdk/issues/2547. The bug is that, if NuGet package includes both reference assemblies and runtime assemblies, then the runtime assemblies can have more dependencies than the reference assemblies (this is by design in Roslyn), but .NET SDK and MSBuild consider only the dependencies of the reference assemblies when they generate binding redirections. On net472, the System.Memory/4.5.1 package includes such a runtime-only dependency on System.Runtime.CompilerServices.Unsafe, hitting the bug. System.Memory/4.5.2 does not have this problem as the reference assemblies were removed in https://github.com/dotnet/corefx/pull/33254.

However, MimeKit/3.4.3 already depends on System.Memory (>= 4.5.5), so this shouldn’t be the problem.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Could not load file or assembly 'System.Buffers, Version= ...
Could not load file or assembly 'System.Buffers, Version=4.0.2.0…' Solution. 1) use CMD(run as Administrator ) and type
Read more >
Could not load file or assembly System.Runtime. ...
I tried to update this System.Runtime.CompilerServices.Unsafe but still having the same problem. Any ideas on how to fix it? Best Regards.
Read more >
Could not load file or assembly 'System.Runtime. ...
Please try to download the Nuget package from here: System.Runtime.CompilerServices.Unsafe. After downloading, you will get a .nupkg file, move ...
Read more >
Please update com.unity.collections reference of System. ...
I am having a similar conflict issue with System. Runtime. CompilerServices. Unsafe.
Read more >
Could not load file or assembly 'System.Buffers' or one of ...
With assembly binding logging enabled, it appears the issue is a binding redirect expecting assembly version 4.0.3.0 , but finding 4.0.2.0 .
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