| | | 1 | | using Microsoft.Extensions.Configuration; |
| | | 2 | | using Microsoft.Extensions.Hosting; |
| | | 3 | | using Microsoft.Extensions.Logging; |
| | | 4 | | |
| | | 5 | | using NexusLabs.Needlr.Injection; |
| | | 6 | | |
| | | 7 | | namespace NexusLabs.Needlr.Hosting; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Extension methods for configuring <see cref="HostSyringe"/> instances. |
| | | 11 | | /// Provides only host-specific configuration methods. |
| | | 12 | | /// </summary> |
| | | 13 | | /// <remarks> |
| | | 14 | | /// Remember to use base <see cref="Syringe"/> extension methods BEFORE transitioning to host mode. |
| | | 15 | | /// </remarks> |
| | | 16 | | /// <example> |
| | | 17 | | /// Complete host configuration: |
| | | 18 | | /// <code> |
| | | 19 | | /// var host = new Syringe() |
| | | 20 | | /// // Transition to host mode |
| | | 21 | | /// .ForHost() |
| | | 22 | | /// // Configure host-specific options |
| | | 23 | | /// .UsingOptions(() => CreateHostOptions.Default |
| | | 24 | | /// .UsingArgs(args) |
| | | 25 | | /// .UsingApplicationName("My Worker Service") |
| | | 26 | | /// .UsingStartupConsoleLogger()) |
| | | 27 | | /// .UsingConfigurationCallback((builder, options) => |
| | | 28 | | /// { |
| | | 29 | | /// // Configure the HostApplicationBuilder |
| | | 30 | | /// builder.Configuration.AddJsonFile("custom-settings.json", optional: true); |
| | | 31 | | /// builder.Services.AddSingleton<IMyCustomService, MyCustomService>(); |
| | | 32 | | /// }) |
| | | 33 | | /// .BuildHost(); |
| | | 34 | | /// |
| | | 35 | | /// await host.RunAsync(); |
| | | 36 | | /// </code> |
| | | 37 | | /// |
| | | 38 | | /// Minimal host: |
| | | 39 | | /// <code> |
| | | 40 | | /// var host = new Syringe() |
| | | 41 | | /// .ForHost() |
| | | 42 | | /// .BuildHost(); |
| | | 43 | | /// </code> |
| | | 44 | | /// </example> |
| | | 45 | | public static class HostSyringeExtensions |
| | | 46 | | { |
| | | 47 | | /// <summary> |
| | | 48 | | /// Configures the <see cref="HostSyringe"/> to use the specified host options factory. |
| | | 49 | | /// </summary> |
| | | 50 | | /// <param name="syringe">The <see cref="HostSyringe"/> to configure.</param> |
| | | 51 | | /// <param name="optionsFactory">The factory function for creating host options.</param> |
| | | 52 | | /// <returns>A new configured <see cref="HostSyringe"/> instance.</returns> |
| | | 53 | | /// <example> |
| | | 54 | | /// <code> |
| | | 55 | | /// var hostSyringe = syringe |
| | | 56 | | /// .ForHost() |
| | | 57 | | /// .UsingOptions(() => CreateHostOptions.Default |
| | | 58 | | /// .UsingArgs(args) |
| | | 59 | | /// .UsingApplicationName("My App") |
| | | 60 | | /// .UsingStartupConsoleLogger()); |
| | | 61 | | /// </code> |
| | | 62 | | /// </example> |
| | | 63 | | public static HostSyringe UsingOptions( |
| | | 64 | | this HostSyringe syringe, |
| | | 65 | | Func<CreateHostOptions> optionsFactory) |
| | | 66 | | { |
| | 7 | 67 | | ArgumentNullException.ThrowIfNull(syringe); |
| | 7 | 68 | | ArgumentNullException.ThrowIfNull(optionsFactory); |
| | | 69 | | |
| | 6 | 70 | | return syringe with { OptionsFactory = optionsFactory }; |
| | | 71 | | } |
| | | 72 | | |
| | | 73 | | /// <summary> |
| | | 74 | | /// Configures the <see cref="HostSyringe"/> to use <see cref="CreateHostOptions.Default"/> |
| | | 75 | | /// with the specified logger for startup diagnostics. |
| | | 76 | | /// Use this when you already have an <see cref="ILogger"/> instance and want to avoid |
| | | 77 | | /// the boilerplate of calling <c>UsingOptions(() => CreateHostOptions.Default.UsingLogger(logger))</c>. |
| | | 78 | | /// </summary> |
| | | 79 | | /// <param name="syringe">The <see cref="HostSyringe"/> to configure.</param> |
| | | 80 | | /// <param name="logger">The logger to use during startup.</param> |
| | | 81 | | /// <returns>A new configured <see cref="HostSyringe"/> instance.</returns> |
| | | 82 | | /// <exception cref="ArgumentNullException">Thrown when <paramref name="syringe"/> or <paramref name="logger"/> is n |
| | | 83 | | /// <example> |
| | | 84 | | /// <code> |
| | | 85 | | /// var host = new Syringe() |
| | | 86 | | /// .UsingSourceGen() |
| | | 87 | | /// .ForHost() |
| | | 88 | | /// .UsingOptions(bootstrapLogger) |
| | | 89 | | /// .BuildHost(); |
| | | 90 | | /// </code> |
| | | 91 | | /// </example> |
| | | 92 | | public static HostSyringe UsingOptions( |
| | | 93 | | this HostSyringe syringe, |
| | | 94 | | ILogger logger) |
| | | 95 | | { |
| | 4 | 96 | | ArgumentNullException.ThrowIfNull(syringe); |
| | 3 | 97 | | ArgumentNullException.ThrowIfNull(logger); |
| | | 98 | | |
| | 2 | 99 | | return syringe with { OptionsFactory = () => CreateHostOptions.Default.UsingLogger(logger) }; |
| | | 100 | | } |
| | | 101 | | |
| | | 102 | | /// <summary> |
| | | 103 | | /// Configures the <see cref="HostSyringe"/> to use the specified host factory. |
| | | 104 | | /// </summary> |
| | | 105 | | /// <param name="syringe">The <see cref="HostSyringe"/> to configure.</param> |
| | | 106 | | /// <param name="factory">The factory function for creating host factories.</param> |
| | | 107 | | /// <returns>A new configured <see cref="HostSyringe"/> instance.</returns> |
| | | 108 | | /// <example> |
| | | 109 | | /// <code> |
| | | 110 | | /// var hostSyringe = syringe |
| | | 111 | | /// .ForHost() |
| | | 112 | | /// .UsingHostFactory((serviceProviderBuilder, serviceCollectionPopulator) => |
| | | 113 | | /// { |
| | | 114 | | /// // Custom factory logic here |
| | | 115 | | /// return new CustomHostFactory(serviceProviderBuilder, serviceCollectionPopulator); |
| | | 116 | | /// }); |
| | | 117 | | /// </code> |
| | | 118 | | /// </example> |
| | | 119 | | public static HostSyringe UsingHostFactory( |
| | | 120 | | this HostSyringe syringe, |
| | | 121 | | Func<IServiceProviderBuilder, IServiceCollectionPopulator, IHostFactory> factory) |
| | | 122 | | { |
| | 1 | 123 | | ArgumentNullException.ThrowIfNull(syringe); |
| | 1 | 124 | | ArgumentNullException.ThrowIfNull(factory); |
| | | 125 | | |
| | 0 | 126 | | return syringe with { HostFactoryCreator = factory }; |
| | | 127 | | } |
| | | 128 | | |
| | | 129 | | /// <summary> |
| | | 130 | | /// Configures the <see cref="HostSyringe"/> to use a callback for configuring the <see cref="HostApplicationBuilder |
| | | 131 | | /// This allows for custom configuration of the builder, such as modifying the <see cref="ConfigurationBuilder"/> |
| | | 132 | | /// or adding additional services before the host is built. |
| | | 133 | | /// </summary> |
| | | 134 | | /// <param name="syringe">The <see cref="HostSyringe"/> to configure.</param> |
| | | 135 | | /// <param name="configureCallback">The callback to configure the <see cref="HostApplicationBuilder"/>.</param> |
| | | 136 | | /// <returns>A new configured <see cref="HostSyringe"/> instance.</returns> |
| | | 137 | | /// <example> |
| | | 138 | | /// <code> |
| | | 139 | | /// var hostSyringe = syringe |
| | | 140 | | /// .ForHost() |
| | | 141 | | /// .UsingConfigurationCallback((builder, options) => |
| | | 142 | | /// { |
| | | 143 | | /// // Add custom configuration sources |
| | | 144 | | /// builder.Configuration.AddJsonFile("appsettings.local.json", optional: true); |
| | | 145 | | /// builder.Configuration.AddEnvironmentVariables("MYAPP_"); |
| | | 146 | | /// |
| | | 147 | | /// // Register additional services that need to be available before plugin registration |
| | | 148 | | /// builder.Services.AddSingleton<ICustomConfigurationService, CustomConfigurationService>(); |
| | | 149 | | /// |
| | | 150 | | /// // Configure logging |
| | | 151 | | /// builder.Services.AddLogging(logging => |
| | | 152 | | /// { |
| | | 153 | | /// logging.AddConsole(); |
| | | 154 | | /// logging.SetMinimumLevel(LogLevel.Debug); |
| | | 155 | | /// }); |
| | | 156 | | /// }); |
| | | 157 | | /// </code> |
| | | 158 | | /// </example> |
| | | 159 | | public static HostSyringe UsingConfigurationCallback( |
| | | 160 | | this HostSyringe syringe, |
| | | 161 | | Action<HostApplicationBuilder, CreateHostOptions> configureCallback) |
| | | 162 | | { |
| | 5 | 163 | | ArgumentNullException.ThrowIfNull(syringe); |
| | 5 | 164 | | ArgumentNullException.ThrowIfNull(configureCallback); |
| | | 165 | | |
| | 4 | 166 | | return syringe with { ConfigureCallback = configureCallback }; |
| | | 167 | | } |
| | | 168 | | } |