asp.net web api - WebApi 2 -cant determined DataProtection.IDataProtector Dependency Injection with structuremap -
i'm using this beriliant project base of mvc project. when use webapi project there problem in idataprotector
injection. redesign base , upload here, , add console project testing authorizing webapi.
this structuremap initialization :
private static readonly lazy<container> _containerbuilder = new lazy<container>(initstructuremap, lazythreadsafetymode.executionandpublication); public static icontainer container { { return _containerbuilder.value; } } return new container(ioc => { ioc.for<iunitofwork>().hybridhttporthreadlocalscoped().use(() => new dbcontext()); ioc.for<idataserializer<authenticationticket>>().use<ticketserializer>(); ioc.for<isecuredataformat<authenticationticket>>().use<securedataformat<authenticationticket>>(); });
and in webapiconfig class di this:
var container = structuremapmvc.container; globalconfiguration.configuration.services.replace( typeof(ihttpcontrolleractivator), new structuremaphttpcontrolleractivator(container));
in startup create dataprotector iappbuilder :
public void configureauth(iappbuilder app) { structuremapmvc.container.configure(config => { config.for<idataprotectionprovider>() .hybridhttporthreadlocalscoped() .use(() => app.getdataprotectionprovider()); }); }
it start after webapiconfig , idataprotection not work in webapi. servicelayer in separate project , dataprotection need inject there.
you need add 2 class project :
1-structuremapdependencyscope class
using structuremap; using system; using system.collections.generic; using system.linq; using system.web.http.dependencies; namespace project.helpers { public class structuremapdependencyscope : idependencyscope { protected readonly icontainer container; public structuremapdependencyscope(icontainer container) { if (container == null) { throw new argumentnullexception("container"); } this.container = container; } public void dispose() { this.container.dispose(); } public object getservice(type servicetype) { if (servicetype == null) { return null; } try { return servicetype.isabstract || servicetype.isinterface ? this.container.trygetinstance(servicetype) : this.container.getinstance(servicetype); } catch { return null; } } public ienumerable<object> getservices(type servicetype) { return this.container.getallinstances(servicetype).cast<object>(); } } }
2-structuremapdependencyresolver class
using structuremap; using system.web.http.dependencies; namespace project.helpers { public class structuremapdependencyresolver : structuremapdependencyscope, idependencyresolver { public structuremapdependencyresolver(icontainer container) : base(container) { } public idependencyscope beginscope() { icontainer child = this.container.getnestedcontainer(); return new structuremapdependencyresolver(child); } } }
at end replace code yours in webapiconfig class:
// ioc config var container = smobjectfactory.container; // web api configuration , services config.dependencyresolver = new structuremapdependencyresolver(container);
read more dependency injection in asp.net mvc 4 , web api structuremap here.
Comments
Post a Comment