hib*_*tor 6 c# unity-container unity-interception
我有一个实现两个接口的类,我想对类的方法应用拦截.
我遵循Unity中的建议将两个接口作为一个单独的接口,但我对结果感到惊讶.简而言之,似乎我的CallHandler被调用了两次.我有一个最简单的例子是:
public interface I1
{
void Method1();
}
public interface I2
{
void Method2();
}
public class C : I1, I2
{
[Log]
public void Method1() {}
public void Method2() {}
}
public class LogAttribute : HandlerAttribute
{
public override ICallHandler CreateHandler(IUnityContainer container)
{
return new LogCallHandler();
}
}
public class LogCallHandler : ICallHandler
{
public IMethodReturn Invoke(
IMethodInvocation input, GetNextHandlerDelegate getNext)
{
Console.WriteLine("Entering " + input.MethodBase.Name);
var methodReturn = getNext().Invoke(input, getNext);
Console.WriteLine("Leaving " + input.MethodBase.Name);
return methodReturn;
}
public int Order { get; set; }
}
void Test()
{
IUnityContainer container = new UnityContainer();
container.AddNewExtension<Interception>();
container.RegisterType<C>(new ContainerControlledLifetimeManager());
container.RegisterType<I1, C>(
new Interceptor<TransparentProxyInterceptor>(),
new InterceptionBehavior<PolicyInjectionBehavior>());
container.RegisterType<I2, C>(
new Interceptor<TransparentProxyInterceptor>(),
new InterceptionBehavior<PolicyInjectionBehavior>());
container.Resolve<I1>().Method1();
}
Run Code Online (Sandbox Code Playgroud)
这给出了这个输出:
Entering Method1
Entering Method1
Leaving Method1
Leaving Method1
Run Code Online (Sandbox Code Playgroud)
删除"container.RegisterType I2,C"行使日志只出现一次.添加第三个接口I3(类似于I2)会导致日志出现三次.
我原本希望只调用一次Log.我可以通过让LogCallHandler检测它是否从另一个LogCallHandler调用来实现这一点,但这看起来不太优雅.
最初我想将拦截行为应用于C而不是分别应用于I1和I2,但这需要C继承MarshalByRefObject,这是我不愿意强加的约束.
还有另一种方法吗?
问题是您正在将透明代理应用于每个接口.相反,如果将它应用于具体类,则只能获得一个代理.此外,除非您希望共享实例,否则不需要将其设为单例.
我在测试控制台项目中运行了此配置并获得了所需的结果.感谢您提供隔离问题的工作代码段!
var container = new UnityContainer()
.AddNewExtension<Interception>()
.RegisterType<I1, C>()
.RegisterType<I2, C>()
.RegisterType<C>(
new ContainerControlledLifetimeManager(),
new Interceptor<TransparentProxyInterceptor>(),
new InterceptionBehavior<PolicyInjectionBehavior>()
);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3259 次 |
| 最近记录: |