'Getting issue with the dependency injection asp core 6 with winforms creation?

Github link to sample project

   static void Main()
    {
        ApplicationConfiguration.Initialize();

        var builder = new HostBuilder()
         .ConfigureServices((hostContext, services) =>
         {
             services.AddTransient<Form1>();
             services.AddTransient<Form2>();

         });

        var host = builder.Build();

        using (var serviceScope = host.Services.CreateScope())
        {
            IServiceProvider services = serviceScope.ServiceProvider;
            Application.Run(services.GetRequiredService<Form1>());
        }
  
        
    }

Form1 is MDI MdiParent where i am injecting Form 2

public partial class Form1 : Form
    {
        private readonly Form2 form2;

        public Form1(Form2 form2)
        {
            InitializeComponent();
            this.form2 = form2;
        }

        private void form2ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.form2.MdiParent = this;
            this.form2.Show();
        }
    }

When I Open Form2 by clicking from Menu it opens and close it by using [X] button When i reopen it i am getting error System.ObjectDisposedException: 'Cannot access a disposed object.
Object name: 'Form2'.'



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source