50

I have a .Net Standard library, and I'm getting an error when trying to use one of the dependant libraries, which I believe is down to a version conflict. In an old style .Net Class library, I might add something like this:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-10.0.0.0" newVersion="10.0.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

But, I obviously can't do that in a Net Standard library; so, my question is, what is the strategy for addressing such issues in the .Net Standard world?

Paul Michaels
  • 16,185
  • 43
  • 146
  • 269
  • Possible duplicate of [How can I add an assembly binding redirect to a .net core unit test project?](https://stackoverflow.com/questions/39252136/how-can-i-add-an-assembly-binding-redirect-to-a-net-core-unit-test-project) – Michael Freidgeim May 07 '18 at 09:34

1 Answers1

63

Binding redirects are a .NET framework concept, there are no binding redirects on .NET Standard and .NET Core.

However, an application (the actual .NET Framework or .NET Core application) need to resolve the files to be used. On .NET Core, this is done by generating a deps.json file based on the build input and a .NET Framework application uses binding redirects.

If a binding redirect is necessary, they have to be added to the .NET Framework application (or library) that used the .NET Standard library.

These binding redirects can be configured to be automatically generated during build, based on the assemblies used during compilation, see the documentation on automatic binding redirects. When using NuGet's new PackageReference style of using NuGet packages, this is done automatically. Since configuring this correctly varies based on the project type, refer to the announcement Issues with .NET Standard 2.0 with .NET Framework & NuGet for detailed descriptions.

The simplest way to make sure that the correct binding redirects are used is to ensure the .NET Framework app or library sets these properties (inside the csproj/vbproj. The second one is not needed for projects that generate .exe executables but needed for unit test projects):

<PropertyGroup>
    <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
    <GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
</PropertyGroup>
Martin Ullrich
  • 94,744
  • 25
  • 252
  • 217
  • 2
    Applying these properties to my .Net Framework library (not exe) did not cause the binding redirects to be added. I have a .Net Framework class library using `` entries. The project has an app.config. The binding redirects are still not being automatically added despite the build complaining about the fact that I should be adding them. I can add them manually, but it would be nice to have this automatically happen. – Ryan Griffith Jan 18 '19 at 20:13
  • Hi @RyanGriffith, Just sharing some insights if it is of some help. I was struggling with this problem for a while. The 2 settings did the trick for me. But, unlike .NET Framework project, where the app.config was modified in situ with new binding redirect elements, I had to build the project and the ?.dll.config had all the entries. Worked well for both .NET Std and Fwk class libraries. Good luck. – Sau001 Apr 07 '19 at 23:25
  • Please link to the documentation for `PackageReference` generating these redirects automatically. It does not work for me, either in the `src` or in the `bin` versions of the `app.config` – Milton Sep 25 '19 at 21:12
  • 1
    PackageReference doesn't really have much to do with these redirects. with these nuget just doesn't touch app.config anymore and msbuild performs redirect generation and adds them to the generated TheApp.config in the output. Not sure how this interacts if a redirect is already present in app.config – Martin Ullrich Sep 26 '19 at 12:29
  • 1
    Make sure your root application project doesn't use `packages.config` but uses `PackageReference` for NuGet packages. [Issues with .NET Standard 2.0 with .NET Framework & NuGet](https://github.com/dotnet/announcements/issues/31) – Oleg Grishko Oct 24 '19 at 16:20
  • 1
    For WCF project I had to copy bindingRedirect from output config file to solution web.config to run project locally in IIS express. – Tomasz Rzepecki Jun 19 '20 at 09:54
  • 43
    Am I missing something or does this answer just describe how to use binding redirects in .NET Framework, while it does not actually explain what equivalent features can be used in .NET Core? The answer implies I need to do *something* with the `.deps.json` files, but I am still entirely in the dark on what that *something* might be. – O. R. Mapper Apr 20 '21 at 21:00
  • 3
    I second the question above, how do I use or modify .deps.json? – Igor Be Dec 01 '21 at 08:41
  • 2
    I have my .deps.json properly created by my build, but then executable fails to load the proper dependency with this error: `Could not load file or assembly... The located assembly's manifest definition does not match the assembly reference. (0x80131040)`. I need to run my app with a different version of a dependency, but modifying the deps.json simply seems useless – Andrea Castello Dec 02 '21 at 15:48
  • helped me generate binding redirects for MSTest projects that were missing, thanks! – ManIkWeet Feb 10 '23 at 07:32