Skip to content

Commit

Permalink
Merge pull request #127 from rob-foulkrod/appConfig-reapply
Browse files Browse the repository at this point in the history
Reapply Azure App Configuration client code for AZ-400 Dynamic Configuration Lab
  • Loading branch information
yashints authored Mar 14, 2024
2 parents 2b1af74 + 9906f9e commit 09e905c
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 10 deletions.
3 changes: 3 additions & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@
<PackageVersion Include="Microsoft.Web.LibraryManager.Build" Version="2.1.175" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.InMemory" Version="8.0.2" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.2" />
<PackageVersion Include="Microsoft.FeatureManagement.AspNetCore" Version="4.0.0-preview2" />
<PackageVersion Include="Microsoft.Azure.AppConfiguration.AspNetCore" Version="7.1.0" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.2">

<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageVersion>
Expand Down
6 changes: 4 additions & 2 deletions src/Web/Pages/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
}
<section class="esh-catalog-hero">
<div class="container">
<img class="esh-catalog-title" src="~/images/main_banner_text.png" />
<feature name="SalesWeekend">
<img class="esh-catalog-title" src="~/images/main_banner_text.png" />
</feature>
</div>
</section>
<section class="esh-catalog-filters">
Expand Down Expand Up @@ -39,7 +41,7 @@
else
{
<div class="esh-catalog-items row">
THERE ARE NO RESULTS THAT MATCH YOUR SEARCH
@Model.SettingsModel.NoResultsMessage
</div>
}
</div>
5 changes: 4 additions & 1 deletion src/Web/Pages/Index.cshtml.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.eShopWeb.Web.Services;
using Microsoft.eShopWeb.Web.ViewModels;
using Microsoft.Extensions.Options;

namespace Microsoft.eShopWeb.Web.Pages;

public class IndexModel : PageModel
{
private readonly ICatalogViewModelService _catalogViewModelService;
public SettingsViewModel SettingsModel { get; }

public IndexModel(ICatalogViewModelService catalogViewModelService)
public IndexModel(ICatalogViewModelService catalogViewModelService, IOptionsSnapshot<SettingsViewModel> options)
{
_catalogViewModelService = catalogViewModelService;
SettingsModel = options.Value;
}

public required CatalogIndexViewModel CatalogModel { get; set; } = new CatalogIndexViewModel();
Expand Down
1 change: 1 addition & 0 deletions src/Web/Pages/_ViewImports.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
@using Microsoft.eShopWeb.Infrastructure.Identity
@namespace Microsoft.eShopWeb.Web.Pages
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@addTagHelper *, Microsoft.FeatureManagement.AspNetCore
49 changes: 47 additions & 2 deletions src/Web/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,20 @@
using Microsoft.eShopWeb.Web.Configuration;
using Microsoft.eShopWeb.Web.HealthChecks;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Microsoft.eShopWeb.Web.Pages;
using Microsoft.FeatureManagement;
using Microsoft.IdentityModel.Tokens;

var builder = WebApplication.CreateBuilder(args);
builder.Logging.AddConsole();

if (builder.Environment.IsDevelopment() || builder.Environment.EnvironmentName == "Docker"){
if (builder.Environment.IsDevelopment() || builder.Environment.EnvironmentName == "Docker")
{
// Configure SQL Server (local)
Microsoft.eShopWeb.Infrastructure.Dependencies.ConfigureServices(builder.Configuration, builder.Services);
}
else{
else
{
// Configure SQL Server (prod)
var credential = new ChainedTokenCredential(new AzureDeveloperCliCredential(), new DefaultAzureCredential());
builder.Configuration.AddAzureKeyVault(new Uri(builder.Configuration["AZURE_KEY_VAULT_ENDPOINT"] ?? ""), credential);
Expand Down Expand Up @@ -93,6 +98,40 @@
config.Path = "/allservices";
});

// Bind configuration "eShopWeb:Settings" section to the Settings object
builder.Services.Configure<SettingsViewModel>(builder.Configuration.GetSection("eShopWeb:Settings"));
// Initialize useAppConfig parameter
var useAppConfig = false;
Boolean.TryParse(builder.Configuration["UseAppConfig"], out useAppConfig);
// Add Azure App Configuration middleware to the container of services.
builder.Services.AddAzureAppConfiguration();
builder.Services.AddFeatureManagement();
// Load configuration from Azure App Configuration
if (useAppConfig)
{
builder.Configuration.AddAzureAppConfiguration(options =>
{
var appConfigEndpoint = builder.Configuration["AppConfigEndpoint"];

if (String.IsNullOrEmpty(appConfigEndpoint))
{
throw new Exception("AppConfigEndpoint is not set in the configuration. Please set AppConfigEndpoint in the configuration.");
}

options.Connect(new Uri(appConfigEndpoint), new DefaultAzureCredential())
.ConfigureRefresh(refresh =>
{
// Default cache expiration is 30 seconds
refresh.Register("eShopWeb:Settings:NoResultsMessage").SetCacheExpiration(TimeSpan.FromSeconds(10));
})
.UseFeatureFlags(featureFlagOptions =>
{
// Default cache expiration is 30 seconds
featureFlagOptions.CacheExpirationInterval = TimeSpan.FromSeconds(10);
});
});
}

// blazor configuration
var configSection = builder.Configuration.GetRequiredSection(BaseUrlConfiguration.CONFIG_NAME);
builder.Services.Configure<BaseUrlConfiguration>(configSection);
Expand All @@ -115,6 +154,12 @@

var app = builder.Build();

if (useAppConfig)
{
// Use Azure App Configuration middleware for dynamic configuration refresh.
app.UseAzureAppConfiguration();
}

app.Logger.LogInformation("App created...");

app.Logger.LogInformation("Seeding Database...");
Expand Down
5 changes: 4 additions & 1 deletion src/Web/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@
"launchBrowser": true,
"inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
"ASPNETCORE_ENVIRONMENT": "Development",
"AZURE_TENANT_ID": "{azure-tenant-id}",
"AZURE_CLIENT_ID": "{azure-client-id}",
"AZURE_CLIENT_SECRET": "{azure-client-secret}"
},
"applicationUrl": "https://localhost:5001;http://localhost:5000"
},
Expand Down
8 changes: 5 additions & 3 deletions src/Web/Web.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<PropertyGroup>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<RootNamespace>Microsoft.eShopWeb.Web</RootNamespace>
Expand Down Expand Up @@ -29,11 +29,13 @@
<PackageReference Include="Microsoft.AspNetCore.Identity.UI" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" />
<PackageReference Include="Microsoft.Azure.AppConfiguration.AspNetCore" />
<PackageReference Include="Microsoft.FeatureManagement.AspNetCore" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="System.IdentityModel.Tokens.Jwt" />
</PackageReference>
</ItemGroup>
<ItemGroup>
<Folder Include="wwwroot\fonts\" />
Expand Down
10 changes: 9 additions & 1 deletion src/Web/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,13 @@
"System": "Warning"
},
"AllowedHosts": "*"
}
},

"eShopWeb": {
"Settings": {
"NoResultsMessage": "THERE ARE NO RESULTS THAT MATCH YOUR SEARCH"
}
},
"UseAppConfig": false,
"AppConfigEndpoint": ""
}

0 comments on commit 09e905c

Please sign in to comment.