1

現在、Castle DynamicProxy を使用してインターセプターを実装しています。サービス層メソッドでいくつかのカスタム属性を取得するためにインターセプターが必要ですが、invocation.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);

アップデート:

構成の問題である可能性があります。構成コードは次のとおりです。

インターセプターインストーラー:

    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"));
    }

サービスインストーラー:

    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 を使用しています。

ありがとう、

ポール

4

2 に答える 2

5

プロキシがインターフェイス プロキシであるためであることが判明しました。メソッド呼び出しのターゲットを取得し、次に methodInfo から属性を取得すると修正されました。

    MethodInfo methodInfo = invocation.MethodInvocationTarget; 
    if (methodInfo == null) { 
        methodInfo = invocation.Method; 
    }
于 2011-06-08T13:18:10.250 に答える
1

インターセプター コードは問題ありませんが、登録が間違っています。あなたが書いたのは、「私があなたに求めるなら、私にください」という意味IInterceptorですSecurityInterceptor。「 を含むクラスへの呼び出しをインターセプトするLoginUser()( と呼びましょうFoo) 」と言いたいのですSecurityInterceptor。C# に翻訳すると、次のようになります。

container.Register(Component.For<Foo>().Interceptors<SecurityInterceptor>());
container.Register(Component.For<SecurityInterceptor>().Named("SecurityInterceptor"));
于 2011-06-04T17:36:35.067 に答える