Could not load file or assembly 'System.Buffers, Version=4.0.2.0...'
Asked Answered
P

9

27

I'm getting the following exception when trying to call GetDatabase method of the MongoClient class after adding a new configuration using VS config. manager:

Could not load file or assembly 'System.Buffers, Version=4.0.2.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

I installed the latest System.Buffer nuget package v4.5.1, created dependentAssembly in my app.config and Reference in my .csproj file, but I still have the same issue. For some reason it tries to reference System.Buffer with v4.0.2. Has anyone had a similar error and how did you solve it?

Phio answered 21/7, 2020 at 16:41 Comment(5)
Did you do a clean build? When Net is changed the dependency of the obj file are not using Net Version. So you have to do a clean build or delete the bin folder of project. Also check if the latest dlls in the bin folder are in correct folder (debug/release).Overheat
Please try to enter the System.Buffer nuget package v4.5.1 in CMD(run as administrator. run cd xxxx(xxxx\packages\System.Buffers.4.5.0\lib\netstandard2.0) and then type : gacutil /i System.Buffers.dll. You can refer to this similar issue. Then when you finish it, please run update-package -reinstall under package manager console to reinstall the package. Besides, you could try to change Version=4.0.2.0 to Version=4.0.3.0 in csproj file.Franzen
Please let us know if it helps.Franzen
jdweng: sure. Still see the same issue.Phio
stackoverflow.com/users/12450747/perry-qian-msft thank you a lot! It works for me.Phio
F
10

Could not load file or assembly 'System.Buffers, Version=4.0.2.0…'

Solution

1) use CMD(run as Administrator ) and type

 cd xxxx(xxxx\packages\System.Buffers.4.5.1\lib\netstandard2.0))

run

gacutil /i System.Buffers.dll

Then, when you finish it, please run update-package -reinstall under package manager console to reinstall the package.

2) you can try to change Version=4.0.2.0 to Version=4.0.3.0 in csproj file.

Besides, there is a similar issue you can refer to.

Franzen answered 22/7, 2020 at 9:39 Comment(2)
If your code is throwing an exception about, "Could not load file or assembly 'System.Buffers, Version=4.0.2.0" you need to do as this answer says but then download System.Buffers 4.4.0 instead of System.Buffers 4.5.1. Start by downloading System.Buffers 4.4.0 from nuget.org/packages/System.Buffers/4.4.0 and then extract it to a folder of your choice. In my case after extracting it, I had my System.Buffers.dll in this folder path C:\System.Buffers 4.4.0\lib\netstandard2.0\Roath
Can someone clarify what is xxxx(xxxx in this answer?Avertin
P
9

In my case, I had to add the assembly reference in the web.config that was already published in the Azure App Service, inside the <configuration></configuration> tag:

<runtime>
  <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
    <dependentAssembly>
      <assemblyIdentity name="System.Buffers" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
      <bindingRedirect oldVersion="0.0.0.0-4.0.3.0" newVersion="4.0.3.0" />
    </dependentAssembly>
  </assemblyBinding>
</runtime>
Preter answered 1/12, 2021 at 15:6 Comment(1)
Is there bug in nuget. Because nuget saying it installed 4.5.1 but when I right click on system.buffers it says 4.0.3.0. In any case, your solution worked. ThanksVermiculite
P
4

I ran into this issue when adding the Azure.Storage.Blobs package to a Class Library where I was attempting to create a blob uploader client. When I added the package, it created an app.config with the correct binding redirect:

      <dependentAssembly>
        <assemblyIdentity name="System.Buffers" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.0.3.0" newVersion="4.0.3.0" />
      </dependentAssembly>
      <dependentAssembly>

The issue, however, is that this binding redirect needs to live in the app.config of the application, not the class library: Can a class library have an App.config file?

So, adding the binding redirect is the correct way to solve this issue. Just make sure the binding redirect is specified in the calling web/console/etc app.

Polythene answered 21/7, 2022 at 15:29 Comment(0)
P
3

The easiest way is to use Nuget package manager, downgrade system.buffers to any lower version, and then upgrade it to the latest again.

Prescriptive answered 7/2, 2021 at 8:1 Comment(0)
D
1

In my case, the answer was that the dependencies were in a DLL, but the EXE referencing the DLL needed to also contain the assembly bindings in the runtime section of the app.config:

  <dependentAssembly>
    <assemblyIdentity name="System.Buffers" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-4.0.3.0" newVersion="4.0.3.0" />
  </dependentAssembly>

Rebuilding the app then resolved the issue for me. VS2017 C# Winforms app .NET frameworks 4.6.1.

Diffluent answered 30/5, 2023 at 22:40 Comment(0)
S
0

Another solution: just remove references to the assembly in the Web.config files of the projects generating the exception.

The references can look like:

<dependentAssembly>
  <assemblyIdentity name="System.Text.Encodings.Web" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
  <bindingRedirect oldVersion="0.0.0.0-4.0.4.0" newVersion="4.0.4.0" />
</dependentAssembly>
Schopenhauer answered 4/8, 2021 at 13:31 Comment(0)
S
0

Just install NuGet Package 4.4.0 and deploy depending Assembly ( System.Buffers ) to Gac.

Sibeal answered 12/9, 2023 at 7:42 Comment(0)
M
0

We are experiencing problems with the accepted solution. Our software (32 bit .NET Framework) ships System.Buffers (.NET 4.6.1) with it and we haven't had any problems resolving it that way.

We think this is actually a proper solution to the issue, but of course requires distributing/installing the software again.

However, we now experience crashes with our software on machines were System.Buffers.dll (netstandard1.1) is located in GAC. Since GAC is preferred (here) and the GAC dll is loaded instead of the one we ship, we then later get a problem resolving System.Threading.dll and System.Runtime.dll (both in GAC installed by .NET Framework).

Removing the dll from GAC solves our problem, but obvious might break whoever placed the dll in GAC.

Examining the Fusion log we see that System.Runtime.dll was not found (despite being in GAC) and it was needed by System.Buffers.dll.

loading System.Runtime.dll

To our surprise we saw that System.Buffers.dll is loaded from GAC, even if we ship it in the installation.

loading System.Buffers.dll

We were also able to reproduce the issue by placing the .NETStandard 1.1 version of System.Buffers in GAC:

System.Buffers.dll

Workarounds/solutions:

  1. Always ship System.Buffers.dll, don't install to GAC
  2. In the Fusion log for System.Runtime.dll we see that the application has an assembly redirect. Removing this allowed our application to start. We don't recommend this as a permanent solution for a number of reasons.
    1. Our software would be running with a different version of System.Buffers than we intend
    2. The assembly redirects are generated by the paket dependency system, which we would effectively have to modify somehow.
Madra answered 15/3, 2024 at 8:32 Comment(0)
S
-1

I ran into a similar issue, tried all the steps above. In the end, another project had an older version of the same library. Too many projects in the solution, the other reference was scrolled out of view.

Sanctuary answered 20/4, 2022 at 21:20 Comment(2)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Barra
This does not really answer the question. If you have a different question, you can ask it by clicking Ask Question. To get notified when this question gets new answers, you can follow this question. Once you have enough reputation, you can also add a bounty to draw more attention to this question. - From ReviewOurs

© 2022 - 2025 — McMap. All rights reserved.