diff --git a/src/NetEvolve.Extensions.Hosting.WinForms/IHostBuilderExtensions.cs b/src/NetEvolve.Extensions.Hosting.WinForms/IHostBuilderExtensions.cs index 68ec520..c436597 100644 --- a/src/NetEvolve.Extensions.Hosting.WinForms/IHostBuilderExtensions.cs +++ b/src/NetEvolve.Extensions.Hosting.WinForms/IHostBuilderExtensions.cs @@ -1,301 +1,301 @@ -namespace NetEvolve.Extensions.Hosting.WinForms; - -using System; -using System.Windows.Forms; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Hosting; -using NetEvolve.Extensions.Hosting.WinForms.Internals; - -#if NET8_0_OR_GREATER -/// -/// Extension methods for or to configure Windows Forms Lifetime. -/// -#elif NET7_0 -/// -/// Extension methods for or to configure Windows Forms Lifetime. -/// -#else -/// -/// Extension methods for to configure Windows Forms Lifetime. -/// -#endif -public static class IHostBuilderExtensions -{ - /// - /// Enables Windows Forms support, builds and starts the host with the specified , - /// then waits for the host to close the before shutting down. - /// - /// Form with which the application is to be started. - /// The to configure. - /// The action to be executed for the configuration of the . - /// with enabled Windows Forms support. - public static IHostBuilder UseWindowsForms( - this IHostBuilder builder, - Action? configure = null - ) - where TStartForm : Form - { - ArgumentNullException.ThrowIfNull(builder); - - return builder.ConfigureServices(services => - services - // Add the start form - .AddSingleton() - .AddSingleton(sp => new ApplicationContext(sp.GetRequiredService())) - // Default WindowsForms services - .AddWindowsFormsLifetime(configure) - ); - } - - /// - /// Enables Windows Forms support, builds and starts the host with the specified , - /// then waits for the host to close the before shutting down. - /// - /// - /// The to configure. - /// The factory. - /// The action to be executed for the configuration of the . - /// with enabled Windows Forms support. - public static IHostBuilder UseWindowsForms( - this IHostBuilder builder, - Func? contextFactory = null, - Action? configure = null - ) - where TApplicationContext : ApplicationContext - { - ArgumentNullException.ThrowIfNull(builder); - - return builder.ConfigureServices(services => - { - services = contextFactory is null - ? services.AddSingleton() - : services.AddSingleton(sp => contextFactory.Invoke(sp)); - - _ = services - .AddSingleton() - // Default WindowsForms services - .AddWindowsFormsLifetime(configure); - }); - } - - /// - /// Enables Windows Forms support, builds and starts the host with the specified , - /// which is created by the function, then waits for the host to close the before shutting down. - /// - /// - /// Form with which the application is to be started. - /// The to configure. - /// The factory. - /// The action to be executed for the configuration of the . - /// with enabled Windows Forms support. - public static IHostBuilder UseWindowsForms( - this IHostBuilder builder, - Func contextFactory, - Action? configure = null - ) - where TApplicationContext : ApplicationContext - where TStartForm : Form - { - ArgumentNullException.ThrowIfNull(builder); - ArgumentNullException.ThrowIfNull(contextFactory); - - return builder.ConfigureServices(services => - services - .AddSingleton() - .AddSingleton(sp => - { - var startForm = sp.GetRequiredService(); - return contextFactory(sp, startForm); - }) - .AddSingleton(sp => - sp.GetRequiredService() - ) - // Default WindowsForms services - .AddWindowsFormsLifetime(configure) - ); - } - -#if NET7_0 - /// - /// Enables Windows Forms support, builds and starts the host with the specified , - /// then waits for the host to close the before shutting down. - /// - /// Form with which the application is to be started. - /// The to configure. - /// The action to be executed for the configuration of the . - /// with enabled Windows Forms support. - public static HostApplicationBuilder UseWindowsForms( - this HostApplicationBuilder builder, - Action? configure = null - ) - where TStartForm : Form - { - ArgumentNullException.ThrowIfNull(builder); - - _ = builder - .Services.AddSingleton() - .AddSingleton(sp => new ApplicationContext(sp.GetRequiredService())) - // Default WindowsForms services - .AddWindowsFormsLifetime(configure); - - return builder; - } - - /// - /// Enables Windows Forms support, builds and starts the host with the specified , - /// then waits for the host to close the before shutting down. - /// - /// - /// The to configure. - /// The factory. - /// The action to be executed for the configuration of the . - /// with enabled Windows Forms support. - public static HostApplicationBuilder UseWindowsForms( - this HostApplicationBuilder builder, - Func? contextFactory = null, - Action? configure = null - ) - where TApplicationContext : ApplicationContext - { - ArgumentNullException.ThrowIfNull(builder); - - var services = contextFactory is null - ? builder.Services.AddSingleton() - : builder.Services.AddSingleton(sp => contextFactory.Invoke(sp)); - - _ = services - .AddSingleton(sp => sp.GetRequiredService()) - // Default WindowsForms services - .AddWindowsFormsLifetime(configure); - - return builder; - } - - /// - /// Enables Windows Forms support, builds and starts the host with the specified , - /// which is created by the function, then waits for the host to close the before shutting down. - /// - /// - /// Form with which the application is to be started. - /// The to configure. - /// The factory. - /// The action to be executed for the configuration of the . - /// with enabled Windows Forms support. - public static HostApplicationBuilder UseWindowsForms( - this HostApplicationBuilder builder, - Func contextFactory, - Action? configure = null - ) - where TApplicationContext : ApplicationContext - where TStartForm : Form - { - ArgumentNullException.ThrowIfNull(builder); - ArgumentNullException.ThrowIfNull(contextFactory); - - _ = builder - .Services.AddSingleton() - .AddSingleton(sp => - { - var startForm = sp.GetRequiredService(); - return contextFactory(sp, startForm); - }) - .AddSingleton(sp => sp.GetRequiredService()) - // Default WindowsForms services - .AddWindowsFormsLifetime(configure); - - return builder; - } -#endif - -#if NET8_0_OR_GREATER - /// - /// Enables Windows Forms support, builds and starts the host with the specified , - /// then waits for the host to close the before shutting down. - /// - /// Form with which the application is to be started. - /// The to configure. - /// The action to be executed for the configuration of the . - /// with enabled Windows Forms support. - public static IHostApplicationBuilder UseWindowsForms( - this IHostApplicationBuilder builder, - Action? configure = null - ) - where TStartForm : Form - { - ArgumentNullException.ThrowIfNull(builder); - - _ = builder - .Services.AddSingleton() - .AddSingleton(sp => new ApplicationContext(sp.GetRequiredService())) - // Default WindowsForms services - .AddWindowsFormsLifetime(configure); - - return builder; - } - - /// - /// Enables Windows Forms support, builds and starts the host with the specified , - /// then waits for the host to close the before shutting down. - /// - /// - /// The to configure. - /// The factory. - /// The action to be executed for the configuration of the . - /// with enabled Windows Forms support. - public static IHostApplicationBuilder UseWindowsForms( - this IHostApplicationBuilder builder, - Func? contextFactory = null, - Action? configure = null - ) - where TApplicationContext : ApplicationContext - { - ArgumentNullException.ThrowIfNull(builder); - - var services = contextFactory is null - ? builder.Services.AddSingleton() - : builder.Services.AddSingleton(sp => contextFactory(sp)); - - _ = services - .AddSingleton(sp => sp.GetRequiredService()) - // Default WindowsForms services - .AddWindowsFormsLifetime(configure); - - return builder; - } - - /// - /// Enables Windows Forms support, builds and starts the host with the specified , - /// which is created by the function, then waits for the host to close the before shutting down. - /// - /// - /// Form with which the application is to be started. - /// The to configure. - /// The factory. - /// The action to be executed for the configuration of the . - /// with enabled Windows Forms support. - public static IHostApplicationBuilder UseWindowsForms( - this IHostApplicationBuilder builder, - Func contextFactory, - Action? configure = null - ) - where TApplicationContext : ApplicationContext - where TStartForm : Form - { - ArgumentNullException.ThrowIfNull(builder); - ArgumentNullException.ThrowIfNull(contextFactory); - - _ = builder - .Services.AddSingleton() - .AddSingleton(sp => - { - var startForm = sp.GetRequiredService(); - return contextFactory(sp, startForm); - }) - .AddSingleton(sp => sp.GetRequiredService()) - // Default WindowsForms services - .AddWindowsFormsLifetime(configure); - - return builder; - } -#endif -} +namespace NetEvolve.Extensions.Hosting.WinForms; + +using System; +using System.Windows.Forms; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using NetEvolve.Extensions.Hosting.WinForms.Internals; + +#if NET8_0_OR_GREATER +/// +/// Extension methods for or to configure Windows Forms Lifetime. +/// +#elif NET7_0 +/// +/// Extension methods for or to configure Windows Forms Lifetime. +/// +#else +/// +/// Extension methods for to configure Windows Forms Lifetime. +/// +#endif +public static class IHostBuilderExtensions +{ + /// + /// Enables Windows Forms support, builds and starts the host with the specified , + /// then waits for the host to close the before shutting down. + /// + /// Form with which the application is to be started. + /// The to configure. + /// The action to be executed for the configuration of the . + /// with enabled Windows Forms support. + public static IHostBuilder UseWindowsForms( + this IHostBuilder builder, + Action? configure = null + ) + where TStartForm : Form + { + ArgumentNullException.ThrowIfNull(builder); + + return builder.ConfigureServices(services => + services + // Add the start form + .AddSingleton() + .AddSingleton(sp => new ApplicationContext(sp.GetRequiredService())) + // Default WindowsForms services + .AddWindowsFormsLifetime(configure) + ); + } + + /// + /// Enables Windows Forms support, builds and starts the host with the specified , + /// then waits for the host to close the before shutting down. + /// + /// + /// The to configure. + /// The factory. + /// The action to be executed for the configuration of the . + /// with enabled Windows Forms support. + public static IHostBuilder UseWindowsForms( + this IHostBuilder builder, + Func? contextFactory = null, + Action? configure = null + ) + where TApplicationContext : ApplicationContext + { + ArgumentNullException.ThrowIfNull(builder); + + return builder.ConfigureServices(services => + { + services = contextFactory is null + ? services.AddSingleton() + : services.AddSingleton(sp => contextFactory.Invoke(sp)); + + _ = services + .AddSingleton() + // Default WindowsForms services + .AddWindowsFormsLifetime(configure); + }); + } + + /// + /// Enables Windows Forms support, builds and starts the host with the specified , + /// which is created by the function, then waits for the host to close the before shutting down. + /// + /// + /// Form with which the application is to be started. + /// The to configure. + /// The factory. + /// The action to be executed for the configuration of the . + /// with enabled Windows Forms support. + public static IHostBuilder UseWindowsForms( + this IHostBuilder builder, + Func contextFactory, + Action? configure = null + ) + where TApplicationContext : ApplicationContext + where TStartForm : Form + { + ArgumentNullException.ThrowIfNull(builder); + ArgumentNullException.ThrowIfNull(contextFactory); + + return builder.ConfigureServices(services => + services + .AddSingleton() + .AddSingleton(sp => + { + var startForm = sp.GetRequiredService(); + return contextFactory(sp, startForm); + }) + .AddSingleton(sp => + sp.GetRequiredService() + ) + // Default WindowsForms services + .AddWindowsFormsLifetime(configure) + ); + } + +#if NET7_0 + /// + /// Enables Windows Forms support, builds and starts the host with the specified , + /// then waits for the host to close the before shutting down. + /// + /// Form with which the application is to be started. + /// The to configure. + /// The action to be executed for the configuration of the . + /// with enabled Windows Forms support. + public static HostApplicationBuilder UseWindowsForms( + this HostApplicationBuilder builder, + Action? configure = null + ) + where TStartForm : Form + { + ArgumentNullException.ThrowIfNull(builder); + + _ = builder + .Services.AddSingleton() + .AddSingleton(sp => new ApplicationContext(sp.GetRequiredService())) + // Default WindowsForms services + .AddWindowsFormsLifetime(configure); + + return builder; + } + + /// + /// Enables Windows Forms support, builds and starts the host with the specified , + /// then waits for the host to close the before shutting down. + /// + /// + /// The to configure. + /// The factory. + /// The action to be executed for the configuration of the . + /// with enabled Windows Forms support. + public static HostApplicationBuilder UseWindowsForms( + this HostApplicationBuilder builder, + Func? contextFactory = null, + Action? configure = null + ) + where TApplicationContext : ApplicationContext + { + ArgumentNullException.ThrowIfNull(builder); + + var services = contextFactory is null + ? builder.Services.AddSingleton() + : builder.Services.AddSingleton(sp => contextFactory.Invoke(sp)); + + _ = services + .AddSingleton(sp => sp.GetRequiredService()) + // Default WindowsForms services + .AddWindowsFormsLifetime(configure); + + return builder; + } + + /// + /// Enables Windows Forms support, builds and starts the host with the specified , + /// which is created by the function, then waits for the host to close the before shutting down. + /// + /// + /// Form with which the application is to be started. + /// The to configure. + /// The factory. + /// The action to be executed for the configuration of the . + /// with enabled Windows Forms support. + public static HostApplicationBuilder UseWindowsForms( + this HostApplicationBuilder builder, + Func contextFactory, + Action? configure = null + ) + where TApplicationContext : ApplicationContext + where TStartForm : Form + { + ArgumentNullException.ThrowIfNull(builder); + ArgumentNullException.ThrowIfNull(contextFactory); + + _ = builder + .Services.AddSingleton() + .AddSingleton(sp => + { + var startForm = sp.GetRequiredService(); + return contextFactory(sp, startForm); + }) + .AddSingleton(sp => sp.GetRequiredService()) + // Default WindowsForms services + .AddWindowsFormsLifetime(configure); + + return builder; + } +#endif + +#if NET8_0_OR_GREATER + /// + /// Enables Windows Forms support, builds and starts the host with the specified , + /// then waits for the host to close the before shutting down. + /// + /// Form with which the application is to be started. + /// The to configure. + /// The action to be executed for the configuration of the . + /// with enabled Windows Forms support. + public static IHostApplicationBuilder UseWindowsForms( + this IHostApplicationBuilder builder, + Action? configure = null + ) + where TStartForm : Form + { + ArgumentNullException.ThrowIfNull(builder); + + _ = builder + .Services.AddSingleton() + .AddSingleton(sp => new ApplicationContext(sp.GetRequiredService())) + // Default WindowsForms services + .AddWindowsFormsLifetime(configure); + + return builder; + } + + /// + /// Enables Windows Forms support, builds and starts the host with the specified , + /// then waits for the host to close the before shutting down. + /// + /// + /// The to configure. + /// The factory. + /// The action to be executed for the configuration of the . + /// with enabled Windows Forms support. + public static IHostApplicationBuilder UseWindowsForms( + this IHostApplicationBuilder builder, + Func? contextFactory = null, + Action? configure = null + ) + where TApplicationContext : ApplicationContext + { + ArgumentNullException.ThrowIfNull(builder); + + var services = contextFactory is null + ? builder.Services.AddSingleton() + : builder.Services.AddSingleton(sp => contextFactory(sp)); + + _ = services + .AddSingleton(sp => sp.GetRequiredService()) + // Default WindowsForms services + .AddWindowsFormsLifetime(configure); + + return builder; + } + + /// + /// Enables Windows Forms support, builds and starts the host with the specified , + /// which is created by the function, then waits for the host to close the before shutting down. + /// + /// + /// Form with which the application is to be started. + /// The to configure. + /// The factory. + /// The action to be executed for the configuration of the . + /// with enabled Windows Forms support. + public static IHostApplicationBuilder UseWindowsForms( + this IHostApplicationBuilder builder, + Func contextFactory, + Action? configure = null + ) + where TApplicationContext : ApplicationContext + where TStartForm : Form + { + ArgumentNullException.ThrowIfNull(builder); + ArgumentNullException.ThrowIfNull(contextFactory); + + _ = builder + .Services.AddSingleton() + .AddSingleton(sp => + { + var startForm = sp.GetRequiredService(); + return contextFactory(sp, startForm); + }) + .AddSingleton(sp => sp.GetRequiredService()) + // Default WindowsForms services + .AddWindowsFormsLifetime(configure); + + return builder; + } +#endif +}