Skip to content

Commit 63d6960

Browse files
authored
Add Function App example with .NET 8 isolated model (#1025)
1 parent 31feeca commit 63d6960

File tree

7 files changed

+190
-0
lines changed

7 files changed

+190
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.14.35828.13 main
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FunctionAppIsolated", "FunctionAppIsolated\FunctionAppIsolated.csproj", "{B24FEE1E-DDB8-E1CD-925F-199BF3D603C4}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{B24FEE1E-DDB8-E1CD-925F-199BF3D603C4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{B24FEE1E-DDB8-E1CD-925F-199BF3D603C4}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{B24FEE1E-DDB8-E1CD-925F-199BF3D603C4}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{B24FEE1E-DDB8-E1CD-925F-199BF3D603C4}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {DBF3FBCB-A4CD-41A8-BA2D-562B632F5BE7}
24+
EndGlobalSection
25+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<TargetFramework>net8.0</TargetFramework>
4+
<AzureFunctionsVersion>v4</AzureFunctionsVersion>
5+
<OutputType>Exe</OutputType>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
<ItemGroup>
10+
<FrameworkReference Include="Microsoft.AspNetCore.App" />
11+
<PackageReference Include="Azure.Identity" Version="1.13.2" />
12+
<PackageReference Include="Microsoft.Azure.AppConfiguration.Functions.Worker" Version="8.1.1" />
13+
<PackageReference Include="Microsoft.Azure.Functions.Worker" Version="2.0.0" />
14+
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.3.0" />
15+
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore" Version="2.0.1" />
16+
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Storage.Queues" Version="5.5.0" />
17+
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="2.0.1" />
18+
<PackageReference Include="Microsoft.FeatureManagement" Version="4.0.0" />
19+
</ItemGroup>
20+
<ItemGroup>
21+
<None Update="host.json">
22+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
23+
</None>
24+
<None Update="local.settings.json">
25+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
26+
<CopyToPublishDirectory>Never</CopyToPublishDirectory>
27+
</None>
28+
</ItemGroup>
29+
<ItemGroup>
30+
<Using Include="System.Threading.ExecutionContext" Alias="ExecutionContext" />
31+
</ItemGroup>
32+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using Azure.Identity;
2+
using Microsoft.Azure.Functions.Worker.Builder;
3+
using Microsoft.Extensions.Configuration;
4+
using Microsoft.Extensions.Hosting;
5+
using Microsoft.FeatureManagement;
6+
7+
var builder = FunctionsApplication.CreateBuilder(args);
8+
9+
// Connect to Azure App Configuration
10+
builder.Configuration.AddAzureAppConfiguration(options =>
11+
{
12+
Uri endpoint = new(Environment.GetEnvironmentVariable("AZURE_APPCONFIG_ENDPOINT") ?? string.Empty);
13+
options.Connect(endpoint, new DefaultAzureCredential())
14+
// Load all keys that start with `TestApp:` and have no label
15+
.Select("TestApp:*")
16+
// Reload configuration if any selected key-values have changed.
17+
// Use the default refresh interval of 30 seconds. It can be overridden via AzureAppConfigurationRefreshOptions.SetRefreshInterval.
18+
.ConfigureRefresh(refreshOptions =>
19+
{
20+
refreshOptions.RegisterAll();
21+
})
22+
// Load all feature flags with no label. To load feature flags with specific keys and labels, set via FeatureFlagOptions.Select.
23+
// Use the default refresh interval of 30 seconds. It can be overridden via FeatureFlagOptions.SetRefreshInterval.
24+
.UseFeatureFlags();
25+
});
26+
27+
// Add Azure App Configuration middleware and feature management to the service collection.
28+
builder.Services
29+
.AddAzureAppConfiguration()
30+
.AddFeatureManagement();
31+
32+
// Use Azure App Configuration middleware for dynamic configuration and feature flag refresh.
33+
builder.UseAzureAppConfiguration();
34+
35+
builder.ConfigureFunctionsWebApplication();
36+
37+
builder.Build().Run();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using Azure.Storage.Queues.Models;
2+
using Microsoft.Azure.Functions.Worker;
3+
using Microsoft.Extensions.Logging;
4+
5+
namespace FunctionAppIsolated
6+
{
7+
public class ReadQueuedMessage
8+
{
9+
private readonly ILogger<ReadQueuedMessage> _logger;
10+
11+
public ReadQueuedMessage(ILogger<ReadQueuedMessage> logger)
12+
{
13+
_logger = logger;
14+
}
15+
16+
// Queue triggered function with queue name defined in Azure App Configuration.
17+
// The queue name is stored with the key `TestApp:Storage:QueueName` in Azure App Configuration.
18+
// `AZURE_APPCONFIG_REFERENCE_QUEUENAME` is an app setting of the Function App referencing this key.
19+
// Learn more about App Configuration Reference:
20+
// https://learn.microsoft.com/azure/app-service/app-service-configuration-references
21+
[Function(nameof(ReadQueuedMessage))]
22+
public void Run([QueueTrigger(queueName: "%AZURE_APPCONFIG_REFERENCE_QUEUENAME%")] QueueMessage message)
23+
{
24+
_logger.LogInformation($"C# Queue trigger function processed: {message.MessageText}");
25+
}
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using Microsoft.AspNetCore.Http;
2+
using Microsoft.AspNetCore.Mvc;
3+
using Microsoft.Azure.Functions.Worker;
4+
using Microsoft.Extensions.Logging;
5+
using Microsoft.FeatureManagement;
6+
7+
namespace FunctionAppIsolated
8+
{
9+
public class ShowBetaFeature
10+
{
11+
private readonly IVariantFeatureManagerSnapshot _featureManager;
12+
private readonly ILogger<ShowBetaFeature> _logger;
13+
14+
public ShowBetaFeature(IVariantFeatureManagerSnapshot featureManager, ILogger<ShowBetaFeature> logger)
15+
{
16+
_featureManager = featureManager;
17+
_logger = logger;
18+
}
19+
20+
[Function("ShowBetaFeature")]
21+
public async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequest req)
22+
{
23+
_logger.LogInformation("C# HTTP trigger function processed a request.");
24+
25+
// Read feature flag
26+
string featureName = "Beta";
27+
bool featureEnabled = await _featureManager.IsEnabledAsync(featureName);
28+
29+
return new OkObjectResult(featureEnabled
30+
? $"{featureName} feature is On"
31+
: $"{featureName} feature is Off (or not found in Azure App Configuration).");
32+
}
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using Microsoft.AspNetCore.Http;
2+
using Microsoft.AspNetCore.Mvc;
3+
using Microsoft.Azure.Functions.Worker;
4+
using Microsoft.Extensions.Configuration;
5+
using Microsoft.Extensions.Logging;
6+
7+
namespace FunctionAppIsolated
8+
{
9+
public class ShowMessage
10+
{
11+
private readonly IConfiguration _configuration;
12+
private readonly ILogger<ShowMessage> _logger;
13+
14+
public ShowMessage(IConfiguration configuration, ILogger<ShowMessage> logger)
15+
{
16+
_configuration = configuration;
17+
_logger = logger;
18+
}
19+
20+
[Function("ShowMessage")]
21+
public IActionResult Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequest req)
22+
{
23+
_logger.LogInformation("C# HTTP trigger function processed a request.");
24+
25+
// Read configuration data
26+
string key = "TestApp:Settings:Message";
27+
string? message = _configuration[key];
28+
29+
return new OkObjectResult(message ?? $"Please create a key-value with the key '{key}' in Azure App Configuration.");
30+
}
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"version": "2.0"
3+
}

0 commit comments

Comments
 (0)