Could Not Load File Or Assembly 'System.Runtime, Version=6.0.0.0

Smit Rathod 100 Reputation points
2023-02-22T08:36:53.72+00:00

0

I have below two class libraries.

  • ClassLibrary1 (.NET framework v4.8)
  • ClassLibrary2 (.NET standard v2.0)

I have to give reference of ClassLibrary2 in ClassLibrary1. When I am giving reference of ClassLibrary2 in ClassLibrary1, I am getting runtime error as below :

Could not load file or assembly 'System.Runtime, Version=6.0.0.0,Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. 
The system cannot find the file specified.":"System.Runtime, Version=6.0.0.0,
Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"

When I am trying to add reference of System.Runtime (v6.0.0) in ClassLibrary1, I am getting compile time errors as below.

The type 'xyztype' found in both 'mscorelib' and 'System.Runtime v6.0.0'

What i have tried so far :

  • Added NETStandard.Lib (netstandard) NUGET
  • Added <Reference Include="netstandard"><Private>True</Private></Reference> in ClassLibrary1.csproj
  • Added System.Runtime package in ClassLibrary1

So how can i get out from this?

How to use ClassLibrary2 in ClassLibrary1 ?

Developer technologies .NET Other
Developer technologies C#
0 comments No comments
{count} votes

Accepted answer
  1. Alan Farias 755 Reputation points
    2023-02-24T23:11:39.53+00:00

    To use ClassLibrary2 in ClassLibrary1, you need to make sure that both class libraries target the same .NET framework version. You can either upgrade ClassLibrary1 to .NET framework v4.6.1 or above, or downgrade ClassLibrary2 to .NET standard v1.6 or below, which supports .NET framework v4.5.2 and above.

    There are a few other ways to resolve this issue:

    1. Use a different version of .NET Standard: If you can, try using a different version of .NET Standard in your ClassLibrary2 project. This may resolve the conflict with System.Runtime and allow you to reference the project in ClassLibrary1 without issues.
    2. Use a different version of .NET Framework: If possible, try targeting a different version of .NET Framework in your ClassLibrary1 project. This may allow you to reference ClassLibrary2 without running into runtime errors.
    3. Use assembly binding redirects: You can use assembly binding redirects in your app.config or web.config file to redirect references to a specific version of System.Runtime to another version that is compatible with your project. This will allow you to reference both ClassLibrary1 and ClassLibrary2 without errors.
    4. Use multi-targeting: If you're using Visual Studio 2019 or later, you can use multi-targeting to target both .NET Framework and .NET Standard in the same project. This will allow you to reference ClassLibrary2 directly in ClassLibrary1 without any conflicts.

    To "Use assembly binding redirects", you need to add an app.config file to your application or library project. In this file, you can specify the redirection rules for each assembly that needs to be redirected. Here is an example of what the app.config file might look like:

    <?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-6.0.0.0" newVersion="4.0.0.0" />
          </dependentAssembly>
        </assemblyBinding>
      </runtime>
    </configuration>
    

    Or, to "Use multi-targeting", in Visual Studio, follow these steps:

    1. Right-click on the project and select Properties.
    2. In the Properties window, select the Application tab.
    3. In the Target Framework dropdown, click on the "..." button.
    4. In the "Target Frameworks" window, select the frameworks you want to target. You can select multiple frameworks by holding down the Ctrl key while clicking.
    5. Click OK to close the "Target Frameworks" window.
    6. Save the changes to the project properties.

    After following these steps, you will be able to write code that targets multiple frameworks in the same project. Note that when you build your project, Visual Studio will create a separate build output for each targeted framework.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.