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

Viewed 23836

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?

6 Answers

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

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>

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.

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.

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>

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.

Related