Ninject + WCF Issue
I'm trying to use Ninject with a WCF service of mine. I'm using Ninject 2.2.1.4 and Ninject.Extensions.Wcf 2.2.0.4. My unit test to ensure Ninject is working properly fails with an ArgumentNullException; Parameter root: null.
Here is my code:
//A couple of classes to setup ninject and its bindings
public class NinjectBindings : NinjectModule
{
public override void Load()
{
Bind<IEmployeeRepository>().To<SqlEmployeeRepository>().InSingletonScope();
}
}
public class Global : NinjectWcfApplication
{
protected override IKernel CreateKernel()
{
IKernel kernel = new StandardKernel(new开发者_运维百科 NinjectBindings());
return kernel;
}
}
//Test method that fails with ArgumentNullException
[TestMethod]
public void Should_Be_Able_To_Get_Employee_Service_From_Ninject()
{
Global globalService = new Global();
var kernel = globalService.Kernel;
EmployeeService employeeService = kernel.Get<EmployeeService>();
Assert.IsNotNull(employeeService);
}
After creating Global
you have to initialize it properly by calling Application_Start()
and Init()
as IIS would do. After this your integration test should run.
But be aware that in reality kernel.Get
is not triggered directly as you do in your test. The creation of the service is much more complex.
When using Ninject.Wcf
, your Global
doesnt get new
d as you've done - you tell WCF that the factory for creating Global is ~NinjectWcfHostFactory
, which triggers the CreateKernel()
that's not happening in your context.
The example shows the overriding of the factory (in a propery on the service declaration within the .svc
file).
精彩评论