Unity Interception, Interceptor is called several times with partial classes mapping
I've been looking around for a fix on this for a couple of days with no luck.
Basically we are using Unity for two things: dependency injection and more importantly for Interception.
What I want is that the interceptor is triggered each time a method inside the partial class is called, but I am seeing that the Interceptor is called several times depending on the number of Mappings I create on the web.config, meaning 2 mappings, 2 interceptions for each method called.
In the code samples below all partial classes have the same name but implement different interfaces and they all reside in different .cs files, this is because this library will be moved to WCF in the short term. So we have several partial classes that look like this:
public partial class PartialClass : IInterface1
{
...
}
public partial class PartialClass : IIn开发者_运维技巧terface2
{
...
}
And the configuration file like this:
<alias alias="IInterface1" type="MyProject.Interface.IInterface1, MyProjectAssembly" />
<alias alias="IInterface2" type="MyProject.Interface.IInterface1, MyProjectAssembly" />
<alias alias="PartialClass" type="MyProject.Services.PartialClass , MyProjectAssembly" />
<alias alias="Interceptor" type="MyProject.Services.Interceptor, MyProjectAssembly" />
<container>
extension type="Interception" />
<register type="IInterface1" mapTo="PartialClass">
<lifetime type="ContainerControlledLifetimeManager" />
<interceptor type="InterfaceInterceptor"/>
<interceptionBehavior type="Interceptor" />
</register>
register type="IInterface2" mapTo="PartialClass">
<lifetime type="ContainerControlledLifetimeManager" />
<interceptor type="InterfaceInterceptor"/>
<interceptionBehavior type="Interceptor" />
</register>
</container>
Finally the constructor where an instance of either partial class is needed
public class MyClass()
{
public MyClass(IInterface1 interface)
{
...
}
public MyClass()
:this(Microsoft.Practices.Unity.UnityContainerExtensions.Resolve<IInterface1>(Container))
{
}
}
The problem I am having is that the interceptor is being called TWO times per request, which means that if I add more mappings (Interface3, Interface4, etc) it will be called 3 or 4 times depending on the number of mappings I add.
If you use the isDefaultForType element on one of the registrations it will apply to all registrations of the type. This will prevent the duplicate calls to handlers. For example
<register type="IInterface1" mapTo="PartialClass">
<lifetime type="ContainerControlledLifetimeManager" />
<interceptor type="InterfaceInterceptor" isDefaultForType="True"/>
<interceptionBehavior type="Interceptor" isDefaultForType="True"/>
</register>
<register type="IInterface2" mapTo="PartialClass">
<lifetime type="ContainerControlledLifetimeManager" />
</register>
精彩评论