Could not load file or assembly 'System.Memory, Version=4.0.1.' in Visual Studio 2015

25,306

Solution 1

If this error is appearing in an IIS app such as ASP.NET, then there is a high probability that you are missing a binding redirect in the web.config.

When you install from Nuget, you should see a binding redirect in the web.config.

If you deploy the application to another machine but fail to set up the binding redirect on that secondary machine, then this error can appear.

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

Solution 2

For some reasons, the specific nugget package was not installed properly and I had not the option to compare it with an older file because I had messed up the backup file. I cloned the repository again(even if I had to discard some progress).

Steps:

  1. I had .Net version issues by downloading via nugget package manager so I had to do it manually.

  2. I have downloaded the .nupkg file from https://www.nuget.org/packages/System.Memory/

  3. Extracted the dll with a batch file found in https://gist.github.com/fex80/976ff887acb825171de6

  4. Copied to the bin folder of the proper project .

  5. Rebuild the project and at last, it worked.

Solution 3

Actually this answer come from a senior and he just helped to solve it! He said because the solution keep the old files -> this error, you can try to open each projects of solution and then remove folders 'bin' and 'obj' -> clean build again! Just an option you can try!

Solution 4

Use IL Disassembler (ildasm.exe) to check if the version in your output directory matches the one in the error message.
Our software often runs into these kind of problems because different nuget packages require different versions together with a nasty mix of libraries not in nuget.
The need for different versions of the same library is not considered by the build process.

If the version mismatch is really the cause of your problem, you may be looking for binding redirects in you app.config to forward all older versions of that library to the newest one.
Something like this will need to be added to the <dependentAssembly> node of the library in question:

<bindingRedirect oldVersion="0.0.0.0-4.2.0.0" newVersion="4.2.0.0" />

ildasm can usually be found here (depending on your Visual Studio version)
C:\Program Files (x86)\Microsoft SDKs\Windows\v8.1A\bin\NETFX 4.5.1 Tools\ildasm.exe

Solution 5

I just had a similar problem with a console app I'm working on. It was working fine last night. Today I got the error message "Could not load file or assembly 'System.Memory, Version=4.0.1.0...". I wasn't able to resolve this until I went into Configuration Manager for my Visual Studio Solution and changed Active solution platform from "Any CPU" to "x64". After rebuilding my program now works and no longer throws the exception.

Share:
25,306
Prometheus
Author by

Prometheus

Software Engineer

Updated on July 20, 2022

Comments

  • Prometheus
    Prometheus almost 2 years

    For a couple of months i had no issue about generating the model from DB by deleting it and recreating it . After a pull from git, an issue has been occurred while trying to make the same process . After the second step (connection string creation with DB) there is no further proceed at the 3rd step and no connection string with the data base is being created at the app.config file.I have tried to test the connection with the database credentials and i am getting the following .

    connection test failed

    When i try to update specific tables from the model diagram as an alternative i get also the below :

    System.Data.Entity.Core.EntityException: An error occurred while closing the provider connection. See the inner exception for details. ---> System.IO.FileNotFoundException: Could not load file or assembly 'System.Memory, Version=4.0.1.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies.

    I have reinstalled Entity Framework and npgsql packages and tried to add all (the same) assemblies but with no success . Similar answers at Stack did not solve my issue . (I am allowed to work with the current versions with no further updates at VS or any of its packages.)

    !Notice : i get all the appropriate data from my services when i use the API calls with the current model (proper communication with DB), but i cannot generate a new model from DB .

    Any solutions ?

    I am using

    • Windows 10

    • VS 2015

    • EntityFrameWork 6.2.0

    • Npgsql 3.1.1

    • .Net v.4.6.2

    • Asp.net

      Thanks in advance !

  • FranzHuber23
    FranzHuber23 about 5 years
    We still do not know which kind of project the OP has. This might be useful depending on the project type (E.g. UWP, ASP.Net, ...)
  • Prometheus
    Prometheus about 5 years
    My mistake , i have seen the above solution which does not fit for me since i am using ASP.Net .