I am attempting to use Ninject v3 to test a web service hosted within a .NET4 windows service ( System.ServiceProcess.ServiceBase
).
I create MyServerModule : NinjectModule
, and within Load()
I conditionally bind my interfaces to runtime objects based on app.config
settings.
In ServiceBase.OnStart()
, the init steps are : First, create a new StandardKernel
based on an instance of MyServerModule
and associate it with Ninject container. Next, create an instance of NinjectServiceHost
based on my service class, and open the service host to begin listening.
In Ninject v2, the code is :
IKernel kernel = new StandardKernel(new MyServerModule());
KernelContainer.Kernel = kernel;
NinjectServiceHost serviceHost = new NinjectServiceHost(typeof(MyService));
serviceHost.Open();
However, in Ninject v3, KernelContainer
no longer exists. Additionally, NinjectServiceHost()
now accepts a server factory that subclasses IServiceBehavior
.
How do I perform similar initialization steps in Ninject v3 ?
I ran into a similar issue running several self-hosted WCF services within a Windows Service. I ended up using
kernel.Get<IServiceBehavior>()
as the first parameter when initializing each NinjectServiceHost.Your example might look like: