c# - Can I get WebApi to work with IoC Aspects/Interceptor -
c# - Can I get WebApi to work with IoC Aspects/Interceptor -
i'm wcf background used ioc aspects/interceptors abstract functions such authentication , logging. add together required interfaces aspects constructor same way typical ioc setup.
i'm trying apply same sort of process webapi, controllers inherit apicontroller , not implement interface. i'm assuming there different way of applying aspects maybe?
public class mycontroller: apicontroller { private readonly iunitofwork _unitofwork; private readonly iloginservice _loginservice; private readonly ilog _log; public logincontroller(iloginservice loginservice, iunitofwork unitofwork, ilog log) { this._loginservice = loginservice; this._unitofwork = unitofwork; this._log = log; } // want intercept method using usertokenauthenticationinterceptor public httpresponsemessage get(guid id) { _log.log(log something); // code thats gets info using this._loginservice _log.log(log save); _unitofwork.save(); } }
the aspect
public class usertokenauthenticationinterceptor : iinterceptionbehavior { private readonly iloginservice _loginservice; private readonly ilog _log; public usertokenauthenticationinterceptor(ilog log, iloginservice loginservice) { this._log = log; this._loginservice = loginservice; } public imethodreturn invoke(imethodinvocation input, getnextinterceptionbehaviordelegate getnext) { _log.log(log entering authentication aspect); // authentication here using this._loginservice _log.log(log exiting authentication aspect); } public ienumerable<type> getrequiredinterfaces() { homecoming type.emptytypes; } public bool willexecute { { homecoming true; }} }
container registration:
container.registertype<iunitofwork, unitofwork.unitofwork>(new hierarchicallifetimemanager()); container.registertype<iloginservice , loginservice>(); container.registertype<ilog, logservice>();
i'm using unity in example. can point me in right direction?
thanks help everyone, figured out.
i got of reply article https://unity.codeplex.com/discussions/446780
i used the next nuget packages.
unity (i added 1 first) unity.webapi (has unity version problem if unity isnt added first)first needed new ifilterprovider implementation. job register actionfilters container.
public class unityactionfilterprovider : actiondescriptorfilterprovider, ifilterprovider { private readonly iunitycontainer container; public unityactionfilterprovider(iunitycontainer container) { this.container = container; } public new ienumerable<filterinfo> getfilters(httpconfiguration configuration, httpactiondescriptor actiondescriptor) { var filters = base.getfilters(configuration, actiondescriptor); foreach (var filter in filters) { container.buildup(filter.instance.gettype(), filter.instance); } homecoming filters; } }
then registration method required register new actionfilterprovider , remove original webapi implementation. needs executed in registercomponents() method in unityconfig.cs file unity.webapi nuget bundle creates.
public static void registerfilterproviders(iunitycontainer container) { var providers = globalconfiguration.configuration.services.getfilterproviders().tolist(); globalconfiguration.configuration.services.add(typeof(system.web.http.filters.ifilterprovider), new unityactionfilterprovider(container)); var defaultprovider = providers.first(p => p actiondescriptorfilterprovider); globalconfiguration.configuration.services.remove(typeof(system.web.http.filters.ifilterprovider), defaultprovider); }
in same registercomponents() method registered types
container.registertype<iunitofwork, unitofwork.unitofwork>(new hierarchicallifetimemanager()); container.registertype<iloginservice , loginservice>(); container.registertype<ilog, logservice>();
next, needed create class based on authorizeattribute.
public class usertokenauthenticationattribute : authorizeattribute { private iloginservice _loginservice; // magic part - unity reads attribute , sets injects related property. means no parameters required in constructor. [microsoft.practices.unity.dependency] public iloginservice loginservice { { homecoming this._loginservice; } set { this._loginservice = value; } } protected override bool isauthorized(httpactioncontext actioncontext) { // authorise code goes here using injected this._loginservice } }
a log action filter required actionfilterattribute
public sealed class logattribute : actionfilterattribute { private ilog _log; // magic part - unity reads attribute , sets injects related property. means no parameters required in constructor. [microsoft.practices.unity.dependency] public ilog log { { homecoming this._log; } set { this._log = value; } } public override void onactionexecuted(httpactionexecutedcontext actionexecutedcontext) { this._log.info("exited " + actioncontext.request.method); } public override void onactionexecuting(httpactioncontext actioncontext) { this._log.info("entering" + actioncontext.request.method); } }
now lets configure webapi controller. need decorate class our new attributes
[usertokenauthentication] // magic attribute in utilize [log] // magic attribute in utilize public class mycontroller: apicontroller { private readonly iunitofwork _unitofwork; private readonly iloginservice _loginservice; private readonly ilog _log; public mycontroller(iloginservice loginservice, iunitofwork unitofwork, ilog log) { this._loginservice = loginservice; this._unitofwork = unitofwork; this._log = log; } [system.web.http.allowanonymous] // doesnt require authentication not logged in yet public httpresponsemessage get(guid id) { _log.log(log something); // code thats gets info using this._loginservice _log.log(log save); _unitofwork.save(); } public httpresponsemessage getmydetails(guid id) { _log.log(log something); // code thats gets info using this._loginservice _log.log(log save); _unitofwork.save(); } }
c# wcf asp.net-web-api aspects unity-interception
Comments
Post a Comment