让我先展示一下正在运行的场景:
我有一个 HandlerAttribute,它使用 InterfaceInterceptor 与 AOP 一起使用,我这样注册:
public void RegisterType<TInterface, TClass>()
where TClass : TInterface
{
// My UnityContainer
this.container.RegisterType<TInterface, TClass>(new ContainerControlledLifetimeManager())
.Configure<Interception>()
.SetDefaultInterceptorFor<TInterface>(new InterfaceInterceptor());
}
具有HandlerAttribute的接口(interface):
public interface IService<TId, TEntity>
where TId : IEquatable<TId>
where TEntity: AbstractEntity<TId> // Class that has a Id
{
[DatabaseTransaction] // The HandlerAttribute
void Update(TEntity entity);
}
因此,我创建了实现 IService 的 MyService,并使用 RegisterType 方法进行注册。因此,之后我从传递 IService 的 myContainer 调用 Resolve 方法,并通过 DatabaseTransactionAttribute 拦截的方法 Update 使用该服务:
public class MyService : IService<int, MyEntity>
{
}
之后:
RegisterType<IService, MyService>()
一切正常,直到我创建实现 IService 的接口(interface) IMyService 和实现 IMyService 的 MyService。我执行相同的步骤来注册并解析上面提到的 IMyService 接口(interface)...
public class IMyService : IService<int, MyEntity>
{
}
public class MyService : IMyService
{
}
之后:
RegisterType<IMyService, MyService>()
问题是,当我在第二种情况下调用 Update 方法时,我的 DatabaseTransactionAttribute 不再拦截该方法,有人知道为什么吗?
我发现这个问题在某种程度上是相关的:Can a C# class inherit attributes from its interface?
那么,我必须在实现具有该属性的接口(interface)的接口(interface)中重新声明吗?哦
拦截器只有在属性位于统一容器中注册的接口(interface)中时才起作用?
最佳答案
据我所知:
The interceptor only works if the attribute is in the interface that is registered in the unity container?
是的
So, i have to re-declare the attribute in the interface that
implementsextends the interface that has the attribute?
是的
关于c# - 当我实现使用 HandlerAttribute 的接口(interface)时,是否可以拦截它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8992296/