Last active
November 19, 2020 15:49
-
-
Save marcduiker/c4d61ec99ac7549aa218b6e8c4d4d111 to your computer and use it in GitHub Desktop.
AppConfig Feature Management with Azure Functions
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
namespace FunctionAppWithFeatureManagement | |
{ | |
public static class FeatureManagementConfig | |
{ | |
public static string ConnectionStringSetting => "FeatureConfigConnectionString"; | |
public static string FeaturePrefix => "FuncApp-*"; | |
public static string FuncAppFeatureA => "FuncApp-FeatureA"; | |
} | |
} |
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
<Project Sdk="Microsoft.NET.Sdk"> | |
<PropertyGroup> | |
<TargetFramework>netcoreapp3.1</TargetFramework> | |
<AzureFunctionsVersion>v3</AzureFunctionsVersion> | |
</PropertyGroup> | |
<ItemGroup> | |
<PackageReference Include="Microsoft.Azure.Functions.Extensions" Version="1.1.0" /> | |
<PackageReference Include="Microsoft.Extensions.Configuration.AzureAppConfiguration" Version="4.0.0" /> | |
<PackageReference Include="Microsoft.FeatureManagement" Version="2.2.0" /> | |
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="3.0.11" /> | |
</ItemGroup> | |
<ItemGroup> | |
<None Update="host.json"> | |
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> | |
</None> | |
<None Update="local.settings.json"> | |
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> | |
<CopyToPublishDirectory>Never</CopyToPublishDirectory> | |
</None> | |
</ItemGroup> | |
</Project> |
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 System.Threading.Tasks; | |
using Microsoft.AspNetCore.Mvc; | |
using Microsoft.Azure.WebJobs; | |
using Microsoft.Azure.WebJobs.Extensions.Http; | |
using Microsoft.AspNetCore.Http; | |
using Microsoft.Extensions.Logging; | |
using Microsoft.FeatureManagement; | |
using Microsoft.Extensions.Configuration.AzureAppConfiguration; | |
namespace FunctionAppWithFeatureManagement | |
{ | |
public class HttpTriggerFeatureA | |
{ | |
private readonly IFeatureManagerSnapshot _featureManagerSnapshot; | |
private readonly IConfigurationRefresher _configurationRefresher; | |
public HttpTriggerFeatureA(IFeatureManagerSnapshot featureManagerSnapshot, IConfigurationRefresher configurationRefresher) | |
{ | |
_featureManagerSnapshot = featureManagerSnapshot; | |
_configurationRefresher = configurationRefresher; | |
} | |
[FunctionName(nameof(HttpTriggerFeatureA))] | |
public async Task<IActionResult> Run( | |
[HttpTrigger(AuthorizationLevel.Function, "get", Route = null)] HttpRequest req, | |
ILogger log) | |
{ | |
await _configurationRefresher.TryRefreshAsync(); | |
string responseMessage; | |
if (await _featureManagerSnapshot.IsEnabledAsync(FeatureManagementConfig.FuncAppFeatureA)) | |
{ | |
responseMessage = $"{FeatureManagementConfig.FuncAppFeatureA} is enabled."; | |
} | |
else | |
{ | |
responseMessage = $"{FeatureManagementConfig.FuncAppFeatureA} is disabled."; | |
} | |
return new OkObjectResult(responseMessage); | |
} | |
} | |
} |
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 FunctionAppWithFeatureManagement; | |
using Microsoft.Azure.Functions.Extensions.DependencyInjection; | |
using Microsoft.Extensions.Configuration; | |
using Microsoft.Extensions.Configuration.AzureAppConfiguration; | |
using Microsoft.Extensions.DependencyInjection; | |
using Microsoft.FeatureManagement; | |
using Microsoft.FeatureManagement.FeatureFilters; | |
using System; | |
[assembly: FunctionsStartup(typeof(StartUp))] | |
namespace FunctionAppWithFeatureManagement | |
{ | |
public class StartUp : FunctionsStartup | |
{ | |
public override void Configure(IFunctionsHostBuilder builder) | |
{ | |
IConfigurationRefresher configurationRefresher = null; | |
IConfigurationRoot configuration = new ConfigurationBuilder() | |
.AddAzureAppConfiguration(options => | |
{ | |
options.Connect(Environment.GetEnvironmentVariable(FeatureManagementConfig.ConnectionStringSetting)) | |
.Select(FeatureManagementConfig.FeaturePrefix) | |
.ConfigureRefresh(refreshOptions => | |
refreshOptions | |
.Register(FeatureManagementConfig.FuncAppFeatureA, refreshAll: true) | |
.SetCacheExpiration(TimeSpan.FromSeconds(10))) | |
.UseFeatureFlags(); | |
configurationRefresher = options.GetRefresher(); | |
}) | |
.Build(); | |
builder.Services.AddFeatureManagement(configuration); | |
builder.Services.AddSingleton<IConfigurationRefresher>(configurationRefresher); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment