Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 当我实现一个使用HandlerAttribute的接口时,它是否可能被拦截?_C#_.net_Attributes_Unity Container_Interceptor - Fatal编程技术网

C# 当我实现一个使用HandlerAttribute的接口时,它是否可能被拦截?

C# 当我实现一个使用HandlerAttribute的接口时,它是否可能被拦截?,c#,.net,attributes,unity-container,interceptor,C#,.net,Attributes,Unity Container,Interceptor,让我首先展示正在运行的场景: 我有一个HandlerAttribute,它使用InterfaceInterceptor与AOP一起工作,我是这样注册的: public void RegisterType<TInterface, TClass>() where TClass : TInterface { // My UnityContainer this.container.RegisterType<TInterface,

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

我有一个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());
}
公共无效注册表类型()
哪里有TClass:TInterface
{   
//我的单位容器
this.container.RegisterType(新的ContainerControlled LifetimeManager())
.Configure()
.setDefaultInterceptor(新的InterfaceInterceptor());
}
具有HandlerAttribute的接口:

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);
}
public class MyService : IService<int, MyEntity>
{
}
公共接口iSeries设备
哪里TId:IEquatable
其中tenty:AbstractEntity//具有Id的类
{
[DatabaseTransaction]//HandlerAttribute
无效更新(潜在实体);
}
因此,我创建了实现IService的MyService,并使用RegisterType方法进行注册。因此,在此之后,我通过iSeries设备从myContainer调用Resolve方法,并使用DatabaseTransactionAttribute截获的方法更新服务:

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);
}
public class MyService : IService<int, MyEntity>
{
}
公共类MyService:IService
{
}
之后:

RegisterType<IService, MyService>()
RegisterType<IMyService, MyService>()
RegisterType()
在我创建接口IMyService(实现iSeries设备)和MyService(实现IMyService)之前,一切正常。我执行相同的步骤来注册和解析上面提到的IMyService接口

public class IMyService : IService<int, MyEntity>
{
}

public class MyService : IMyService
{
}
公共类IMyService:IService
{
}
公共类MyService:IMyService
{
}
之后:

RegisterType<IService, MyService>()
RegisterType<IMyService, MyService>()
RegisterType()
问题是,当我在第二种情况下调用Update方法时,我的DatabaseTransactionAttribute不再拦截该方法,有人知道为什么吗


我发现这个问题在某种程度上是相关的:

因此,我必须在实现具有属性的接口的接口中重新声明?哦

仅当属性位于unity容器中注册的接口中时,拦截器才起作用

AFAIK:

拦截器仅在属性位于 是否在unity容器中注册

因此,我必须在实现的接口中重新声明属性 具有该属性的接口