References of References with COM caller [duplicate]
Asked Answered
T

1

13

I have a scenario like this:

  1. COM Call from an MS Access Application using VBA
  2. Call is into a C# library we are building which has a reference to Newtonsoft.Json v9
  3. Library uses nuget packages with their own dependency of Newtonsoft.Json v6 (Say, Microsoft.AspNet.WebApi.Client)
  4. Dependent library within it's own blackbox code blow up when they try to use Newtonsoft.Json (or any library that isn't the same version that our C# library is referencing)

Error:

Could not load file or assembly 'Newtonsoft.Json, Version=6.0.4.0, Culture=neutral, 
PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. 
The system cannot find the file specified.

I expect I need something like a dependentAssembly redirect binding somewhere like what you using in .Net to indicate how to resolve these types of things, but I haven't found anything through my searching that tells me where that might need to go for the referenced assembly in COM to pick it up.

It's worth noting that in the case of Newtonsoft.Json, downgrading to 6.0.4 isn't a fix because there are multiple references to Json.Net within the various packages we are using so I don't have the power to just choose one version.

Also, in a .Net tester application we used during library development everything works as expected and the references figure themselves out to use the right versions. So I believe I need to figure out how to tell COM to find what it is looking for.

Any help is appreciated! Thanks.

Tarver answered 9/8, 2016 at 13:25 Comment(6)
If there is anything I can do to clarify the question, let me know.Tarver
https://mcmap.net/q/909292/-add-managed-dll-dependencied-to-unmanaged-c-projectNumismatist
@HansPassant Thanks for the linkTarver
copy that assembly to your bin folder and reference it. or allow "copy to local" in your reference properties.Kaitlin
@krishKM They are all put there and set to copy local. The challenge is that when going through the COM type library the referenced versions inside the references are not respecting the dependent assembly configuration and are looking for different versions of the json dll as a result.Tarver
So it's not really an answer to the question when multiple versions if you don't have control to change all of the dependencies, but to get around this I had to downgrade all of the NuGet libraries to versions that were using the same version of Newtonsoft.Json. Not loving using Json 4.5 when 9 is out, but it worked at least! Also, this is my first bounty so I'm not sure what happens if it expires and I don't have a question to select.Tarver
L
0

One workaround I found for this kind of problem is to include a config file in the same path of the executable who call the c# code.

I discovered that, if I create a VB6 application called "myapp.exe", I could create, in the same folder of "myapp.exe", a text file called "myapp.exe.config", with the following contents:

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

In your case, I believe the exe name would be "msaccess.exe" or something like that.

Lambrequin answered 20/9, 2016 at 16:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.