【问题标题】:Simple Injector - Windows Forms Example Broken简单注入器 - Windows 窗体示例损坏
【发布时间】:2015-11-05 21:26:15
【问题描述】:

我正在尝试在 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 和 ExecutionContextScoping 扩展,但绝对没有任何效果。也许这只是一个 PEBKAC 问题。

谢谢, 埃里克

【问题讨论】:

    标签: c# winforms simple-injector


    【解决方案1】:

    【讨论】:

      【解决方案2】:

      我在尝试重构的旧 Winforms 应用程序中遇到了非常相似的问题。

      在我的情况下,我也相信在你的情况下,表单是使用 Application.Run() 创建的。如果我们看一下这个stackoverflow link,它会提到运行时会自动调用 dispose() 方法。

      这意味着我们可以放心地忽略simpleinjector docs 中描述的此获胜表单注册的警告。

      代码如下所示:

      Registration registration = _container.GetRegistration(typeof(MainView)).Registration;
      registration.SuppressDiagnosticWarning(DiagnosticType.DisposableTransientComponent, "Main Windows Form (MainView) will be automatically disposed by runtime as it is registered using Application.Run()");
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-04-07
        • 1970-01-01
        • 2013-05-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-06-30
        相关资源
        最近更新 更多