Adding a bindingRedirect to a .Net Standard library
Asked Answered
D

1

52

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?

Dandruff answered 8/9, 2017 at 8:14 Comment(1)
Possible duplicate of How can I add an assembly binding redirect to a .net core unit test project?Hannahannah
M
68

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>
Marcmarcano answered 8/9, 2017 at 16:27 Comment(10)
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 <PackageReference> 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.Foresheet
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.Rationalize
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.configShoveler
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.configMarcmarcano
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 & NuGetBaldhead
For WCF project I had to copy bindingRedirect from output config file to solution web.config to run project locally in IIS express.Elconin
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.Aubrette
I second the question above, how do I use or modify .deps.json?Bowers
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 uselessWaxplant
<GenerateBindingRedirectsOutputType> helped me generate binding redirects for MSTest projects that were missing, thanks!Balky

© 2022 - 2024 — McMap. All rights reserved.