Ninject Topshelf Microsoft.Owin.Hosting -



Ninject Topshelf Microsoft.Owin.Hosting -

how can utilize topshelf.ninject , incorporate owinninjectdependencyresolver (from ninject.web.webapi.owinhost)?

i can work need instantiate ninject kernel twice (once topshelf , 1 time httpconfiguration.dependencyresolver. not seem right way utilize ninject.

any help or illustration code related specific design helpful.

so had same problem , managed solve it.

the key to:

not utilize [assembly: owinstartup(...)] attribute bootstrap owin. inject ikernel using ninject service class beingness used configure topshelf. use start(startoptions options, action<owin.iappbuilder> startup) method of microsoft.owin.hosting.webapp class start self-hosted web-app. pass in injected kernel owin bootstrap routine via startup action. use passed-in reference kernel in phone call appbuilder.useninjectmiddleware(() => kernel); in owin startup routine, rather appbuilder.useninjectmiddleware(createkernel);. profit.

the finish solution follows:

create console application solution called backgroundprocessor. add packages listed in packages.config listing below solution using nuget. add app.config solution using listing below. add each of code files provided below solution; have provided total listings because of lines of code quite sensitive using statements because of heavy utilize of extension methods libraries utilized solution. compile , run project. test solution hitting url http://localhost:9000/test on local machine.

packages.config

<?xml version="1.0" encoding="utf-8"?> <packages> <package id="microsoft.aspnet.webapi" version="5.0.0" targetframework="net45" /> <package id="microsoft.aspnet.webapi.client" version="5.0.0" targetframework="net45" /> <package id="microsoft.aspnet.webapi.core" version="5.0.0" targetframework="net45" /> <package id="microsoft.aspnet.webapi.owin" version="5.0.0" targetframework="net45" /> <package id="microsoft.aspnet.webapi.webhost" version="5.0.0" targetframework="net45" /> <package id="microsoft.owin" version="2.1.0" targetframework="net45" /> <package id="microsoft.owin.host.httplistener" version="2.1.0" targetframework="net45" /> <package id="microsoft.owin.hosting" version="2.1.0" targetframework="net45" /> <package id="newtonsoft.json" version="4.5.11" targetframework="net45" /> <package id="ninject" version="3.2.2.0" targetframework="net45" /> <package id="ninject.extensions.contextpreservation" version="3.2.0.0" targetframework="net45" /> <package id="ninject.extensions.namedscope" version="3.2.0.0" targetframework="net45" /> <package id="ninject.web.common" version="3.2.2.0" targetframework="net45" /> <package id="ninject.web.common.owinhost" version="3.2.2.0" targetframework="net45" /> <package id="ninject.web.webapi" version="3.2.0.0" targetframework="net45" /> <package id="ninject.web.webapi.owinhost" version="3.2.1.0" targetframework="net45" /> <package id="owin" version="1.0" targetframework="net45" /> <package id="topshelf" version="3.1.3" targetframework="net45" /> <package id="topshelf.ninject" version="0.3.0.0" targetframework="net45" /> </packages>

app.config

<?xml version="1.0" encoding="utf-8"?> <configuration> <startup> <supportedruntime version="v4.0" sku=".netframework,version=v4.5" /> </startup> <runtime> <assemblybinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentassembly> <assemblyidentity name="topshelf" publickeytoken="b800c4cfcdeea87b" culture="neutral" /> <bindingredirect oldversion="0.0.0.0-3.1.122.0" newversion="3.1.122.0" /> </dependentassembly> <dependentassembly> <assemblyidentity name="ninject" publickeytoken="c7192dc5380945e7" culture="neutral" /> <bindingredirect oldversion="0.0.0.0-3.2.0.0" newversion="3.2.0.0" /> </dependentassembly> <dependentassembly> <assemblyidentity name="microsoft.owin" publickeytoken="31bf3856ad364e35" culture="neutral" /> <bindingredirect oldversion="0.0.0.0-2.1.0.0" newversion="2.1.0.0" /> </dependentassembly> </assemblybinding> </runtime> </configuration>

program.cs

using system; using system.collections.generic; using system.linq; using system.text; using system.threading.tasks; using ninject; using topshelf; using topshelf.ninject; namespace backgroundprocessor { using modules; using services; public class programme { public static int main(string[] args) { var exitcode = hostfactory.run ( c => { c.useninject(new module()); c.service<service> ( sc => { sc.constructusingninject(); sc.whenstarted((service, hostcontrol) => service.start(hostcontrol)); sc.whenstopped((service, hostcontrol) => service.stop(hostcontrol)); } ); c.setservicename("backgroundprocessorsvc"); c.setdisplayname("background processor"); c.setdescription("processes things in background"); c.enablepauseandcontinue(); c.enableshutdown(); c.startautomaticallydelayed(); c.runaslocalsystem(); } ); homecoming (int)exitcode; } } }

modules\module.cs

using system; using system.collections.generic; using system.linq; using system.text; using ninject.modules; namespace backgroundprocessor { using contracts; using services; namespace modules { public class module : ninjectmodule { public override void load() { bind<iservice>().to<service>(); } } } }

contracts\iservice.cs

using system; namespace backgroundprocessor { namespace contracts { public interface iservice { bool start(topshelf.hostcontrol hostcontrol); bool stop(topshelf.hostcontrol hostcontrol); } } }

services\service.cs

using system; using system.collections.generic; using system.linq; using system.text; using microsoft.owin.hosting; using ninject; using topshelf; namespace backgroundprocessor { using configs; using contracts; namespace services { public class service : iservice { private readonly ikernel kernel; public service(ikernel kernel) : base() { this.kernel = kernel; } protected ikernel kernel { { homecoming this.kernel; } } protected idisposable webappholder { get; set; } protected int port { { homecoming 9000; } } public bool start(hostcontrol hostcontrol) { if (webappholder == null) { webappholder = webapp.start(new startoptions { port = port }, appbuilder => { new startupconfig().configure(appbuilder, kernel); }); } homecoming true; } public bool stop(hostcontrol hostcontrol) { if (webappholder != null) { webappholder.dispose(); webappholder = null; } homecoming true; } } } }

configs\startupconfig.cs

using system; using system.collections.generic; using system.linq; using system.text; using system.web.http; using ninject; using ninject.web.common.owinhost; using ninject.web.webapi.owinhost; using owin; namespace backgroundprocessor { namespace configs { public class startupconfig { public void configure(iappbuilder appbuilder, ikernel kernel) { var config = new httpconfiguration(); config.maphttpattributeroutes(); config.mapdefinedroutes(); appbuilder.useninjectmiddleware(() => kernel); appbuilder.useninjectwebapi(config); } } } }

configs\routesconfig.cs

using system; using system.collections.generic; using system.linq; using system.text; using system.threading.tasks; using system.web; using system.web.http; namespace backgroundprocessor { namespace configs { public static class routesconfig { public static void mapdefinedroutes(this httpconfiguration config) { config.routes.maphttproute ( name: "defaultapi", routetemplate: "api/{controller}/{id}", defaults: new { id = routeparameter.optional } ); } } } }

controllers\testcontroller.cs

using system; using system.collections.generic; using system.linq; using system.net; using system.net.http; using system.text; using system.threading.tasks; using system.web; using system.web.http; namespace backgroundprocessor { namespace controllers { [routeprefix("test")] public class testcontroller : apicontroller { [httpget] [route("")] public httpresponsemessage index() { homecoming request.createresponse<string>(httpstatuscode.ok, "hello world!"); } } } }

ninject owin topshelf

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -