Form1 注册为瞬态,但实现了 IDisposable

Form1 is registered as transient, but implements IDisposable

几个小时后,我找不到适合我的问题的答案,即使我在 Whosebug 网站上看到了很多像我的问题一样的问题。 在我解释这个问题之前,我想说我在 windows 表单应用程序中实现了用于依赖注入的 simpleInjector ,如下图所示
发生此错误:

The configuration is invalid. The following diagnostic warnings were reported: -[Disposable Transient Component] Form1 is registered as transient, but implements IDisposable. See the Error property for detailed information about the warnings. Please see https://simpleinjector.org/diagnostics how to fix problems and how to suppress individual warnings.

我实现了SimpleInjector就像这个网站的例子Doing dependency injection in Windows Forms

代码:

Main form (Form1)

namespace WindowsFormsAppSqliteDapper
{
    public partial class Form1 : Form
    {
        IStudentService _studentService;
        public Form1(IStudentService studentService)
        {
            _studentService = studentService;
            InitializeComponent();

        }

        private void Form1_Load(object sender, EventArgs e)
        {
           var res = _studentService.GetStudents();

        }
    }
}

Simple Injector Configuration (Program.cs)

namespace WindowsFormsAppSqliteDapper
{
    static class Program
    {
        private static Container container;
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Bootstrap();
            Application.Run(container.GetInstance<Form1>());
        }
        private static void Bootstrap()
        {
            // Create the container as usual.
            container = new Container();
            container.Options.DefaultScopedLifestyle =  new  AsyncScopedLifestyle();

            // Register your types, for instance:
            container.Register<IStudentService, StudentService>(Lifestyle.Scoped);
            //container.Register<IUserContext, WinFormsUserContext>();
            container.Register<IStudentRepository, StudentRepository>(Lifestyle.Scoped);
            container.Register<Form1>();

            // Optionally verify the container.
            container.Verify();

        }
    }
}

如果您需要查看包裹等内容,我会放在这里。 谢谢

我在这一行更改了我的代码

container.Register<Form1>(); 

container.Register<Form1>(Lifestyle.Singleton);

它工作正常。您可以在下面查看完整代码

 private static void Bootstrap()
        {
            // Create the container as usual.
            container = new Container();
           // container.Options.DefaultScopedLifestyle =  new  AsyncScopedLifestyle();

            // Register your types, for instance:
            container.Register<IStudentService, StudentService>(Lifestyle.Singleton);
            //container.Register<IUserContext, WinFormsUserContext>();
            container.Register<IStudentRepository, StudentRepository>(Lifestyle.Singleton);
            container.Register<Form1>(Lifestyle.Singleton);

            // Optionally verify the container.
            container.Verify();

        }

根据 the documentation 如何解决这个问题,严重性只是警告,因此您可以安全地忽略它:

The warning can be suppressed on a per-registration basis as follows:

Registration registration = container.GetRegistration(typeof(IService)).Registration;

registration.SuppressDiagnosticWarning(DiagnosticType.DisposableTransientComponent,
    "Reason of suppression");