0

I have a pretty complicated case but I'll try to explain it. I have a project structured like following Project structure Class libraries "B" and "C" consume different version of the same NuGet package.

Problem 1: The v1 version of the NuGet package copies it's content to a folder "V1" and v2 - to a "V2" folder. How do I specify the path for class library "B" to use DLLs from "V1" folder and "C" - from "V2" folder? When I try to add usings in "B" and "C" to "V1" and "V2" respectively I get a compiler Error CS0246 "The type or namespace could not be found".

Problem 2: If problem 1 is solved, will class library "A" be able to use "B" and "C" without any additional changes? Or is it needed to add App.config file and specify attributes there or make any changes in .csproj file?

Note that Base class library is packed into NuGet package, so I don't have any Main application, I need to solve these problems on class library level.

I tried creating App.config file in both "B" and "C" with following content:

<configuration>
    <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
            <probing privatePath="<V1 for "B" and V2 for "C")>" />
        </assemblyBinding>
    </runtime>
</configuration>

But it doesn't seem to help. Is there any way to specify path in .csproj file?

Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
  • You cannot use different version of the same library – Selvin Jan 18 '23 at 10:14
  • You can't mix versions but you can tell .NET to use a specific version when another is required. This is done with a `bindingRedirect` element – Panagiotis Kanavos Jan 18 '23 at 10:15
  • You plain can't do this. Loading libraries is specific to a process, not the libraries inside that process. One process, one assembly. If both package versions work with the newest version of the library, you can use an assembly binding. If not, what you can attempt, even though it's fraught with peril, is have a custom `AppDomain.AssemblyResolve` event to handle this on a per-assembly basis, since that includes information of which assembly is requesting the load. This can still run into trouble if any crossover of data needs to happen, since static data would be duplicated. – Jeroen Mostert Jan 18 '23 at 10:17

0 Answers0