我应该在 windows 服务中的何处创建组合根目录?

Where should I create the composition root in a windows service?

在我拥有的 windows 服务应用程序的命令行 exe 应用程序中,有一个 main 方法,这就是我使用简单注入器 [=12] 注册所有依赖项的地方=].那是我的 composition root now. In the same windows service application,有一个 OnStart 可覆盖的方法。这就是我进行 ETL 操作的地方。

我应该把合成根目录放在哪里?在 main 方法或 OnStart?

Where should I keep the composition root? In the main method or OnStart?

两者都是您的 Composition Root 的有效位置。 Main 和您的 OnStart 都可以用作 Composition Root。我自己过去曾使用过这两个位置。通常,我会创建一个单独的 BootstrapperCompositionRoot class 来包含应用程序的合成根,然后直接从主组件或从我的 ServiceBase 派生组件中调用该合成根.