c# - 将 Ninject 与 Windows 服务一起使用

标签 c# windows-services ninject

有没有将 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/

相关文章:

asp.net-mvc-3 - Ninject.MVC3 Bootstrapper 的 Kernel 属性被标记为过时。如何访问内核?

c# - IIS - 拒绝访问路径

c# - 使用不同的兼容类型覆盖属性

C# 将数组传递给 Visual Basic 库

c# - Windows 服务内的 TPL

c# - 如何将 Windows 服务中的参数从 Installer 传递到 Program.cs 中的 Main 函数?

c# - 在 C# 中刷新 ListView 时防止项目失去焦点

c# - 在 Windows 7 中使用 Windows 服务截取屏幕截图

c# - 翻译 ninject ISecureDataFormat 绑定(bind)到 Autofac

c# - 可选地使用 Ninject 注入(inject)一个属性