| | | 1 | | using Microsoft.Extensions.Hosting; |
| | | 2 | | |
| | | 3 | | namespace NexusLabs.Needlr.Hosting; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Extension methods for <see cref="IHostFactory"/>. |
| | | 7 | | /// </summary> |
| | | 8 | | public static class IHostFactoryExtensions |
| | | 9 | | { |
| | | 10 | | /// <summary> |
| | | 11 | | /// Creates an <see cref="IHost"/> using the specified options. |
| | | 12 | | /// </summary> |
| | | 13 | | /// <param name="factory">The host factory.</param> |
| | | 14 | | /// <param name="options">The options for creating the host.</param> |
| | | 15 | | /// <returns>The configured <see cref="IHost"/>.</returns> |
| | | 16 | | /// <exception cref="ArgumentNullException">Thrown when <paramref name="factory"/> or <paramref name="options"/> is |
| | | 17 | | public static IHost Create( |
| | | 18 | | this IHostFactory factory, |
| | | 19 | | CreateHostOptions options) |
| | | 20 | | { |
| | 0 | 21 | | ArgumentNullException.ThrowIfNull(factory); |
| | 0 | 22 | | ArgumentNullException.ThrowIfNull(options); |
| | | 23 | | |
| | 0 | 24 | | static void EmptyConfig(HostApplicationBuilder _, CreateHostOptions __) { } |
| | 0 | 25 | | return factory.Create(options, EmptyConfig); |
| | | 26 | | } |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// Creates an <see cref="IHost"/> using the specified options and configuration callback. |
| | | 30 | | /// </summary> |
| | | 31 | | /// <param name="factory">The host factory.</param> |
| | | 32 | | /// <param name="options">The options for creating the host.</param> |
| | | 33 | | /// <param name="configureCallback">Optional callback to configure the <see cref="HostApplicationBuilder"/>.</param> |
| | | 34 | | /// <returns>The configured <see cref="IHost"/>.</returns> |
| | | 35 | | /// <exception cref="ArgumentNullException">Thrown when <paramref name="factory"/> or <paramref name="options"/> is |
| | | 36 | | public static IHost Create( |
| | | 37 | | this IHostFactory factory, |
| | | 38 | | CreateHostOptions options, |
| | | 39 | | Action<HostApplicationBuilder, CreateHostOptions>? configureCallback) |
| | | 40 | | { |
| | 11 | 41 | | ArgumentNullException.ThrowIfNull(factory); |
| | 11 | 42 | | ArgumentNullException.ThrowIfNull(options); |
| | | 43 | | |
| | | 44 | | HostApplicationBuilder Factory() |
| | | 45 | | { |
| | 11 | 46 | | var hostApplicationBuilder = Host.CreateApplicationBuilder(options.Settings); |
| | 11 | 47 | | configureCallback?.Invoke(hostApplicationBuilder, options); |
| | 11 | 48 | | return hostApplicationBuilder; |
| | | 49 | | } |
| | | 50 | | |
| | 11 | 51 | | return factory.Create(options, Factory); |
| | | 52 | | } |
| | | 53 | | } |