Created
February 2, 2020 19:41
-
-
Save kasunkv/074bf6077805f29bb26f0882ed26bbbf to your computer and use it in GitHub Desktop.
Configuring offline caching for Azure App Configuration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Azure.Identity; | |
using Microsoft.Extensions.Configuration.AzureAppConfiguration; | |
namespace MusicStore.Web | |
{ | |
public class Program | |
{ | |
.. | |
public static IHostBuilder CreateHostBuilder(string[] args) => | |
Host.CreateDefaultBuilder(args) | |
.ConfigureWebHostDefaults(webBuilder => { | |
webBuilder.UseStartup<Startup>(); | |
}) | |
.ConfigureAppConfiguration((context, config) => { | |
var settings = config.Build(); | |
var appConfigEndpoint = settings["AppSettings:AppConfiguration:Endpoint"]; | |
var userAssignedIdentityClientId = settings["AppSettings:Identity:ClientId"]; | |
if (!string.IsNullOrEmpty(appConfigEndpoint)) | |
{ | |
var endpoint = new Uri(appConfigEndpoint); | |
config.AddAzureAppConfiguration(options => | |
{ | |
options | |
// Use managed identity to access app configuration | |
.Connect(endpoint, new ManagedIdentityCredential(clientId: userAssignedIdentityClientId)) | |
// Setup dynamic refresh | |
.ConfigureRefresh(refreshOpt => | |
{ | |
refreshOpt.Register(key: "AppSettings:Version", refreshAll: true, label: LabelFilter.Null); | |
refreshOpt.SetCacheExpiration(TimeSpan.FromSeconds(10)); | |
}) | |
// Setup offline cache | |
.SetOfflineCache(new OfflineFileCache()) | |
.UseFeatureFlags(); | |
}); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment