Skip to content

Commit

Permalink
use GetRequiredSection and Service to inject options (dotnet-architec…
Browse files Browse the repository at this point in the history
  • Loading branch information
michelcedric authored Dec 1, 2021
1 parent 13fed89 commit 3a81dcf
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 17 deletions.
9 changes: 4 additions & 5 deletions src/BlazorAdmin/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Net.Http;
using System.Threading.Tasks;
using BlazorAdmin.Services;
Expand All @@ -20,9 +20,8 @@ public static async Task Main(string[] args)
var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add<App>("#admin");

var baseUrlConfig = new BaseUrlConfiguration();
builder.Configuration.Bind(BaseUrlConfiguration.CONFIG_NAME, baseUrlConfig);
builder.Services.AddScoped<BaseUrlConfiguration>(sp => baseUrlConfig);
var configSection = builder.Configuration.GetRequiredSection(BaseUrlConfiguration.CONFIG_NAME);
builder.Services.Configure<BaseUrlConfiguration>(configSection);

builder.Services.AddScoped(sp => new HttpClient() { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });

Expand All @@ -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);

Expand Down
5 changes: 3 additions & 2 deletions src/BlazorAdmin/Services/CatalogLookupDataService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using BlazorShared.Interfaces;
using BlazorShared.Models;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

namespace BlazorAdmin.Services;

Expand All @@ -23,12 +24,12 @@ public class CatalogLookupDataService<TLookupData, TReponse>
private readonly string _apiUrl;

public CatalogLookupDataService(HttpClient httpClient,
BaseUrlConfiguration baseUrlConfiguration,
IOptions<BaseUrlConfiguration> baseUrlConfiguration,
ILogger<CatalogLookupDataService<TLookupData, TReponse>> logger)
{
_httpClient = httpClient;
_logger = logger;
_apiUrl = baseUrlConfiguration.ApiBase;
_apiUrl = baseUrlConfiguration.Value.ApiBase;
}

public async Task<List<TLookupData>> List()
Expand Down
5 changes: 3 additions & 2 deletions src/BlazorAdmin/Services/HttpService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Threading.Tasks;
using BlazorShared;
using BlazorShared.Models;
using Microsoft.Extensions.Options;

namespace BlazorAdmin.Services;

Expand All @@ -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> baseUrlConfiguration, ToastService toastService)
{
_httpClient = httpClient;
_toastService = toastService;
_apiUrl = baseUrlConfiguration.ApiBase;
_apiUrl = baseUrlConfiguration.Value.ApiBase;
}

public async Task<T> HttpGet<T>(string uri)
Expand Down
7 changes: 4 additions & 3 deletions src/PublicApi/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Collections.Generic;
using System.Collections.Generic;
using System.Text;
using AutoMapper;
using BlazorShared;
Expand Down Expand Up @@ -96,8 +96,9 @@ public void ConfigureServices(IServiceCollection services)
services.AddScoped(typeof(IAppLogger<>), typeof(LoggerAdapter<>));
services.AddScoped<ITokenClaimsService, IdentityTokenClaimService>();

var baseUrlConfig = new BaseUrlConfiguration();
Configuration.Bind(BaseUrlConfiguration.CONFIG_NAME, baseUrlConfig);
var configSection = Configuration.GetRequiredSection(BaseUrlConfiguration.CONFIG_NAME);
services.Configure<BaseUrlConfiguration>(configSection);
var baseUrlConfig = configSection.Get<BaseUrlConfiguration>();

services.AddMemoryCache();

Expand Down
5 changes: 3 additions & 2 deletions src/Web/HealthChecks/ApiHealthCheck.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@
using System.Threading.Tasks;
using BlazorShared;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Microsoft.Extensions.Options;

namespace Microsoft.eShopWeb.Web.HealthChecks;

public class ApiHealthCheck : IHealthCheck
{
private readonly BaseUrlConfiguration _baseUrlConfiguration;

public ApiHealthCheck(BaseUrlConfiguration baseUrlConfiguration)
public ApiHealthCheck(IOptions<BaseUrlConfiguration> baseUrlConfiguration)
{
_baseUrlConfiguration = baseUrlConfiguration;
_baseUrlConfiguration = baseUrlConfiguration.Value;
}

public async Task<HealthCheckResult> CheckHealthAsync(
Expand Down
6 changes: 3 additions & 3 deletions src/Web/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,10 @@ public void ConfigureServices(IServiceCollection services)
config.Path = "/allservices";
});

var configSection = Configuration.GetRequiredSection(BaseUrlConfiguration.CONFIG_NAME);
services.Configure<BaseUrlConfiguration>(configSection);
var baseUrlConfig = configSection.Get<BaseUrlConfiguration>();

var baseUrlConfig = new BaseUrlConfiguration();
Configuration.Bind(BaseUrlConfiguration.CONFIG_NAME, baseUrlConfig);
services.AddScoped<BaseUrlConfiguration>(sp => baseUrlConfig);
// Blazor Admin Required Services for Prerendering
services.AddScoped<HttpClient>(s => new HttpClient
{
Expand Down

0 comments on commit 3a81dcf

Please sign in to comment.