Missing Config File With Binding Redirects in Pure CPP Client Wrapping .NET Library via CPP/CLI

  .net, app-config, assembly-binding-redirect, c++, c++-cli

The goal is to create a c++ client that can use a managed library via a wrapper so that the client project does not need to be compiled with /clr (due to legacy issues).


CppClientPure (client console .exe)

  • links to:

CppWrapper (static .lib compiled with /clr — CPP/CLI)

  • references:

DevKit (.net class library)

  • references several assemblies

    The DevKit contains a reference to a third-party assembly that looks for Newtonsoft.Json, Version=, that is causing a runtime failure:

      Message=The type initializer for 'Energistics.Etp.v11.Datatypes.MessageHeader' threw an exception.
    Inner Exception 1:
    FileLoadException: Could not load file or assembly 'Newtonsoft.Json, Version=, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
      Message=The type initializer for 'Energistics.Etp.v11.Datatypes.MessageHeader' threw an exception.

Fusion log shows the following:

"Assembly manager loaded from:  C:WindowsMicrosoft.NETFramework64v4.0.30319clr.dll

Running under executable  E:tfssrcBuilder_MainlineSourceLibrariesManagedetp-devkitetp-devkit-mastersrcx64DebugCppClientPure.exern

--- A detailed error log follows.

=== Pre-bind state information ===
LOG: DisplayName = Newtonsoft.Json, Version=, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeedn (Fully-specified)r
LOG: Appbase = file:///E:/tfssrc/Builder_Mainline/Source/Libraries/Managed/etp-devkit/etp-devkit-master/src/x64/Debug/r
LOG: Initial PrivatePath = NULLrnCalling assembly : Avro, Version=, Culture=neutral, PublicKeyToken=b1248d7eeb9d0814.
LOG: This bind starts in default load context.
LOG: No application configuration file found.
LOG: Using host configuration file:
LOG: Using machine configuration file from C:WindowsMicrosoft.NETFramework64v4.0.30319configmachine.config.
LOG: Post-policy reference: Newtonsoft.Json, Version=, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed
LOG: Attempting download of new URL file:///E:/tfssrc/Builder_Mainline/Source/Libraries/Managed/etp-devkit/etp-devkit-master/src/x64/Debug/Newtonsoft.Json.DLL.
WRN: Comparing the assembly name resulted in the mismatch: Major Version
ERR: Failed to complete setup of assembly (hr = 0x80131040). Probing terminated."

I have added a CppClientPure.config file that contains the following binding redirect:

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

It is the only .config file in the output directory but it doesn’t seem like it is getting picked up and therefore, the binding redirects are not applied. According to this post, How do I use binding redirects on a .NET COM DLL with an unmanaged client? what I’m attempting seems like something possible. The solution is targetting .NET Framework 4.5.2 everywhere.

Any ideas are appreciated. Thanks.

Source: Windows Questions C++