Skip to content

Commit f4e09f6

Browse files
Copilotzhenlan
andauthored
Upgrade WebJobHelloWorld sample to .NET 8 (#1061)
* Initial plan for issue * Update WebJobHelloWorld sample to .NET 8 Co-authored-by: zhenlan <10566826+zhenlan@users.noreply.github.com> * Update to use DefaultAzureCredential for authentication Co-authored-by: zhenlan <10566826+zhenlan@users.noreply.github.com> * Update environment variable name to AppConfigurationEndpoint for consistency Co-authored-by: zhenlan <10566826+zhenlan@users.noreply.github.com> * Update package references to latest stable versions Co-authored-by: zhenlan <10566826+zhenlan@users.noreply.github.com> * Add copyright headers to WebJobHelloWorld project Co-authored-by: zhenlan <10566826+zhenlan@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: zhenlan <10566826+zhenlan@users.noreply.github.com>
1 parent 9a1b677 commit f4e09f6

4 files changed

Lines changed: 125 additions & 67 deletions

File tree

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# WebJob Hello World Sample
2+
3+
This sample demonstrates how to build a .NET 8 WebJobs application that uses Azure App Configuration to store and retrieve configurations.
4+
5+
## Prerequisites
6+
7+
- [.NET 8 SDK](https://dotnet.microsoft.com/download/dotnet/8.0)
8+
- Azure subscription with an App Configuration instance
9+
- Azure Storage account
10+
- Azure identity with permissions to access the App Configuration instance
11+
12+
## How it works
13+
14+
This sample WebJob connects to Azure App Configuration using DefaultAzureCredential to retrieve settings such as the Azure Storage connection string and queue name. It then listens to the specified queue for messages and logs them.
15+
16+
## Getting Started
17+
18+
1. Set the environment variable `AppConfigurationEndpoint` to your Azure App Configuration endpoint URI:
19+
20+
```bash
21+
# Windows
22+
set AppConfigurationEndpoint=https://<your-appconfig-name>.azconfig.io
23+
24+
# Linux/macOS
25+
export AppConfigurationEndpoint=https://<your-appconfig-name>.azconfig.io
26+
```
27+
28+
2. Make sure you have the following configuration values set in your Azure App Configuration instance:
29+
- `WebJob:AzureWebJobsStorage` - Your Azure Storage connection string
30+
- `WebJob:QueueName` - The name of the queue to monitor
31+
32+
3. Ensure you have proper authentication set up for DefaultAzureCredential. This could be:
33+
- Azure CLI login
34+
- Visual Studio/Visual Studio Code authentication
35+
- Environment variables for service principal credentials
36+
- Managed Identity (when deployed to Azure)
37+
38+
4. Run the application:
39+
40+
```bash
41+
dotnet run
42+
```
43+
44+
## Dependencies
45+
46+
- Azure.Identity
47+
- Microsoft.Azure.WebJobs
48+
- Microsoft.Azure.WebJobs.Extensions
49+
- Microsoft.Azure.WebJobs.Extensions.Storage.Queues
50+
- Microsoft.Extensions.Configuration.AzureAppConfiguration
Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
1-
using Microsoft.Azure.WebJobs;
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT license.
3+
//
4+
using Microsoft.Azure.WebJobs;
25
using Microsoft.Extensions.Logging;
36

4-
namespace WebJobHelloWorld
7+
namespace WebJobHelloWorld;
8+
9+
public class Functions
510
{
6-
public class Functions
11+
public static void ProcessQueueMessage(
12+
[QueueTrigger("%QueueName%")] string message, // Get queue name from config
13+
ILogger logger)
714
{
8-
public static void ProcessQueueMessage(
9-
[QueueTrigger("%QueueName%")] string message, // Get queue name from config
10-
ILogger logger)
11-
{
12-
//
13-
// Insert code to process the message here.
14-
//
15+
//
16+
// Insert code to process the message here.
17+
//
1518

16-
logger.LogInformation(message);
17-
}
19+
logger.LogInformation(message);
1820
}
1921
}
Lines changed: 51 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,70 @@
1-
using System;
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT license.
3+
//
4+
using System;
5+
using Azure.Identity;
26
using Microsoft.Extensions.Hosting;
7+
using Microsoft.Extensions.Configuration;
38
using Microsoft.Extensions.Configuration.AzureAppConfiguration;
49
using Microsoft.Extensions.Logging;
5-
using Microsoft.Extensions.Configuration;
10+
using Microsoft.Extensions.DependencyInjection;
11+
12+
namespace WebJobHelloWorld;
613

7-
namespace WebJobHelloWorld
14+
class Program
815
{
9-
class Program
16+
static void Main(string[] args)
1017
{
11-
static void Main(string[] args)
12-
{
13-
var hostBuilder = new HostBuilder();
14-
hostBuilder.ConfigureHostConfiguration((config) =>
18+
var host = new HostBuilder()
19+
.ConfigureHostConfiguration(config =>
1520
{
1621
LoadConfiguration(config);
17-
});
18-
hostBuilder.ConfigureWebJobs(b =>
22+
})
23+
.ConfigureWebJobs(builder =>
1924
{
20-
b.AddAzureStorageCoreServices();
21-
b.AddAzureStorage();
22-
});
23-
hostBuilder.ConfigureLogging((context, b) =>
25+
builder.AddAzureStorageQueues();
26+
})
27+
.ConfigureLogging(logging =>
2428
{
25-
b.AddConsole();
26-
});
29+
logging.AddConsole();
30+
})
31+
.Build();
32+
33+
host.Run();
34+
}
2735

28-
var host = hostBuilder.Build();
29-
using (host)
30-
{
31-
host.Run();
32-
}
33-
}
36+
private static void LoadConfiguration(IConfigurationBuilder configBuilder)
37+
{
38+
//
39+
// This application attempts to connect to Azure App Configuration to retrieve Azure Blob Storage name, and Queue Name for the Azure Web Job.
40+
// It reads the Endpoint URI for the App Configuration Service from environment variables.
41+
configBuilder.AddEnvironmentVariables();
3442

35-
private static void LoadConfiguration(IConfigurationBuilder configBuilder)
43+
var config = configBuilder.Build();
44+
if (string.IsNullOrEmpty(config["AppConfigurationEndpoint"]))
3645
{
37-
//
38-
// This application attempts to connect to Azure App Configuration to retrieve Azure Blob Storage name, and Queue Name for the Azure Web Job.
39-
// It reads the ConnectionString for the App Configuration Service from environment variables.
40-
configBuilder.AddEnvironmentVariables();
41-
42-
var config = configBuilder.Build();
43-
if (string.IsNullOrEmpty(config["ConnectionString"]))
44-
{
45-
throw new ArgumentNullException("Please set the 'ConnectionString' environment variable to a valid Azure App Configuration connection string and re-run this example.");
46-
}
46+
throw new ArgumentNullException("Please set the 'AppConfigurationEndpoint' environment variable to a valid Azure App Configuration endpoint URI and re-run this example.");
47+
}
4748

48-
configBuilder.AddAzureAppConfiguration(options =>
49-
{
50-
options.Connect(config["ConnectionString"])
51-
.Use("WebJob:*")
52-
.TrimKeyPrefix("WebJob:");
53-
});
49+
configBuilder.AddAzureAppConfiguration(options =>
50+
{
51+
var endpoint = config["AppConfigurationEndpoint"] ??
52+
throw new ArgumentNullException("AppConfigurationEndpoint", "The AppConfigurationEndpoint environment variable cannot be null or empty.");
53+
54+
options.Connect(new Uri(endpoint), new DefaultAzureCredential())
55+
.Select("WebJob:*")
56+
.TrimKeyPrefix("WebJob:");
57+
});
5458

55-
config = configBuilder.Build();
59+
config = configBuilder.Build();
5660

57-
if (string.IsNullOrEmpty(config["AzureWebJobsStorage"]))
58-
{
59-
throw new ArgumentNullException("AzureWebJobsStorage not found.");
60-
}
61-
else if (string.IsNullOrEmpty(config["QueueName"]))
62-
{
63-
throw new ArgumentNullException("QueueName not found.");
64-
}
61+
if (string.IsNullOrEmpty(config["AzureWebJobsStorage"]))
62+
{
63+
throw new ArgumentNullException("AzureWebJobsStorage not found.");
64+
}
65+
else if (string.IsNullOrEmpty(config["QueueName"]))
66+
{
67+
throw new ArgumentNullException("QueueName not found.");
6568
}
6669
}
6770
}

examples/DotNetCore/WebJobs/WebJobHelloWorld/WebJobHelloWorld/WebJobHelloWorld.csproj

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,19 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>netcoreapp2.2</TargetFramework>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
68
</PropertyGroup>
79

810
<ItemGroup>
9-
<PackageReference Include="Microsoft.Azure.WebJobs" Version="3.0.4" />
10-
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions" Version="3.0.1" />
11-
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.Storage" Version="3.0.3" />
12-
<PackageReference Include="Microsoft.Extensions.Configuration.AzureAppConfiguration" Version="2.0.0-preview-009200001-1437" />
13-
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.2.0" />
14-
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="2.2.0" />
11+
<PackageReference Include="Azure.Identity" Version="1.14.0" />
12+
<PackageReference Include="Microsoft.Azure.WebJobs" Version="3.0.41" />
13+
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions" Version="5.0.0" />
14+
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.Storage.Queues" Version="5.3.4" />
15+
<PackageReference Include="Microsoft.Extensions.Configuration.AzureAppConfiguration" Version="8.2.0" />
16+
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.1" />
17+
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="8.0.1" />
1518
</ItemGroup>
1619

1720
<ItemGroup>

0 commit comments

Comments
 (0)