Visual Studio 2017 - Could not load file or assembly 'System.Runtime, Version=4.1.0.0' or one of its dependencies

前端 未结 25 2416
轻奢々
轻奢々 2020-11-28 22:52

I am using Visual Studio 2017 and am trying to create a .Net Standard 1.5 library and use it in a .Net 4.6.2 nUnit test project.

I am getting the following error...

相关标签:
25条回答
  • 2020-11-28 23:29

    I resolved that error by referencing the NetStandard.Library and the following app.config File in the NUnit-Project.

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
    <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
            <dependentAssembly>
                <assemblyIdentity name="System.Runtime" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
                <bindingRedirect oldVersion="0.0.0.0-4.1.1.0" newVersion="4.1.1.0" />
            </dependentAssembly>
            <dependentAssembly>
                <assemblyIdentity name="System.Reflection" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
                <bindingRedirect oldVersion="0.0.0.0-4.1.1.0" newVersion="4.1.1.0" />
            </dependentAssembly>
            <dependentAssembly>
                <assemblyIdentity name="System.Runtime.InteropServices" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
                <bindingRedirect oldVersion="0.0.0.0-4.1.0.0" newVersion="4.1.1.0" />
            </dependentAssembly>
        </assemblyBinding>
    </runtime>
    

    Edit

    If anything other than System.Runtime, System.Reflection or System.Runtime.InteropServices is missing (e.g. System.Linq), then just add a new dependentAssembly node.

    Edit 2

    In new Visual Studio Versions (2017 15.8 I think) it's possible that Studio creates the app.config File. Just check the Auto-generate binding redirects Checkbox in Project-Properties - Application.

    Edit 3

    Auto-generate binding redirects does not work well with .NET Classlibraries. Adding the following lines to the csproj files solves this and a working .config file for the Classlibary will be generated.

    <PropertyGroup>
      <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
      <GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
    </PropertyGroup>
    
    0 讨论(0)
  • 2020-11-28 23:33

    I fixed the problem by removing the Nuget Package System.Runtime and then reinstalling it

    0 讨论(0)
  • 2020-11-28 23:33

    I had a similar problem in VS 2017 15.45 - I found when I checked that even though the project compiled and ran it came up with a system.IO.FileNotFoundException with regard to System.Runtime when I tried to access TPL Dataflow objects.

    When I checked the projects in the solution, one of them (the top one) was missing the System.Runtime package used by the underlying projects. Once I installed it from Nuget it all worked correctly.

    0 讨论(0)
  • 2020-11-28 23:34

    I ended up in this situation several times with my .NET 4.6.1 web site. I created the problem each time when I added a reference to a separate .NET Core project. Upon building, Visual Studio correctly alerted me that such cross-framework references are invalid, and I quickly deleted the project reference. The project built fine after that, but the System.Runtime error appeared when accessing the web site and refused to go away.

    The fix each time was lame but effective: I deleted the project directory and redownloaded it from source control. Even though there was no difference between before and after, I was able to build the project and access the page with no complaints.

    0 讨论(0)
  • 2020-11-28 23:36

    Ran into this just now in a Unit Test project after adding MsTest V2 through Nuget. Renaming app.config (so effectively removing it) did the trick for me.

    Having read through all the above posts, I'm still not sure why, sorry!

    0 讨论(0)
  • 2020-11-28 23:38

    I tried all the solutions here, but to no avail. Eventually, I solved it by opening the new csproj file and manually added the following section:

    <Reference Include="System.Runtime, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
    <HintPath>..\packages\System.Runtime.4.3.0\lib\net462\System.Runtime.dll</HintPath>
    </Reference>
    
    0 讨论(0)
提交回复
热议问题