Ninject拦截属性与参数传递给拦截器?

时间:2013-08-19 21:30:18

标签: c# dependency-injection ninject ninject-interception

我目前使用以下代码进行拦截工作(非常简单):

(见底部问题)

我的拦截器:

public interface IAuthorizationInterceptor : IInterceptor { }
public class AuthorizationInterceptor : IAuthorizationInterceptor
{


    public IParameter[] AttributeParameters { get; private set; }


    // This doesnt work currently... paramters has no values
    public AuthorizationInterceptor(IParameter[] parameters) {
        AttributeParameters = parameters;
    }

    public void Intercept(IInvocation invocation) {
        // I have also tried to get the attributes like this
        // which also returns nothing.
        var attr = invocation.Request.Method.GetCustomAttributes(true);


        try {
            BeforeInvoke(invocation);
        } catch (AccessViolationException ex) {

        } catch (Exception ex) {
            throw;
        }
        // Continue method and/or processing additional attributes
        invocation.Proceed();
        AfterInvoke(invocation);
    }


    protected void BeforeInvoke(IInvocation invocation) {

        // Enumerate parameters of method call
        foreach (var arg in invocation.Request.Arguments) {
            // Just a test to see if I can get arguments
        }

        //TODO: Replace with call to auth system code.
        bool isAuthorized = true;

        if (isAuthorized == true) {
            // Do stuff               
        }
        else {
            throw new AccessViolationException("Failed");
        }             
    }

    protected  void AfterInvoke(IInvocation invocation) {


    }
}

我的属性:

public class AuthorizeAttribute : InterceptAttribute
{
    public string[] AttributeParameters { get; private set; }

    public AuthorizeAttribute(params string[] parameters) {
        AttributeParameters = parameters;
    }

    public override IInterceptor CreateInterceptor(IProxyRequest request) {
        var param = new List<Parameter>();
        foreach(string p in AttributeParameters) {
            param.Add( new Parameter(p, p, false));
        }
        // Here I have tried passing ConstructorArgument(s) but the result
        // in the inteceptor constructor is the same. 
        return request.Context.Kernel.Get<IAuthorizationInterceptor>(param.ToArray());
    }
}

应用于方法:

[Authorize("test")]
public virtual Result<Vault> Vault(DateTime date, bool LiveMode = true, int? SnapshotId = null)
{
        ...
}

这很有效,我可以通过这个属性传递其他参数:

[Authorize("test")]

如果你在我的属性中注意到我从属性中抓取了一些参数,我可以在属性类中访问它,但是我无法将它们传递给Interceptor。我已经尝试在Kernel.Get&lt;&gt;()调用中使用ConstructorArgument,它不会引发错误,但AuthorizationInterceptor构造函数不会从ninject获取任何值。我也尝试过GetCustomAttributes(),你可以在代码示例中看到,但这也没有返回任何内容。通过查看其他类似的帖子(Ninject Interception 3.0 Interface proxy by method attributes),这似乎是正确的方法,但它不起作用。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

通过在拦截器上创建初始化方法,我得到了一些工作。我真的不喜欢它,因为它将我与AuthorizationInterceptor的具体实现联系起来,但它完成了工作(该死的最后期限大声笑)。我仍然想知道是否有更好的方法来做到这一点,所以我不会标记我自己的答案,希望有人能有更好的方法来做到这一点。

我修改了属性如下:

    public override IInterceptor CreateInterceptor(IProxyRequest request) {
        AuthorizationInterceptor attr = (AuthorizationInterceptor)request.Context.Kernel.Get<IAuthorizationInterceptor>();
        attr.Init(AttributeParameters);
        return attr;
    }

并在拦截器上创建了一个Init方法:

    public void Init(params string[] parameters) {
        AttributeParameters = parameters;
    }
相关问题