Referencing multiple versions of the same assembly

2019-06-09 06:50发布

问题:

Is it possible to have multiple versions of the same strongly named assembly loaded in the same process? There is plenty of similar questions, but non of the answers seem to work.

The reason I'm asking is we need to use newer version (4.0.0.0) of SomeAssembly.dll, but some referenced dlls depend on the older version (3.0.0.0) of this dll.

I have tried using the below assembly binding redirection configuration, but ended up with compilation errors, because my assemblies depend on a newer version of the dll and this configuration is completely overriding the linkage and only version 3.0.0.0 is being used. I would expect this to fail at run-time. Is compiler using the config files here?

Version 4.0.0.0 is referenced in project as per usual. Version 3.0.0.0 is copied to the project output.

  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="SomeAssembly" publicKeyToken="123" culture="neutral" />
        <bindingRedirect oldVersion="3.0.0.0" newVersion="3.0.0.0" />
        <codeBase version="4.0.0.0" href="bin/SomeAssembly.dll" />
        <codeBase version="3.0.0.0" href="SomeAssembly.dll" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>

回答1:

Try removing line

<bindingRedirect oldVersion="3.0.0.0" newVersion="3.0.0.0" />

and put assemblies in separate folder inside BIN folder. e.g.

<dependentAssembly>
    <assemblyIdentity name="SomeAssembly" publicKeyToken="123" culture="neutral" />
    <codeBase version="4.0.0.0" href="V4\SomeAssembly.dll" />
    <codeBase version="3.0.0.0" href="V3\SomeAssembly.dll" />
  </dependentAssembly>