我正在尝试使用我的WCF服务来使用Ninject。我使用的是Ninject 2.2.1.4和Ninject.Extensions.Wcf 2.2.0.4。我用来确保Ninject正常工作的单元测试失败,返回一个ArgumentNullException;参数根:空。
下面是我的代码:
//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);
}发布于 2011-07-12 23:51:05
创建Global后,必须像IIS那样通过调用Application_Start()和Init()正确地对其进行初始化。在此之后,您的集成测试应该会运行。
但请注意,在现实中,kernel.Get并不像您在测试中那样直接触发。服务的创建要复杂得多。
发布于 2011-07-12 21:38:46
当使用Ninject.Wcf时,您的Global不会像您所做的那样获得newd -您告诉WCF创建全局的工厂是~NinjectWcfHostFactory,这会触发在您的上下文中没有发生的CreateKernel()。
该示例显示了工厂的覆盖(在.svc文件内服务声明的属性中)。
https://stackoverflow.com/questions/6664122
复制相似问题