c# - Simple Injector property injection on action filter -


the action filter want inject starts this

public class userauthorisation : authorizeattribute {     public iwcfclientproxy<iappframeworkservicechannel>         frameworkserviceproxy { get; set; } 

i have setup container this:

container.register<iwcfclientproxy<iappframeworkservicechannel>>(     ()=> new wcfclientproxy<iappframeworkservicechannel>());  container.registerinitializer<userauthorisation>(handler => {     handler.frameworkserviceproxy = container        .getinstance<iwcfclientproxy<iappframeworkservicechannel>>(); }); 

when run frameworkserviceproxy property null.

i have read post: simple injector: injecting property in base class , followed answer. have read example in page simple injector documentation.

i not injecting base class , maybe issue?

## update ##

i adding more information think should working has been said in stevens answer.

i using nuget package mvc 3. adds following application:

public static class simpleinjectorinitializer {     /// <summary>initialize container , register mvc3 dependency resolver.</summary>     public static void initialize()     {         var container = new container();         initializecontainer(container);         container.registermvccontrollers(assembly.getexecutingassembly());         container.registermvcattributefilterprovider();         container.verify();         dependencyresolver.setresolver(new simpleinjectordependencyresolver(container));     }      private static void initializecontainer(container container)     {         container.register<iwcfclientproxy<iappframeworkservicechannel>>(() => new wcfclientproxy<iappframeworkservicechannel>());         container.registerinitializer<userauthorisation>(handler =>             {                 handler.frameworkserviceproxy = container.getinstance<iwcfclientproxy<iappframeworkservicechannel>>();             });     } 

this includes container.registermvcattributefilterprovider(); understand should register filter provider , should mean filters created through container (this understanding might wrong) , properties automatically wired-up.

my filter registered in global.asax.cs so:

public static void registerglobalfilters(globalfiltercollection filters) {     filters.add(new handleerrorattribute());     filters.add(new userauthorisation()); } 

it seems me filter not being created container think need else happen ?

you registering initializer on userauthorisation attribute. initializers however, used container when type created container itself. since attributes created clr, initializer won't go off.

the simpleinjector.integration.web.mvc.dll (this nuget package) contains registermvcattributefilterprovider extension method. register attributefilterprovider implicit property injection (and call container.injectproperties method). after calling container.registermvcattributefilterprovider(), see property injected automatically.


Comments

Popular posts from this blog

dns - How To Use Custom Nameserver On Free Cloudflare? -

python - Pygame screen.blit not working -

c# - Web API response xml language -