How to fix “type exists in both assemblies” failure when using DynamicProxy types in an assembly referencing NSubstitute?

StackOverflow https://stackoverflow.com/questions/12266279

Question

I have an application that uses DynamicProxy 3.1 to do runtime interception. I have a test assembly that uses NSubstitute for mocking. I just wrote some "integration" tests against my fully bootstrapped container (StructureMap using InterceptWith to do the interception) so that I can assert that certain types coming out of the container are proxied properly.

[Subject(typeof(RobotBoard))]
public class When_resolving_an_intercepted_type : WithContainer<IRobotBoard>
{
    It should_have_recovery = () => Subject.ShouldHaveInterceptor<RecoveryInterceptor>();
}

public static class TestExtensions
{
    public static void ShouldHaveInterceptor<T>(this object obj)
        where T : IInterceptor
    {
        ((IProxyTargetAccessor)obj)
            .GetInterceptors()
            .ToList()
            .Exists(x => x is T)
            .ShouldBeTrue();
    }
}

However, I get this error, indicating that DynamicProxy references are inside the NSubstitute assembly, too! (it appears to be ilmerged).

Error    11    MyCompany.MyModule.Specifications    D:\code\source\tests\When_resolving_an_intercepted_type.cs
The type 'Castle.DynamicProxy.IProxyTargetAccessor' exists in both 'd:\code\packages\Castle.Core.3.1.0\lib\net40-client\Castle.Core.dll' and 'd:\code\packages\NSubstitute.1.4.2.0\lib\NET40\NSubstitute.dll'

Is there anyway around this conflict?

Était-ce utile?

La solution

You could grab the NSubstitute source code and remove the ilmerge commands from the project's targets. I've opened issue 86 on their repository to address this.

<exec command="&quot;$(MSBuildProjectDirectory)\..\..\ThirdParty\Ilmerge\ILMerge.exe&quot; /internalize:&quot;$(MSBuildProjectDirectory)\ilmerge.exclude&quot; /keyfile:$(AssemblyOriginatorKeyFile) /out:@(MainAssembly)  &quot;@(IntermediateAssembly)&quot; @(AssembliesToMerge->'&quot;%(FullPath)&quot;', ' ')" Condition=" '$(TargetFrameworkVersion)' == 'v3.5'" />
<exec command="&quot;$(MSBuildProjectDirectory)\..\..\ThirdParty\Ilmerge\ILMerge.exe&quot; /internalize:&quot;$(MSBuildProjectDirectory)\ilmerge.exclude&quot; /keyfile:$(AssemblyOriginatorKeyFile) /out:@(MainAssembly) /targetplatform:&quot;v4,$(FrameworkReferenceAssemblyPath).&quot; &quot;@(IntermediateAssembly)&quot; @(AssembliesToMerge->'&quot;%(FullPath)&quot;', ' ')" Condition=" '$(TargetFrameworkVersion)' == 'v4.0'" />

Autres conseils

You could try using an alias to reference the NSubstitute or DynamicProxy assemblies.

See MSDN How to: Use the Global Namespace Alias (C# Programming Guide) for more info.

You can use the 'extern alias' directive as explained here: http://blogs.msdn.com/b/ansonh/archive/2006/09/27/774692.aspx

basically

(1) in VS, go to the assembly reference for FooVersion1, and right click > Properties.

(2) change 'aliases' value to 'FooVersion1'

(3) in your .cs file use:

extern alias FooVersion1;
using foo = FooVersion1::FooVersion1;
...
var something = foo.FooClass();
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top