-
Notifications
You must be signed in to change notification settings - Fork 79
Upgrade WebJobHelloWorld sample to .NET 8 #1061
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2e1eef0
Initial plan for issue
Copilot 8099575
Update WebJobHelloWorld sample to .NET 8
Copilot fd199ca
Update to use DefaultAzureCredential for authentication
Copilot f63d5db
Update environment variable name to AppConfigurationEndpoint for cons…
Copilot 61d1bb5
Update package references to latest stable versions
Copilot ffcc3ca
Add copyright headers to WebJobHelloWorld project
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| # WebJob Hello World Sample | ||
|
|
||
| This sample demonstrates how to build a .NET 8 WebJobs application that uses Azure App Configuration to store and retrieve configurations. | ||
|
|
||
| ## Prerequisites | ||
|
|
||
| - [.NET 8 SDK](https://dotnet.microsoft.com/download/dotnet/8.0) | ||
| - Azure subscription with an App Configuration instance | ||
| - Azure Storage account | ||
| - Azure identity with permissions to access the App Configuration instance | ||
|
|
||
| ## How it works | ||
|
|
||
| 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. | ||
|
|
||
| ## Getting Started | ||
|
|
||
| 1. Set the environment variable `AppConfigurationEndpoint` to your Azure App Configuration endpoint URI: | ||
|
|
||
| ```bash | ||
| # Windows | ||
| set AppConfigurationEndpoint=https://<your-appconfig-name>.azconfig.io | ||
|
|
||
| # Linux/macOS | ||
| export AppConfigurationEndpoint=https://<your-appconfig-name>.azconfig.io | ||
| ``` | ||
|
|
||
| 2. Make sure you have the following configuration values set in your Azure App Configuration instance: | ||
| - `WebJob:AzureWebJobsStorage` - Your Azure Storage connection string | ||
| - `WebJob:QueueName` - The name of the queue to monitor | ||
|
|
||
| 3. Ensure you have proper authentication set up for DefaultAzureCredential. This could be: | ||
| - Azure CLI login | ||
| - Visual Studio/Visual Studio Code authentication | ||
| - Environment variables for service principal credentials | ||
| - Managed Identity (when deployed to Azure) | ||
|
|
||
| 4. Run the application: | ||
|
|
||
| ```bash | ||
| dotnet run | ||
| ``` | ||
|
|
||
| ## Dependencies | ||
|
|
||
| - Azure.Identity | ||
| - Microsoft.Azure.WebJobs | ||
| - Microsoft.Azure.WebJobs.Extensions | ||
| - Microsoft.Azure.WebJobs.Extensions.Storage.Queues | ||
| - Microsoft.Extensions.Configuration.AzureAppConfiguration |
26 changes: 14 additions & 12 deletions
26
examples/DotNetCore/WebJobs/WebJobHelloWorld/WebJobHelloWorld/Functions.cs
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,19 +1,21 @@ | ||
| using Microsoft.Azure.WebJobs; | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT license. | ||
| // | ||
| using Microsoft.Azure.WebJobs; | ||
| using Microsoft.Extensions.Logging; | ||
|
|
||
| namespace WebJobHelloWorld | ||
| namespace WebJobHelloWorld; | ||
|
|
||
| public class Functions | ||
| { | ||
| public class Functions | ||
| public static void ProcessQueueMessage( | ||
| [QueueTrigger("%QueueName%")] string message, // Get queue name from config | ||
| ILogger logger) | ||
| { | ||
| public static void ProcessQueueMessage( | ||
| [QueueTrigger("%QueueName%")] string message, // Get queue name from config | ||
| ILogger logger) | ||
| { | ||
| // | ||
| // Insert code to process the message here. | ||
| // | ||
| // | ||
| // Insert code to process the message here. | ||
| // | ||
|
|
||
| logger.LogInformation(message); | ||
| } | ||
| logger.LogInformation(message); | ||
| } | ||
| } |
99 changes: 51 additions & 48 deletions
99
examples/DotNetCore/WebJobs/WebJobHelloWorld/WebJobHelloWorld/Program.cs
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,67 +1,70 @@ | ||
| using System; | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT license. | ||
| // | ||
| using System; | ||
| using Azure.Identity; | ||
| using Microsoft.Extensions.Hosting; | ||
| using Microsoft.Extensions.Configuration; | ||
| using Microsoft.Extensions.Configuration.AzureAppConfiguration; | ||
| using Microsoft.Extensions.Logging; | ||
| using Microsoft.Extensions.Configuration; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
|
|
||
| namespace WebJobHelloWorld; | ||
|
|
||
| namespace WebJobHelloWorld | ||
| class Program | ||
| { | ||
| class Program | ||
| static void Main(string[] args) | ||
| { | ||
| static void Main(string[] args) | ||
| { | ||
| var hostBuilder = new HostBuilder(); | ||
| hostBuilder.ConfigureHostConfiguration((config) => | ||
| var host = new HostBuilder() | ||
| .ConfigureHostConfiguration(config => | ||
| { | ||
| LoadConfiguration(config); | ||
| }); | ||
| hostBuilder.ConfigureWebJobs(b => | ||
| }) | ||
| .ConfigureWebJobs(builder => | ||
| { | ||
| b.AddAzureStorageCoreServices(); | ||
| b.AddAzureStorage(); | ||
| }); | ||
| hostBuilder.ConfigureLogging((context, b) => | ||
| builder.AddAzureStorageQueues(); | ||
| }) | ||
| .ConfigureLogging(logging => | ||
| { | ||
| b.AddConsole(); | ||
| }); | ||
| logging.AddConsole(); | ||
| }) | ||
| .Build(); | ||
|
|
||
| host.Run(); | ||
| } | ||
|
|
||
| var host = hostBuilder.Build(); | ||
| using (host) | ||
| { | ||
| host.Run(); | ||
| } | ||
| } | ||
| private static void LoadConfiguration(IConfigurationBuilder configBuilder) | ||
| { | ||
| // | ||
| // This application attempts to connect to Azure App Configuration to retrieve Azure Blob Storage name, and Queue Name for the Azure Web Job. | ||
| // It reads the Endpoint URI for the App Configuration Service from environment variables. | ||
| configBuilder.AddEnvironmentVariables(); | ||
|
|
||
| private static void LoadConfiguration(IConfigurationBuilder configBuilder) | ||
| var config = configBuilder.Build(); | ||
| if (string.IsNullOrEmpty(config["AppConfigurationEndpoint"])) | ||
| { | ||
| // | ||
| // This application attempts to connect to Azure App Configuration to retrieve Azure Blob Storage name, and Queue Name for the Azure Web Job. | ||
| // It reads the ConnectionString for the App Configuration Service from environment variables. | ||
| configBuilder.AddEnvironmentVariables(); | ||
|
|
||
| var config = configBuilder.Build(); | ||
| if (string.IsNullOrEmpty(config["ConnectionString"])) | ||
| { | ||
| throw new ArgumentNullException("Please set the 'ConnectionString' environment variable to a valid Azure App Configuration connection string and re-run this example."); | ||
| } | ||
| throw new ArgumentNullException("Please set the 'AppConfigurationEndpoint' environment variable to a valid Azure App Configuration endpoint URI and re-run this example."); | ||
| } | ||
|
|
||
| configBuilder.AddAzureAppConfiguration(options => | ||
| { | ||
| options.Connect(config["ConnectionString"]) | ||
| .Use("WebJob:*") | ||
| .TrimKeyPrefix("WebJob:"); | ||
| }); | ||
| configBuilder.AddAzureAppConfiguration(options => | ||
| { | ||
| var endpoint = config["AppConfigurationEndpoint"] ?? | ||
| throw new ArgumentNullException("AppConfigurationEndpoint", "The AppConfigurationEndpoint environment variable cannot be null or empty."); | ||
|
|
||
| options.Connect(new Uri(endpoint), new DefaultAzureCredential()) | ||
| .Select("WebJob:*") | ||
| .TrimKeyPrefix("WebJob:"); | ||
| }); | ||
|
|
||
| config = configBuilder.Build(); | ||
| config = configBuilder.Build(); | ||
|
|
||
| if (string.IsNullOrEmpty(config["AzureWebJobsStorage"])) | ||
| { | ||
| throw new ArgumentNullException("AzureWebJobsStorage not found."); | ||
| } | ||
| else if (string.IsNullOrEmpty(config["QueueName"])) | ||
| { | ||
| throw new ArgumentNullException("QueueName not found."); | ||
| } | ||
| if (string.IsNullOrEmpty(config["AzureWebJobsStorage"])) | ||
| { | ||
| throw new ArgumentNullException("AzureWebJobsStorage not found."); | ||
| } | ||
| else if (string.IsNullOrEmpty(config["QueueName"])) | ||
| { | ||
| throw new ArgumentNullException("QueueName not found."); | ||
| } | ||
| } | ||
| } |
zhenlan marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.