How do I use binding redirects on a .NET COM DLL with an unmanaged client?

Viewed 728

Here’s the architecture. I have a communications library (using Kestrel and Websockets) in a C# class library DLL. Let’s call it “Communication.dll”. I also have a C# class library (“ComObject.dll”) that is a COM object that wraps “Communication.dll”.

Finally, I have a (very) legacy C++ app that uses the “ComObject.dll” through COM. We’ll call this “Client.exe”.

The problem I’m running into is that there are a lot of components involved in getting all of this to work, and those components require different versions of their dependencies.

Yes, I know. That’s what binding redirects were designed to solve. However, I can’t seem to get that to work in this scenario. Normally, the auto-generated binding redirects should fix this for you, but again, that doesn’t seem to work in this scenario.

So my question is how you’re supposed to specify the config file when the .exe is not managed.

The library “Communication.dll” has an app.config file in the project, but I don’t think that does anything. Editing it doesn’t seem to make a difference. When the auto-generated binding redirects are turned on, .config files are generated for both DLLs. However, I don’t think they’re actually being used. If I edit those config file before running, the edits don’t seem to make a difference. If I don’t have the exact version that’s being called for, I get the FileNotFoundException.

For example, somewhere in the tangle of assemblies, someone requires Microsoft.AspNetCore.Mvc.Core version 2.2.0.0. If I install 2.2.5.0, things will build, but I will get a FileNotFoundException when code tries to use it. I need to add the following binding redirect (inside a properly formatted config file:

  <dependentAssembly>
    <assemblyIdentity name="Microsoft.AspNetCore.Mvc.Core" publicKeyToken="adb9793829ddae60" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-2.2.5.0" newVersion="2.2.5.0" />
  </dependentAssembly>

As I said above, the .exe is not managed, so there’s no app.config in the project, and adding a “Client.exe.config” to the output directory doesn’t seem like a likely solution.

I can solve the above by just downgrading to 2.2.0.0. No big deal.

However, somewhere in the jumble of components, someone requires System.Buffers 4.0.3.0. Someone else requires 4.5.0.0, and someone else requires 4.4.0.0. If I try to downgrade, everything throws a fit (lots of detected downgrade errors). If I try to install 4.5.0.0, no binding redirect anywhere seems to avoid the FileNotFoundException.

If I do a bunch of searching, I find that a lot of people specifically have trouble with System.Buffers (for example, https://github.com/dotnet/corefx/issues/33148), but I find no solution. Actually, one place just said “update all of your components and it will magically start working”. Tried that and it didn’t.

0 Answers
Related