我目前(非常簡單化)用下面的代碼攔截工作:Ninject攔截屬性的參數傳遞給攔截器?
(見底部的問題)
我的攔截器:
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")]
如果你會發現在我的屬性,我抓住從屬性的一些參數,我能訪問屬性類,但我無法將它們傳遞給攔截器。我已經嘗試在Kernel.Get <>()調用中使用ConstructorArgument,它不會拋出錯誤,但AuthorizationInterceptor構造函數不會從ninject獲取任何值。我也嘗試過GetCustomAttributes(),你可以在代碼示例中看到,但是這也不會返回任何結果。從看起來像這樣的其他類似帖子(Ninject Interception 3.0 Interface proxy by method attributes)似乎是正確的方式,但它不起作用。有任何想法嗎?
構造器參數應該工作。 (IEnumerable parameters)和Kernel.Get (新的ConstructorArgument –
BatteryBackupUnit