How to resolve Castle.Windsor and MoQ version conf

2019-07-23 07:31发布

问题:

In my project I need to use simultaneously Castle.Windsor and Moq dlls. Windsor requires Castle.Core also to be referenced in the project.

Problem starts when I try to use methods from Castle.Core: Castle.DynamicProxy.Generators.AttributesToAvoidReplicating.Add(...);

Problem1: If I use Moq.dll from NET40 folder, I got built error "The type 'Castle.DynamicProxy.Generators.AttributesToAvoidReplicating' exists in both '...\Windsor\dotNet40\Castle.Core.dll' and '...\MoQ\NET40\Moq.dll'"

Problem2: If I use Moq.dll from "NET40-RequiresCastle" folder, which is logically in my situation, I got versions conflict - Moq.dll uses Castle.Core, Version=2.5.0.0, but Windsor uses Castle.Core, Version=2.5.1.0

回答1:

Problem can be solved using assembly binding - App.config:

<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
  <dependentAssembly>
    <assemblyIdentity name="Castle.Core" publicKeyToken="407dd0808d44fbdc" />
    <bindingRedirect oldVersion="1.0.0.0-2.5.0.0" newVersion="2.5.1.0" />
  </dependentAssembly>
</assemblyBinding>