diff --git a/src/BlazorAdmin/Program.cs b/src/BlazorAdmin/Program.cs index a002838e4..b80094751 100644 --- a/src/BlazorAdmin/Program.cs +++ b/src/BlazorAdmin/Program.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Net.Http; using System.Threading.Tasks; using BlazorAdmin.Services; @@ -20,9 +20,8 @@ public static async Task Main(string[] args) var builder = WebAssemblyHostBuilder.CreateDefault(args); builder.RootComponents.Add("#admin"); - var baseUrlConfig = new BaseUrlConfiguration(); - builder.Configuration.Bind(BaseUrlConfiguration.CONFIG_NAME, baseUrlConfig); - builder.Services.AddScoped(sp => baseUrlConfig); + var configSection = builder.Configuration.GetRequiredSection(BaseUrlConfiguration.CONFIG_NAME); + builder.Services.Configure(configSection); builder.Services.AddScoped(sp => new HttpClient() { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) }); @@ -37,7 +36,7 @@ public static async Task Main(string[] args) builder.Services.AddBlazorServices(); - builder.Logging.AddConfiguration(builder.Configuration.GetSection("Logging")); + builder.Logging.AddConfiguration(builder.Configuration.GetRequiredSection("Logging")); await ClearLocalStorageCache(builder.Services); diff --git a/src/BlazorAdmin/Services/CatalogLookupDataService.cs b/src/BlazorAdmin/Services/CatalogLookupDataService.cs index 4927d6fd3..fa06b1608 100644 --- a/src/BlazorAdmin/Services/CatalogLookupDataService.cs +++ b/src/BlazorAdmin/Services/CatalogLookupDataService.cs @@ -9,6 +9,7 @@ using BlazorShared.Interfaces; using BlazorShared.Models; using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; namespace BlazorAdmin.Services; @@ -23,12 +24,12 @@ public class CatalogLookupDataService private readonly string _apiUrl; public CatalogLookupDataService(HttpClient httpClient, - BaseUrlConfiguration baseUrlConfiguration, + IOptions baseUrlConfiguration, ILogger> logger) { _httpClient = httpClient; _logger = logger; - _apiUrl = baseUrlConfiguration.ApiBase; + _apiUrl = baseUrlConfiguration.Value.ApiBase; } public async Task> List() diff --git a/src/BlazorAdmin/Services/HttpService.cs b/src/BlazorAdmin/Services/HttpService.cs index 574992126..2362e9e5d 100644 --- a/src/BlazorAdmin/Services/HttpService.cs +++ b/src/BlazorAdmin/Services/HttpService.cs @@ -4,6 +4,7 @@ using System.Threading.Tasks; using BlazorShared; using BlazorShared.Models; +using Microsoft.Extensions.Options; namespace BlazorAdmin.Services; @@ -14,11 +15,11 @@ public class HttpService private readonly string _apiUrl; - public HttpService(HttpClient httpClient, BaseUrlConfiguration baseUrlConfiguration, ToastService toastService) + public HttpService(HttpClient httpClient, IOptions baseUrlConfiguration, ToastService toastService) { _httpClient = httpClient; _toastService = toastService; - _apiUrl = baseUrlConfiguration.ApiBase; + _apiUrl = baseUrlConfiguration.Value.ApiBase; } public async Task HttpGet(string uri) diff --git a/src/PublicApi/Startup.cs b/src/PublicApi/Startup.cs index 9296c9564..2a52fd586 100644 --- a/src/PublicApi/Startup.cs +++ b/src/PublicApi/Startup.cs @@ -1,4 +1,4 @@ -using System.Collections.Generic; +using System.Collections.Generic; using System.Text; using AutoMapper; using BlazorShared; @@ -96,8 +96,9 @@ public void ConfigureServices(IServiceCollection services) services.AddScoped(typeof(IAppLogger<>), typeof(LoggerAdapter<>)); services.AddScoped(); - var baseUrlConfig = new BaseUrlConfiguration(); - Configuration.Bind(BaseUrlConfiguration.CONFIG_NAME, baseUrlConfig); + var configSection = Configuration.GetRequiredSection(BaseUrlConfiguration.CONFIG_NAME); + services.Configure(configSection); + var baseUrlConfig = configSection.Get(); services.AddMemoryCache(); diff --git a/src/Web/HealthChecks/ApiHealthCheck.cs b/src/Web/HealthChecks/ApiHealthCheck.cs index 3a330a62a..49f6634e5 100644 --- a/src/Web/HealthChecks/ApiHealthCheck.cs +++ b/src/Web/HealthChecks/ApiHealthCheck.cs @@ -3,6 +3,7 @@ using System.Threading.Tasks; using BlazorShared; using Microsoft.Extensions.Diagnostics.HealthChecks; +using Microsoft.Extensions.Options; namespace Microsoft.eShopWeb.Web.HealthChecks; @@ -10,9 +11,9 @@ public class ApiHealthCheck : IHealthCheck { private readonly BaseUrlConfiguration _baseUrlConfiguration; - public ApiHealthCheck(BaseUrlConfiguration baseUrlConfiguration) + public ApiHealthCheck(IOptions baseUrlConfiguration) { - _baseUrlConfiguration = baseUrlConfiguration; + _baseUrlConfiguration = baseUrlConfiguration.Value; } public async Task CheckHealthAsync( diff --git a/src/Web/Startup.cs b/src/Web/Startup.cs index c3ea9eac6..d854a6ffd 100644 --- a/src/Web/Startup.cs +++ b/src/Web/Startup.cs @@ -148,10 +148,10 @@ public void ConfigureServices(IServiceCollection services) config.Path = "/allservices"; }); + var configSection = Configuration.GetRequiredSection(BaseUrlConfiguration.CONFIG_NAME); + services.Configure(configSection); + var baseUrlConfig = configSection.Get(); - var baseUrlConfig = new BaseUrlConfiguration(); - Configuration.Bind(BaseUrlConfiguration.CONFIG_NAME, baseUrlConfig); - services.AddScoped(sp => baseUrlConfig); // Blazor Admin Required Services for Prerendering services.AddScoped(s => new HttpClient {