简单注入器 - Windows 窗体示例已损坏

Eri*_*ace 5 c# winforms simple-injector

我正在尝试在 Windows 窗体应用程序中使用 Simple Injector。不幸的是,https://simpleinjector.readthedocs.org/en/latest/windowsformsintegration.html上的文档不正确或已过时。

当您实际运行该示例时,会导致以下错误: The configuration is invalid. The following diagnostic warnings were reported: -[Disposable Transient Component] MainView is registered as transient, but implements IDisposable.

此外,我正在构建的应用程序是一个 Winforms MVP(被动视图)项目。我可以将 MainView 的范围更改为单例并且它可以工作。但是对于我的一生,由于这个范围界定问题,我无法弄清楚如何打开其他窗口。有没有人在具有多个窗口的真实 MVP winforms 应用程序中成功使用 SimpleInjector?我很想知道 Presenters、Forms/Views 和 Main 入口点是如何配置的,以及它们的 Lifestyle 范围是什么。

仅供参考,我曾尝试使用 LifetimeScoping 和 ExecutionContextScopeing 扩展,但绝对没有任何效果。也许这只是一个 PEBKAC 问题。

谢谢,埃里克

小智 4

使用container.RegisterSingleton<“object”>();

http://simpleinjector.readthedocs.org/en/latest/lifetimes.html