| | | 1 | | using Microsoft.Extensions.Configuration; |
| | | 2 | | using Microsoft.Extensions.DependencyInjection; |
| | | 3 | | using Microsoft.Extensions.Hosting; |
| | | 4 | | using Microsoft.Extensions.Logging; |
| | | 5 | | |
| | | 6 | | using NexusLabs.Needlr.Injection; |
| | | 7 | | |
| | | 8 | | using System.Reflection; |
| | | 9 | | |
| | | 10 | | namespace NexusLabs.Needlr.Hosting; |
| | | 11 | | |
| | | 12 | | /// <summary> |
| | | 13 | | /// Factory for creating <see cref="IHost"/> instances with Needlr configuration. |
| | | 14 | | /// </summary> |
| | | 15 | | [DoNotAutoRegister] |
| | 11 | 16 | | public sealed class HostFactory( |
| | 11 | 17 | | IServiceProviderBuilder _serviceProviderBuilder, |
| | 11 | 18 | | IServiceCollectionPopulator _serviceCollectionPopulator, |
| | 11 | 19 | | IPluginFactory _pluginFactory) : |
| | | 20 | | IHostFactory |
| | | 21 | | { |
| | | 22 | | /// <inheritdoc /> |
| | | 23 | | public IHost Create( |
| | | 24 | | CreateHostOptions options, |
| | | 25 | | Func<HostApplicationBuilder> createHostApplicationBuilderCallback) |
| | | 26 | | { |
| | 11 | 27 | | options.Logger.LogInformation("Creating host application builder..."); |
| | 11 | 28 | | var hostApplicationBuilder = createHostApplicationBuilderCallback.Invoke(); |
| | | 29 | | |
| | 11 | 30 | | var candidateAssemblies = _serviceProviderBuilder.GetCandidateAssemblies(); |
| | | 31 | | |
| | 11 | 32 | | ConfigureServices( |
| | 11 | 33 | | _serviceCollectionPopulator, |
| | 11 | 34 | | hostApplicationBuilder, |
| | 11 | 35 | | _pluginFactory, |
| | 11 | 36 | | options.Logger, |
| | 11 | 37 | | candidateAssemblies, |
| | 11 | 38 | | options.PrePluginRegistrationCallbacks, |
| | 11 | 39 | | options.PostPluginRegistrationCallbacks); |
| | | 40 | | |
| | 11 | 41 | | options.Logger.LogInformation("Building host..."); |
| | 11 | 42 | | var host = hostApplicationBuilder.Build(); |
| | | 43 | | |
| | 11 | 44 | | ConfigureHost( |
| | 11 | 45 | | _serviceProviderBuilder, |
| | 11 | 46 | | _pluginFactory, |
| | 11 | 47 | | host, |
| | 11 | 48 | | options.Logger, |
| | 11 | 49 | | candidateAssemblies); |
| | | 50 | | |
| | 11 | 51 | | options.Logger.LogInformation("Host created successfully."); |
| | 11 | 52 | | return host; |
| | | 53 | | } |
| | | 54 | | |
| | | 55 | | private static void ConfigureHost( |
| | | 56 | | IServiceProviderBuilder serviceProviderBuilder, |
| | | 57 | | IPluginFactory pluginFactory, |
| | | 58 | | IHost host, |
| | | 59 | | ILogger logger, |
| | | 60 | | IReadOnlyList<Assembly> assembliesToLoadFrom) |
| | | 61 | | { |
| | 11 | 62 | | ArgumentNullException.ThrowIfNull(serviceProviderBuilder); |
| | 11 | 63 | | ArgumentNullException.ThrowIfNull(pluginFactory); |
| | 11 | 64 | | ArgumentNullException.ThrowIfNull(host); |
| | 11 | 65 | | ArgumentNullException.ThrowIfNull(logger); |
| | 11 | 66 | | ArgumentNullException.ThrowIfNull(assembliesToLoadFrom); |
| | | 67 | | |
| | 11 | 68 | | logger.LogInformation("Configuring host..."); |
| | | 69 | | |
| | 11 | 70 | | RegisterHostPlugins( |
| | 11 | 71 | | host, |
| | 11 | 72 | | pluginFactory, |
| | 11 | 73 | | assembliesToLoadFrom, |
| | 11 | 74 | | logger); |
| | | 75 | | |
| | 11 | 76 | | var configuration = host.Services.GetRequiredService<IConfiguration>(); |
| | 11 | 77 | | serviceProviderBuilder.ConfigurePostBuildServiceCollectionPlugins( |
| | 11 | 78 | | host.Services, |
| | 11 | 79 | | configuration); |
| | | 80 | | |
| | 11 | 81 | | logger.LogInformation("Host configured successfully."); |
| | 11 | 82 | | } |
| | | 83 | | |
| | | 84 | | private static void RegisterHostPlugins( |
| | | 85 | | IHost host, |
| | | 86 | | IPluginFactory pluginFactory, |
| | | 87 | | IReadOnlyList<Assembly> assembliesToLoadFrom, |
| | | 88 | | ILogger logger) |
| | | 89 | | { |
| | 11 | 90 | | ArgumentNullException.ThrowIfNull(host); |
| | 11 | 91 | | ArgumentNullException.ThrowIfNull(pluginFactory); |
| | 11 | 92 | | ArgumentNullException.ThrowIfNull(assembliesToLoadFrom); |
| | 11 | 93 | | ArgumentNullException.ThrowIfNull(logger); |
| | | 94 | | |
| | 11 | 95 | | logger.LogInformation("Configuring plugins for the host..."); |
| | | 96 | | |
| | 11 | 97 | | HostPluginOptions options = new( |
| | 11 | 98 | | host, |
| | 11 | 99 | | assembliesToLoadFrom, |
| | 11 | 100 | | pluginFactory); |
| | 22 | 101 | | foreach (var plugin in pluginFactory.CreatePluginsFromAssemblies<IHostPlugin>( |
| | 11 | 102 | | assembliesToLoadFrom)) |
| | | 103 | | { |
| | 0 | 104 | | logger.LogInformation("Configuring host plugin '{PluginName}'...", plugin.GetType().Name); |
| | 0 | 105 | | plugin.Configure(options); |
| | | 106 | | } |
| | | 107 | | |
| | 11 | 108 | | logger.LogInformation("Configured plugins for the host."); |
| | 11 | 109 | | } |
| | | 110 | | |
| | | 111 | | private static void RegisterHostApplicationBuilderPlugins( |
| | | 112 | | HostApplicationBuilder builder, |
| | | 113 | | IPluginFactory pluginFactory, |
| | | 114 | | IReadOnlyList<Assembly> assembliesToLoadFrom, |
| | | 115 | | ILogger logger) |
| | | 116 | | { |
| | 11 | 117 | | ArgumentNullException.ThrowIfNull(builder); |
| | 11 | 118 | | ArgumentNullException.ThrowIfNull(pluginFactory); |
| | 11 | 119 | | ArgumentNullException.ThrowIfNull(assembliesToLoadFrom); |
| | 11 | 120 | | ArgumentNullException.ThrowIfNull(logger); |
| | | 121 | | |
| | 11 | 122 | | logger.LogInformation("Configuring plugins for the host application builder..."); |
| | | 123 | | |
| | 11 | 124 | | HostApplicationBuilderPluginOptions options = new( |
| | 11 | 125 | | builder, |
| | 11 | 126 | | assembliesToLoadFrom, |
| | 11 | 127 | | logger, |
| | 11 | 128 | | pluginFactory); |
| | 22 | 129 | | foreach (var plugin in pluginFactory.CreatePluginsFromAssemblies<IHostApplicationBuilderPlugin>( |
| | 11 | 130 | | assembliesToLoadFrom)) |
| | | 131 | | { |
| | 0 | 132 | | logger.LogInformation("Configuring host application builder plugin '{PluginName}'...", plugin.GetType().Name |
| | 0 | 133 | | plugin.Configure(options); |
| | | 134 | | } |
| | | 135 | | |
| | 11 | 136 | | logger.LogInformation("Configured plugins for the host application builder."); |
| | 11 | 137 | | } |
| | | 138 | | |
| | | 139 | | private static void ConfigureServices( |
| | | 140 | | IServiceCollectionPopulator serviceCollectionPopulator, |
| | | 141 | | HostApplicationBuilder builder, |
| | | 142 | | IPluginFactory pluginFactory, |
| | | 143 | | ILogger logger, |
| | | 144 | | IReadOnlyList<Assembly> assembliesToLoadFrom, |
| | | 145 | | IReadOnlyList<Action<IServiceCollection>> prePluginRegistrationCallbacks, |
| | | 146 | | IReadOnlyList<Action<IServiceCollection>> postPluginRegistrationCallbacks) |
| | | 147 | | { |
| | 11 | 148 | | ArgumentNullException.ThrowIfNull(serviceCollectionPopulator); |
| | 11 | 149 | | ArgumentNullException.ThrowIfNull(builder); |
| | 11 | 150 | | ArgumentNullException.ThrowIfNull(pluginFactory); |
| | 11 | 151 | | ArgumentNullException.ThrowIfNull(logger); |
| | 11 | 152 | | ArgumentNullException.ThrowIfNull(assembliesToLoadFrom); |
| | 11 | 153 | | ArgumentNullException.ThrowIfNull(prePluginRegistrationCallbacks); |
| | 11 | 154 | | ArgumentNullException.ThrowIfNull(postPluginRegistrationCallbacks); |
| | | 155 | | |
| | 11 | 156 | | logger.LogInformation("Configuring host services..."); |
| | | 157 | | |
| | 24 | 158 | | foreach (var callback in prePluginRegistrationCallbacks) |
| | | 159 | | { |
| | 1 | 160 | | logger.LogInformation("Executing pre-plugin registration callback..."); |
| | 1 | 161 | | callback.Invoke(builder.Services); |
| | | 162 | | } |
| | | 163 | | |
| | 11 | 164 | | RegisterHostApplicationBuilderPlugins( |
| | 11 | 165 | | builder, |
| | 11 | 166 | | pluginFactory, |
| | 11 | 167 | | assembliesToLoadFrom, |
| | 11 | 168 | | logger); |
| | | 169 | | |
| | 11 | 170 | | logger.LogInformation("Registering services to service collection..."); |
| | 11 | 171 | | serviceCollectionPopulator.RegisterToServiceCollection( |
| | 11 | 172 | | builder.Services, |
| | 11 | 173 | | builder.Configuration, |
| | 11 | 174 | | assembliesToLoadFrom); |
| | 11 | 175 | | logger.LogInformation("Registered services to service collection."); |
| | | 176 | | |
| | 24 | 177 | | foreach (var callback in postPluginRegistrationCallbacks) |
| | | 178 | | { |
| | 1 | 179 | | logger.LogInformation("Executing post-plugin registration callback..."); |
| | 1 | 180 | | callback.Invoke(builder.Services); |
| | | 181 | | } |
| | | 182 | | |
| | 11 | 183 | | logger.LogInformation("Configured host services successfully."); |
| | 11 | 184 | | } |
| | | 185 | | } |