I Have a Windows Forms Application where I'm trying to use Dependency Injectio for my Forms and some Services. This is the initial configuration in my StartUpModule:
Module StartUpModule
Sub Main(args As String())
Dim Host = CreateHostBuilder(args).Build()
Using ServiceScope = Host.Services.CreateScope()
Dim Services = ServiceScope.ServiceProvider
Try
Dim Form1 = Services.GetRequiredService(GetType(BaseMenu))
Application.EnableVisualStyles()
Application.Run(Form1)
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
End Using
End Sub
Public Function CreateHostBuilder(args As String()) As IHostBuilder
Return Host.CreateDefaultBuilder(args).ConfigureServices(Function(HostContext, Services) ConfigureServices(Services))
End Function
Public Function ConfigureServices(Services As IServiceCollection) As IHostBuilder
'Forms
Services.AddSingleton(GetType(BaseMenu))
Services.AddTransient(GetType(Form1))
Services.AddTransient(GetType(Form2))
'...
Services.AddTransient(GetType(IUserRepository), GetType(UserRepository))
'Servicos
Services.AddTransient(GetType(IUserService), GetType(UserService))
'Controllers
Services.AddTransient(GetType(UserController))
End Function
End Module
I need to create an SPA(Single Page Application), so I nest the Child Forms onto the BaseMenu Form. To do this I need to instantiate the Child Forms and I want to use DI for that. For that I pass ServiceProvider to My BaseMenu Form As Follows:
Public Class BaseMenu
Private ReadOnly ServiceProvider As IServiceProvider
Public Sub New(_ServiceProvider As IServiceProvider)
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
ServiceProvider = _ServiceProvider
End Sub
Private ActiveForm As Form
Public Sub OpenChildForm(ChildForm As Form)
If ActiveForm IsNot Nothing Then
If ChildForm.GetType <> ActiveForm.GetType Then
ActiveForm.Close()
Else
Exit Sub
End If
End If
Panel1.SuspendLayout()
ActiveForm = ChildForm
AddHandler ActiveForm.FormClosed, AddressOf ChildForm_Closed
ChildForm.TopLevel = False
ChildForm.FormBorderStyle = FormBorderStyle.None
ChildForm.Dock = DockStyle.Fill
Panel1.Controls.Add(ChildForm)
Panel1.Tag = ChildForm
ChildForm.BringToFront()
ChildForm.Show()
Panel1.ResumeLayout()
End Sub
Private Sub ChildForm_Closed()
ActiveForm = Nothing
GC.Collect()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
OpenChildForm(ServiceProider.GetService(GetType(Form1)))
End Sub
So far so Good. It opens the forms as intended. The problem I'm facing is when I Close the child Form or open another form in its place. The references to said form aren't being deleted, creating a memory leak. Everytime I reopen the childform a new one gets created but the previous one does not get deleted.
I'm guessing it has something to do with the scope of the App, but I'm not sure how to tackle it.