c# - 当我实现使用 HandlerAttribute 的接口(interface)时,是否可以拦截它?

标签 c# .net attributes unity-container interceptor

让我先展示一下正在运行的场景:

我有一个 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/

相关文章:

javascript - 无法使用 jquery 获取 html5 数据属性

c# - RollingFileAppender 中的先前文件名

c# - 当我用计时器更新它时, ListView 会闪烁

c# - UdpClient 发送 接收

c# - 如何阻止异步 tcp .NET 代码用完整个系统的资源

asp.net-mvc - FilterAttribute 顺序不起作用

generics - 是否可以使用 Windows 7 访问 GATT(通用属性配置文件)蓝牙设备?

c# - 通过 Internet 的 SQL 连接 : good practice?

c# - 在没有 PostBack 的情况下更改标签文本(使用更新面板)

c# - 如何截断 .NET 字符串?