无法使用 DynamicProxy 在拦截器中检索 CustomAttributes

发布于 2024-11-13 21:21:58 字数 2068 浏览 3 评论 0原文

我目前正在使用 Castle DynamicProxy 实现拦截器。我要求拦截器在我的服务层方法上获取一些自定义属性,但 invoking.Method.GetCustomAttributes 不返回任何内容。我可能做错了什么吗?

拦截方法:

 [Transaction()]
 [SecurityRole(AuthenticationRequired = false, Role = SystemRole.Unauthorised)]
 public virtual void LoginUser(out SystemUser userToLogin, string username)
 {
     ...
 }

拦截器:

// Checks that a security attribute has been defined
foreach (SecurityRoleAttribute role in invocation.Method.GetCustomAttributes(typeof(SecurityRoleAttribute), true))
{
    if (!securityAttributeDefined)
        securityAttributeDefined = true;
}

我也尝试过:

Attribute.GetCustomAttribute(invocation.Method, typeof(SecurityRoleAttribute), true);

更新:

可能是配置问题。配置代码如下:

InterceptorsInstaller:

    public void Install(IWindsorContainer container, IConfigurationStore store)
    {
         container.Register(
            Component.For<LoggingInterceptor>()
            .Named("LoggingInterceptor"));

         container.Register(
            Component.For<SecurityInterceptor>()
            .Named("SecurityInterceptor"));

         container.Register(
            Component.For<ValidationInterceptor>()
            .Named("ValidationInterceptor"));
    }

ServiceInstaller:

    public void Install(IWindsorContainer container, IConfigurationStore store)
    {
        string[] interceptors = {"LoggingInterceptor", "SecurityInterceptor"};

        container.Register(AllTypes.FromAssemblyContaining<BaseService>().Pick()
                            .If(Component.IsInSameNamespaceAs<LoginService>())
                            .Configure(c => c
                                               .LifeStyle.Transient
                                               .Interceptors(interceptors))
                            .WithService.DefaultInterface());
    }

我正在使用 Castle 2.5.2/.Net 3.5。

谢谢,

保罗

I'm currently implementing Interceptors using Castle DynamicProxy. I require the interceptor to pick up some custom attributes on my service layer method, but invocation.Method.GetCustomAttributes returns nothing. Anything I could be doing wrong?

Intercepted Method:

 [Transaction()]
 [SecurityRole(AuthenticationRequired = false, Role = SystemRole.Unauthorised)]
 public virtual void LoginUser(out SystemUser userToLogin, string username)
 {
     ...
 }

Interceptor:

// Checks that a security attribute has been defined
foreach (SecurityRoleAttribute role in invocation.Method.GetCustomAttributes(typeof(SecurityRoleAttribute), true))
{
    if (!securityAttributeDefined)
        securityAttributeDefined = true;
}

I've also tried:

Attribute.GetCustomAttribute(invocation.Method, typeof(SecurityRoleAttribute), true);

Update:

May be a configuration issue. The config code is as follows:

InterceptorsInstaller:

    public void Install(IWindsorContainer container, IConfigurationStore store)
    {
         container.Register(
            Component.For<LoggingInterceptor>()
            .Named("LoggingInterceptor"));

         container.Register(
            Component.For<SecurityInterceptor>()
            .Named("SecurityInterceptor"));

         container.Register(
            Component.For<ValidationInterceptor>()
            .Named("ValidationInterceptor"));
    }

ServiceInstaller:

    public void Install(IWindsorContainer container, IConfigurationStore store)
    {
        string[] interceptors = {"LoggingInterceptor", "SecurityInterceptor"};

        container.Register(AllTypes.FromAssemblyContaining<BaseService>().Pick()
                            .If(Component.IsInSameNamespaceAs<LoginService>())
                            .Configure(c => c
                                               .LifeStyle.Transient
                                               .Interceptors(interceptors))
                            .WithService.DefaultInterface());
    }

I'm Using Castle 2.5.2/.Net 3.5.

Thanks,

Paul

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

誰ツ都不明白 2024-11-20 21:21:58

原来是因为代理是接口代理。获取方法调用目标,然后从 methodInfo 获取属性修复它:

    MethodInfo methodInfo = invocation.MethodInvocationTarget; 
    if (methodInfo == null) { 
        methodInfo = invocation.Method; 
    }

Turns out it was because the proxy is an interface proxy. Getting the method invocation target, then getting the attributes from methodInfo fixed it:

    MethodInfo methodInfo = invocation.MethodInvocationTarget; 
    if (methodInfo == null) { 
        methodInfo = invocation.Method; 
    }
少年亿悲伤 2024-11-20 21:21:58

您的拦截器代码很好,但您注册错误。你写的意思是“如果我向你要 IInterceptor,请给我 SecurityInterceptor”。您想说“使用 SecurityInterceptor 拦截对包含 LoginUser() 的类(我们称之为 Foo)的调用”。翻译成C#,看起来像这样:

container.Register(Component.For<Foo>().Interceptors<SecurityInterceptor>());
container.Register(Component.For<SecurityInterceptor>().Named("SecurityInterceptor"));

Your interceptor code is fine, but you're registering it wrong. What you wrote means “if I ask you for IInterceptor, give me SecurityInterceptor”. You want to say “intercept calls to the class that contains LoginUser() (let's call it Foo) using SecurityInterceptor”. Translated into C#, it looks like this:

container.Register(Component.For<Foo>().Interceptors<SecurityInterceptor>());
container.Register(Component.For<SecurityInterceptor>().Named("SecurityInterceptor"));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文