-
Notifications
You must be signed in to change notification settings - Fork 40
Setting up a self hosted web application
This chapter explains how to setup an application in case you want to host the web services yourself in you application instead of deploying the application to IIS.
The first you have to add a reference to Ninject.Web.Common.Selfhost
either by adding the reference manually or installing the corresponding NuGet Package. Also add a reference to Ninject.Extensions.ContextPreservation
for InRequestScope
to behave correctly.
Then you can create and start a NinjectSelfHostBootstrapper
instance in cour composition root of your application like shown in the following example. In order to host services in this bootstrapper you have to pass one or more configurations to the bootstrapper. These configurations are specific to the technology you are using (WCF, WebAPI, ...). Please read the documentation for the technologies that you are using for more informations about how to configure a service.
private void StartNinjectSelfHost()
{
var someWcfService = NinjectWcfConfiguration.Create<MyWcfService, NinjectServiceSelfHostFactory>();
var webApiConfiguration = new HttpSelfHostConfiguration("http://localhost:8080");
webApiConfiguration.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "{controller}/{id}",
defaults: new { id = RouteParameter.Optional, controller = "values" });
this.selfHost = new NinjectSelfHostBootstrapper(
CreateKernel,
someWcfService,
webApiConfiguration);
this.selfHost.Start();
}
/// <summary>
/// Creates the kernel.
/// </summary>
/// <returns>the newly created kernel.</returns>
private static StandardKernel CreateKernel()
{
var kernel = new StandardKernel();
kernel.Load(Assembly.GetExecutingAssembly());
return kernel;
}