有没有将 Ninject 与 Windows 服务一起使用的好例子?我不确定是否需要任何扩展。另外,不确定 Composition Root 应该是什么?有没有将 Ninject 与 Windows 服务一起使用的好例子?
最佳答案
Windows 服务在依赖注入(inject)方面与常规命令行应用程序没有太大区别。直接的组合根是您的 Main
方法。
我通常采用的方法是创建 StandardKernel
,其中包含一个模块,我的依赖关系已在其中得到解决。然后使用 kernel.Get
解析顶级依赖项 - 其他一切都将从那里开始:
static void Main(string[] args)
{
var kernel = new StandardKernel(new FooModule());
var barDependency = kernel.Get<Bar>();
System.ServiceProcess.ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[] { new FooService(barDependency) };
System.ServiceProcess.ServiceBase.Run(ServicesToRun);
}
关于c# - 将 Ninject 与 Windows 服务一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10286235/