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
Globalyou have to initialize it properly by callingApplication_Start()andInit()as IIS would do. After this your integration test should run.But be aware that in reality
kernel.Getis not triggered directly as you do in your test. The creation of the service is much more complex.