WindowsFormsGenericHost is a simple library that allows you to use the .NET Generic Host model with Windows Forms applications, on both .NET Core and .NET Framework.
This project was inspired by Alex Oswald's WindowsFormsLifetime project. It was released soon after his project, with the aims of adding support for ApplicationContext, and having a slightly different API and implementation. It also borrows heavily from Microsoft.Extensions.Hosting's ConsoleLifetime
I'd recommend using WindowsFormsLifetime instead of this project, as it was the original and is more actively maintained. However, if this project suits your use case more, then feel free to use this one.
Set up your Program.cs
in a similar manner to this:
static class Program
{
public static void Main(string[] args)
{
using var host = Host.CreateDefaultBuilder()
.ConfigureServices(ConfigureServices)
.UseWindowsFormsLifetime<MainForm>()
.Build();
host.Run();
}
private static void ConfigureServices(IServiceCollection services)
{
services.AddForms();
services.AddYourOtherServicesHere();
}
}
Check out the samples
Just grab it from NuGet
PM> Install-Package WindowsFormsGenericHost
$ dotnet add package WindowsFormsGenericHost
Copyright Matthew King. Distributed under the MIT License. Refer to license.txt for more information.