我在拦截注册到类实例的组件时遇到问题。
//this does not get intercepted
container.Register(Component.For<IService>().Instance(instanceService)
.Interceptors<Interceptor>());
如果我在不使用类实例的情况下注册组件,拦截器就可以工作
//this get intercepted
container.Register(Component.For<IService>().ImplementedBy<SampleService>()
.Interceptors<Interceptor>());
这是一个错误还是设计使然?
谢谢
这是单元测试代码。
[Test]
public void Test_Windsor_Interceptor_With_Instance_Component_Registration()
{
IService instanceService = new SampleService();
var container = new WindsorContainer();
container.Register(Component.For<Interceptor>());
//this get intercepted
container.Register(Component.For<IService>().ImplementedBy<SampleService>()
.Interceptors<Interceptor>());
////this does not get intercepted
//container.Register(Component.For<IService>().Instance(instanceService)
// .Interceptors<Interceptor>());
var proxiedService = container.Resolve<IService>();
proxiedService.DoSomething();
}
public class Interceptor : Castle.DynamicProxy.IInterceptor
{
public void Intercept(Castle.DynamicProxy.IInvocation invocation)
{
throw new System.NotImplementedException("Interceptor succesfully called but not implemented");
}
}
public interface IService
{
void DoSomething();
}
public class SampleService : IService
{
public void DoSomething()
{
string dummy = string.Empty;
}
}