Skip to content

Commit

Permalink
Merge pull request MicrosoftLearning#8 from fimdim/main
Browse files Browse the repository at this point in the history
Add Azure App Configuration
  • Loading branch information
unaihuete93 authored Oct 31, 2022
2 parents 5a5ba20 + 41c1234 commit 5dda8a6
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 5 deletions.
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 CatalogIndexViewModel CatalogModel { get; set; } = new CatalogIndexViewModel();
Expand Down
6 changes: 6 additions & 0 deletions src/Web/Pages/SettingsViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Microsoft.eShopWeb.Web.Pages;

public class SettingsViewModel
{
public string NoResultsMessage { get; set; }
}
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
37 changes: 37 additions & 0 deletions src/Web/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
using Microsoft.eShopWeb.Web.Configuration;
using Microsoft.eShopWeb.Web.HealthChecks;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Azure.Identity;
using Microsoft.eShopWeb.Web.Pages;
using Microsoft.FeatureManagement;
using Microsoft.IdentityModel.Tokens;

var builder = WebApplication.CreateBuilder(args);

Expand Down Expand Up @@ -74,6 +78,33 @@
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 =>
{
options.Connect(new Uri(builder.Configuration["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 @@ -96,6 +127,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
3 changes: 3 additions & 0 deletions src/Web/Web.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
<PackageReference Include="MediatR.Extensions.Microsoft.DependencyInjection" Version="10.0.1" />
<PackageReference Include="BuildBundlerMinifier" Version="3.2.449" Condition="'$(Configuration)'=='Release'" PrivateAssets="All" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Server" Version="6.0.7" />
<PackageReference Include="Microsoft.Azure.AppConfiguration.AspNetCore" Version="5.1.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="6.0.7" />
<PackageReference Include="Microsoft.FeatureManagement.AspNetCore" Version="2.5.1" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="6.0.7" />
<PackageReference Include="Microsoft.Web.LibraryManager.Build" Version="2.1.175" />
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="6.0.7" />
Expand All @@ -34,6 +36,7 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.22.1" />
<PackageReference Include="Azure.Identity" Version="1.3.0" />
</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": "{appconfig-endpoint}"
}

0 comments on commit 5dda8a6

Please sign in to comment.