From 67b9ab9332e869c3fee29a1bde30e887574cde12 Mon Sep 17 00:00:00 2001 From: Zhenlan Wang Date: Mon, 3 Mar 2025 10:29:04 -0800 Subject: [PATCH] Add Function App example with .NET 8 isolated model --- .../AzureFunctions/AzureFunctions.sln | 25 +++++++++++++ .../FunctionAppIsolated.csproj | 32 ++++++++++++++++ .../FunctionAppIsolated/Program.cs | 37 +++++++++++++++++++ .../FunctionAppIsolated/ReadQueuedMessage.cs | 27 ++++++++++++++ .../FunctionAppIsolated/ShowBetaFeature.cs | 34 +++++++++++++++++ .../FunctionAppIsolated/ShowMessage.cs | 32 ++++++++++++++++ .../FunctionAppIsolated/host.json | 3 ++ 7 files changed, 190 insertions(+) create mode 100644 examples/DotNetCore/AzureFunctions/AzureFunctions.sln create mode 100644 examples/DotNetCore/AzureFunctions/FunctionAppIsolated/FunctionAppIsolated.csproj create mode 100644 examples/DotNetCore/AzureFunctions/FunctionAppIsolated/Program.cs create mode 100644 examples/DotNetCore/AzureFunctions/FunctionAppIsolated/ReadQueuedMessage.cs create mode 100644 examples/DotNetCore/AzureFunctions/FunctionAppIsolated/ShowBetaFeature.cs create mode 100644 examples/DotNetCore/AzureFunctions/FunctionAppIsolated/ShowMessage.cs create mode 100644 examples/DotNetCore/AzureFunctions/FunctionAppIsolated/host.json diff --git a/examples/DotNetCore/AzureFunctions/AzureFunctions.sln b/examples/DotNetCore/AzureFunctions/AzureFunctions.sln new file mode 100644 index 00000000..c608c83c --- /dev/null +++ b/examples/DotNetCore/AzureFunctions/AzureFunctions.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.14.35828.13 main +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FunctionAppIsolated", "FunctionAppIsolated\FunctionAppIsolated.csproj", "{B24FEE1E-DDB8-E1CD-925F-199BF3D603C4}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {B24FEE1E-DDB8-E1CD-925F-199BF3D603C4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B24FEE1E-DDB8-E1CD-925F-199BF3D603C4}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B24FEE1E-DDB8-E1CD-925F-199BF3D603C4}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B24FEE1E-DDB8-E1CD-925F-199BF3D603C4}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {DBF3FBCB-A4CD-41A8-BA2D-562B632F5BE7} + EndGlobalSection +EndGlobal diff --git a/examples/DotNetCore/AzureFunctions/FunctionAppIsolated/FunctionAppIsolated.csproj b/examples/DotNetCore/AzureFunctions/FunctionAppIsolated/FunctionAppIsolated.csproj new file mode 100644 index 00000000..e03b78a4 --- /dev/null +++ b/examples/DotNetCore/AzureFunctions/FunctionAppIsolated/FunctionAppIsolated.csproj @@ -0,0 +1,32 @@ + + + net8.0 + v4 + Exe + enable + enable + + + + + + + + + + + + + + + PreserveNewest + + + PreserveNewest + Never + + + + + + diff --git a/examples/DotNetCore/AzureFunctions/FunctionAppIsolated/Program.cs b/examples/DotNetCore/AzureFunctions/FunctionAppIsolated/Program.cs new file mode 100644 index 00000000..0dfda2b8 --- /dev/null +++ b/examples/DotNetCore/AzureFunctions/FunctionAppIsolated/Program.cs @@ -0,0 +1,37 @@ +using Azure.Identity; +using Microsoft.Azure.Functions.Worker.Builder; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Hosting; +using Microsoft.FeatureManagement; + +var builder = FunctionsApplication.CreateBuilder(args); + +// Connect to Azure App Configuration +builder.Configuration.AddAzureAppConfiguration(options => +{ + Uri endpoint = new(Environment.GetEnvironmentVariable("AZURE_APPCONFIG_ENDPOINT") ?? string.Empty); + options.Connect(endpoint, new DefaultAzureCredential()) + // Load all keys that start with `TestApp:` and have no label + .Select("TestApp:*") + // Reload configuration if any selected key-values have changed. + // Use the default refresh interval of 30 seconds. It can be overridden via AzureAppConfigurationRefreshOptions.SetRefreshInterval. + .ConfigureRefresh(refreshOptions => + { + refreshOptions.RegisterAll(); + }) + // Load all feature flags with no label. To load feature flags with specific keys and labels, set via FeatureFlagOptions.Select. + // Use the default refresh interval of 30 seconds. It can be overridden via FeatureFlagOptions.SetRefreshInterval. + .UseFeatureFlags(); +}); + +// Add Azure App Configuration middleware and feature management to the service collection. +builder.Services + .AddAzureAppConfiguration() + .AddFeatureManagement(); + +// Use Azure App Configuration middleware for dynamic configuration and feature flag refresh. +builder.UseAzureAppConfiguration(); + +builder.ConfigureFunctionsWebApplication(); + +builder.Build().Run(); diff --git a/examples/DotNetCore/AzureFunctions/FunctionAppIsolated/ReadQueuedMessage.cs b/examples/DotNetCore/AzureFunctions/FunctionAppIsolated/ReadQueuedMessage.cs new file mode 100644 index 00000000..0cb5795f --- /dev/null +++ b/examples/DotNetCore/AzureFunctions/FunctionAppIsolated/ReadQueuedMessage.cs @@ -0,0 +1,27 @@ +using Azure.Storage.Queues.Models; +using Microsoft.Azure.Functions.Worker; +using Microsoft.Extensions.Logging; + +namespace FunctionAppIsolated +{ + public class ReadQueuedMessage + { + private readonly ILogger _logger; + + public ReadQueuedMessage(ILogger logger) + { + _logger = logger; + } + + // Queue triggered function with queue name defined in Azure App Configuration. + // The queue name is stored with the key `TestApp:Storage:QueueName` in Azure App Configuration. + // `AZURE_APPCONFIG_REFERENCE_QUEUENAME` is an app setting of the Function App referencing this key. + // Learn more about App Configuration Reference: + // https://learn.microsoft.com/azure/app-service/app-service-configuration-references + [Function(nameof(ReadQueuedMessage))] + public void Run([QueueTrigger(queueName: "%AZURE_APPCONFIG_REFERENCE_QUEUENAME%")] QueueMessage message) + { + _logger.LogInformation($"C# Queue trigger function processed: {message.MessageText}"); + } + } +} diff --git a/examples/DotNetCore/AzureFunctions/FunctionAppIsolated/ShowBetaFeature.cs b/examples/DotNetCore/AzureFunctions/FunctionAppIsolated/ShowBetaFeature.cs new file mode 100644 index 00000000..135e8c28 --- /dev/null +++ b/examples/DotNetCore/AzureFunctions/FunctionAppIsolated/ShowBetaFeature.cs @@ -0,0 +1,34 @@ +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Azure.Functions.Worker; +using Microsoft.Extensions.Logging; +using Microsoft.FeatureManagement; + +namespace FunctionAppIsolated +{ + public class ShowBetaFeature + { + private readonly IVariantFeatureManagerSnapshot _featureManager; + private readonly ILogger _logger; + + public ShowBetaFeature(IVariantFeatureManagerSnapshot featureManager, ILogger logger) + { + _featureManager = featureManager; + _logger = logger; + } + + [Function("ShowBetaFeature")] + public async Task Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequest req) + { + _logger.LogInformation("C# HTTP trigger function processed a request."); + + // Read feature flag + string featureName = "Beta"; + bool featureEnabled = await _featureManager.IsEnabledAsync(featureName); + + return new OkObjectResult(featureEnabled + ? $"{featureName} feature is On" + : $"{featureName} feature is Off (or not found in Azure App Configuration)."); + } + } +} diff --git a/examples/DotNetCore/AzureFunctions/FunctionAppIsolated/ShowMessage.cs b/examples/DotNetCore/AzureFunctions/FunctionAppIsolated/ShowMessage.cs new file mode 100644 index 00000000..9934b83a --- /dev/null +++ b/examples/DotNetCore/AzureFunctions/FunctionAppIsolated/ShowMessage.cs @@ -0,0 +1,32 @@ +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Azure.Functions.Worker; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Logging; + +namespace FunctionAppIsolated +{ + public class ShowMessage + { + private readonly IConfiguration _configuration; + private readonly ILogger _logger; + + public ShowMessage(IConfiguration configuration, ILogger logger) + { + _configuration = configuration; + _logger = logger; + } + + [Function("ShowMessage")] + public IActionResult Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequest req) + { + _logger.LogInformation("C# HTTP trigger function processed a request."); + + // Read configuration data + string key = "TestApp:Settings:Message"; + string? message = _configuration[key]; + + return new OkObjectResult(message ?? $"Please create a key-value with the key '{key}' in Azure App Configuration."); + } + } +} diff --git a/examples/DotNetCore/AzureFunctions/FunctionAppIsolated/host.json b/examples/DotNetCore/AzureFunctions/FunctionAppIsolated/host.json new file mode 100644 index 00000000..a8fe8c4c --- /dev/null +++ b/examples/DotNetCore/AzureFunctions/FunctionAppIsolated/host.json @@ -0,0 +1,3 @@ +{ + "version": "2.0" +}