Let's assume you have a project structure as follows:
...where A
and B
are class libraries, and C
is an executable-type project (such as a unit test or console project).
Let's assume the folder structure is like this:
ABC.sln
A/A.csproj
A/...
B/B.csproj
B/...
C/C.csproj
C/...
lib/thirdparty4/thirdparty.dll
lib/thirdparty5/thirdparty.dll
If we attempted to naively reference our projects together, we'd have a problem: two versions of thirdparty.dll
will be copied into the same folder (the output (i.e., bin) directory of C
). We need a way for C
to copy both dlls into its output directory, and provide a mechanism for referencing either one.
To solve this, I modified C.csproj
to contain the following:
<ItemGroup>
<Content Include="..libhirdparty4hirdparty.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Link>thirdparty4hirdparty.dll</Link>
</Content>
<Content Include="..libhirdparty5hirdparty.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Link>thirdparty5hirdparty.dll</Link>
</Content>
</ItemGroup>
This will instruct it to create both thirdparty4hirdparty.dll
and thirdparty5hirdparty.dll
in its output directory.
Now, after building C
, its output directory looks like this:
CinDebugA.dll
CinDebugB.dll
CinDebugC.dll
CinDebughirdparty4hirdparty.dll
CinDebughirdparty5hirdparty.dll
To instruct C
to use both of these dlls, I added an App.config
file to it, with the following:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="thirdparty" culture="neutral" publicKeyToken="1234567890123445"/>
<bindingRedirect oldVersion="4.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
<codeBase version="4.0.0.0" href="thirdparty4hirdparty.dll" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="thirdparty" culture="neutral" publicKeyToken="1234567890123445"/>
<bindingRedirect oldVersion="5.0.0.0-5.0.0.0" newVersion="5.0.0.0" />
<codeBase version="5.0.0.0" href="thirdparty5hirdparty.dll" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
This will instruct the assembly to, depending on which version is in need, use one DLL or the other, both of which will be available within subfolders of the output directory. (The bindingRedirect elements are optional, but you can use them if you need a range of revisions for this to apply to.)