c# - WebApi Filter Unity -


environement : unity , asp.net mvc webapi follow sample webpage http://www.asp.net/mvc/tutorials/hands-on-labs/aspnet-mvc-4-dependency-injection , after modifications, find way inject filter controler.

i used code in boostrapper.cs

var container = new unitycontainer(); container.registerinstance<ifilterprovider>("filterprovider", new    filterprovider(container)); container.registerinstance<iactionfilter>("logactionfilter", new traceactionfilter()); 

i add class filterprovider

public class filterprovider : ifilterprovider {     private iunitycontainer container;      public filterprovider(iunitycontainer container)     {         this.container = container;     }      public ienumerable<filter> getfilters(controllercontext controllercontext, actiondescriptor actiondescriptor)     {         foreach (iactionfilter actionfilter in this.container.resolveall<iactionfilter>())         {             yield return new filter(actionfilter, filterscope.first, null);         }     } } 

so each time method called called class implemented controller, can see code called in class traceactionfilter.

public class traceactionfilter : iactionfilter {     public void onactionexecuted(actionexecutedcontext filtercontext)     {         filtercontext.httpcontext.trace.write("onactionexecuted");         filtercontext.httpcontext.trace.write("action " + filtercontext.actiondescriptor.actionname);         filtercontext.httpcontext.trace.write("controller " + filtercontext.actiondescriptor.controllerdescriptor.controllername);     }      public void onactionexecuting(actionexecutingcontext filtercontext)     {                 filtercontext.httpcontext.trace.write("onactionexecuting");                 filtercontext.httpcontext.trace.write("action " + filtercontext.actiondescriptor.actionname);                 filtercontext.httpcontext.trace.write("controller " + filtercontext.actiondescriptor.controllerdescriptor.controllername);     } }  

but don't find on web, sample or right way permitting same thing : inject filter add same behavior webapi. way find add manually filter using tag, not used dependency injection. can provide help?

best regards, alexandre

[update after first answer] launch command install-package unity.aspnet.webapi permit access unityconfig. add in global.asax

    protected void application_start()     {         bootstrapper.initialise();         unitywebapiactivator.start();         configureapi(globalconfiguration.configuration); //line added          arearegistration.registerallareas();         webapiconfig.register(globalconfiguration.configuration);         filterconfig.registerglobalfilters(globalfilters.filters);         routeconfig.registerroutes(routetable.routes);         bundleconfig.registerbundles(bundletable.bundles);     } 

i add in method configureapi()

    void configureapi(httpconfiguration config)     {         var unity = new unitycontainer();         unity.registertype<productsapicontroller>();         unity.registertype<categoriesapicontroller>();          unity.registertype<iproductservice, productservice>(             new hierarchicallifetimemanager());         unity.registertype<iproductsdatabase, productsdatabase>(              new hierarchicallifetimemanager());          unity.registertype<icategorieservice, categorieservice>(             new hierarchicallifetimemanager());         unity.registertype<icategoriesdatabase, categoriesdatabase>(              new hierarchicallifetimemanager());          unity.registertype<ilogprovider, elmahprovider>(              new hierarchicallifetimemanager());          unity.registertype<irootcontroller, rootcontroller>(             new hierarchicallifetimemanager());          config.dependencyresolver = new ioccontainer(unity);          //added parts         var providers = globalconfiguration.configuration.services.getfilterproviders().tolist();         config.services.add(             typeof(system.web.http.filters.ifilterprovider),             new filter2provider(unityconfig.getconfiguredcontainer()));          var defaultprovider = providers.first(p => p system.web.http.filters.actiondescriptorfilterprovider);         config.services.remove(typeof(system.web.http.filters.ifilterprovider), defaultprovider);                 } 

when call method ...api/categoriesapi/getall breakpoint in traceactionfilter not called.

any idea? continu search.

you need create custom system.web.http.filters.filterprovider (be aware mvc has classes same names in different namespace):

public class unityactionfilterprovider      : system.web.http.filters.actiondescriptorfilterprovider,        system.web.http.filters.ifilterprovider {     private readonly iunitycontainer container;      public unityactionfilterprovider(iunitycontainer container)     {         this.container = container;     }      public new ienumerable<filterinfo> getfilters(httpconfiguration configuration,          httpactiondescriptor actiondescriptor)     {         foreach (iactionfilter actionfilter in container.resolveall<iactionfilter>())         {             // todo: determine correct filterscope             yield return new filterinfo(actionfilter, filterscope.global);         }     } } 

then need set filter provider @ startup:

var providers =      globalconfiguration.configuration.services.getfilterproviders().tolist();  globalconfiguration.configuration.services.add(     typeof(system.web.http.filters.ifilterprovider),     new unityactionfilterprovider(unityconfig.getconfiguredcontainer()));  var defaultprovider = providers.first(p => p actiondescriptorfilterprovider);  globalconfiguration.configuration.services.remove(     typeof(system.web.http.filters.ifilterprovider),      defaultprovider); 

you should use above unity bootstrapper asp.net web api

a similar question using attributes: asp.net web api - inject dependencies actionfilterattribute

since using container maintain list of filters register filterinfo directly instead of iactionfilter.


Comments

Popular posts from this blog

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -

java - JavaFX 2 slider labelFormatter not being used -