3

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?

tshepang
  • 12,111
  • 21
  • 91
  • 136
Anthony Mastrean
  • 21,850
  • 21
  • 110
  • 188

3 Answers3

2

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'" />
Anthony Mastrean
  • 21,850
  • 21
  • 110
  • 188
gregsaab
  • 102
  • 8
2

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.

David Tchepak
  • 9,826
  • 2
  • 56
  • 68
2

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();
  • This article helped me more: http://www.davidarno.org/c-howtos/aliases-overcoming-name-conflicts-part-2-extern-alias/ – James Nail Nov 20 '14 at 21:24