使用Ninject与Windows服务 [英] Using Ninject with a Windows Service
问题描述
使用Ninject与Windows服务的任何很好的例子?我不知道是否有什么任何扩展我的需要。另外,不知道成分根应该是什么?使用Ninject与Windows服务在那里的任何好的例子吗?
Any good examples of using Ninject with a Windows Service? I'm not sure what if any extensions I need. Also, not sure what the Composition Root should be? Any good examples of using Ninject with a Windows service out there?
推荐答案
一个窗口服务不从一个普通的命令行差距不大在应用方面的依赖注入。直进成分根是你的主
方法。
A windows service does not differ much from a regular command line application in regard to dependency injection. The straight-forward composition root is your Main
method.
的方式,我通常做它是创建 StandardKernel
那里在我的依赖是解决的一个模块。然后使用 kernel.Get
来解决顶层的依赖关系 - 一切将遵循有:
The way I usually have done it is create the StandardKernel
there with a module in which my dependencies are resolved. Then use kernel.Get
to resolve the top level dependencies - everything else will follow from there:
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);
}
这篇关于使用Ninject与Windows服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!