c# - Ninject interception attribute with parameters passed to interceptor? -
i have interception working (very simplistically) following code:
(see question @ bottom)
my interceptor:
public interface iauthorizationinterceptor : iinterceptor { } public class authorizationinterceptor : iauthorizationinterceptor { public iparameter[] attributeparameters { get; private set; } // doesnt work currently... paramters has no values public authorizationinterceptor(iparameter[] parameters) { attributeparameters = parameters; } public void intercept(iinvocation invocation) { // have tried attributes // 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) { // test see if can arguments } //todo: replace call auth system code. bool isauthorized = true; if (isauthorized == true) { // stuff } else { throw new accessviolationexception("failed"); } } protected void afterinvoke(iinvocation invocation) { } }
my attribute:
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 have tried passing constructorargument(s) result // in inteceptor constructor same. return request.context.kernel.get<iauthorizationinterceptor>(param.toarray()); } }
applied method:
[authorize("test")] public virtual result<vault> vault(datetime date, bool livemode = true, int? snapshotid = null) { ... }
this works, , able pass additional parameters through attribute this:
[authorize("test")]
if you'll noticed in attribute grabbing parameters attribute, able access in attribute class, unable pass interceptor. have tried using constructorargument in kernel.get<>() call, doesnt throw error, authorizationinterceptor constructor doesnt values ninject. have tried getcustomattributes() can see in code sample returns nothing. looking @ other similar posts (ninject interception 3.0 interface proxy method attributes) seems correct way, doesn't work. ideas?
i able working creating initialization method on interceptor. don't it, because ties me specific implementation of authorizationinterceptor, gets job done (damn deadlines lol). still know if there better way this, not going mark own answer in hopes comes along better way of doing this.
i modified attribute follows:
public override iinterceptor createinterceptor(iproxyrequest request) { authorizationinterceptor attr = (authorizationinterceptor)request.context.kernel.get<iauthorizationinterceptor>(); attr.init(attributeparameters); return attr; }
and created init method on the interceptor:
public void init(params string[] parameters) { attributeparameters = parameters; }
Comments
Post a Comment