|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using Microsoft.Extensions.Configuration; |
| 7 | +using Microsoft.Extensions.DependencyInjection; |
| 8 | +using Microsoft.Extensions.Hosting; |
| 9 | + |
| 10 | +namespace Microsoft.AspNetCore.Builder |
| 11 | +{ |
| 12 | + /// <summary> |
| 13 | + /// A non-buildable <see cref="IHostBuilder"/> for <see cref="WebApplicationBuilder"/>. |
| 14 | + /// Use <see cref="WebApplicationBuilder.Build"/> to build the <see cref="WebApplicationBuilder"/>. |
| 15 | + /// </summary> |
| 16 | + public sealed class ConfigureHostBuilder : IHostBuilder |
| 17 | + { |
| 18 | + private Action<IHostBuilder>? _operations; |
| 19 | + |
| 20 | + /// <inheritdoc /> |
| 21 | + public IDictionary<object, object> Properties { get; } = new Dictionary<object, object>(); |
| 22 | + |
| 23 | + internal Configuration Configuration => _configuration; |
| 24 | + |
| 25 | + private readonly IConfigurationBuilder _hostConfiguration = new ConfigurationBuilder(); |
| 26 | + |
| 27 | + private readonly WebHostEnvironment _environment; |
| 28 | + private readonly Configuration _configuration; |
| 29 | + private readonly IServiceCollection _services; |
| 30 | + |
| 31 | + internal ConfigureHostBuilder(Configuration configuration, WebHostEnvironment environment, IServiceCollection services, string[]? args) |
| 32 | + { |
| 33 | + _configuration = configuration; |
| 34 | + _environment = environment; |
| 35 | + _services = services; |
| 36 | + |
| 37 | + this.ConfigureDefaults(args); |
| 38 | + } |
| 39 | + |
| 40 | + IHost IHostBuilder.Build() |
| 41 | + { |
| 42 | + throw new NotSupportedException($"Call {nameof(WebApplicationBuilder)}.{nameof(WebApplicationBuilder.Build)}() instead."); |
| 43 | + } |
| 44 | + |
| 45 | + /// <inheritdoc /> |
| 46 | + public IHostBuilder ConfigureAppConfiguration(Action<HostBuilderContext, IConfigurationBuilder> configureDelegate) |
| 47 | + { |
| 48 | + _operations += b => b.ConfigureAppConfiguration(configureDelegate); |
| 49 | + return this; |
| 50 | + } |
| 51 | + |
| 52 | + /// <inheritdoc /> |
| 53 | + public IHostBuilder ConfigureContainer<TContainerBuilder>(Action<HostBuilderContext, TContainerBuilder> configureDelegate) |
| 54 | + { |
| 55 | + _operations += b => b.ConfigureContainer(configureDelegate); |
| 56 | + return this; |
| 57 | + } |
| 58 | + |
| 59 | + /// <inheritdoc /> |
| 60 | + public IHostBuilder ConfigureHostConfiguration(Action<IConfigurationBuilder> configureDelegate) |
| 61 | + { |
| 62 | + // HACK: We need to evaluate the host configuration as they are changes so that we have an accurate view of the world |
| 63 | + configureDelegate(_hostConfiguration); |
| 64 | + |
| 65 | + var config = _hostConfiguration.Build(); |
| 66 | + |
| 67 | + _environment.ApplicationName = config[HostDefaults.ApplicationKey] ?? _environment.ApplicationName; |
| 68 | + _environment.ContentRootPath = config[HostDefaults.ContentRootKey] ?? _environment.ContentRootPath; |
| 69 | + _environment.EnvironmentName = config[HostDefaults.EnvironmentKey] ?? _environment.EnvironmentName; |
| 70 | + _environment.ResolveFileProviders(config); |
| 71 | + Configuration.ChangeBasePath(_environment.ContentRootPath); |
| 72 | + |
| 73 | + _operations += b => b.ConfigureHostConfiguration(configureDelegate); |
| 74 | + return this; |
| 75 | + } |
| 76 | + |
| 77 | + /// <inheritdoc /> |
| 78 | + public IHostBuilder ConfigureServices(Action<HostBuilderContext, IServiceCollection> configureDelegate) |
| 79 | + { |
| 80 | + // Run these immediately so that they are observable by the imperative code |
| 81 | + configureDelegate(new HostBuilderContext(Properties) |
| 82 | + { |
| 83 | + Configuration = Configuration, |
| 84 | + HostingEnvironment = _environment |
| 85 | + }, |
| 86 | + _services); |
| 87 | + |
| 88 | + return this; |
| 89 | + } |
| 90 | + |
| 91 | + /// <inheritdoc /> |
| 92 | + public IHostBuilder UseServiceProviderFactory<TContainerBuilder>(IServiceProviderFactory<TContainerBuilder> factory) where TContainerBuilder : notnull |
| 93 | + { |
| 94 | + _operations += b => b.UseServiceProviderFactory(factory); |
| 95 | + return this; |
| 96 | + } |
| 97 | + |
| 98 | + /// <inheritdoc /> |
| 99 | + public IHostBuilder UseServiceProviderFactory<TContainerBuilder>(Func<HostBuilderContext, IServiceProviderFactory<TContainerBuilder>> factory) where TContainerBuilder : notnull |
| 100 | + { |
| 101 | + _operations += b => b.UseServiceProviderFactory(factory); |
| 102 | + return this; |
| 103 | + } |
| 104 | + |
| 105 | + /// <inheritdoc /> |
| 106 | + public void ExecuteActions(IHostBuilder hostBuilder) |
| 107 | + { |
| 108 | + _operations?.Invoke(hostBuilder); |
| 109 | + } |
| 110 | + } |
| 111 | +} |
0 commit comments