diff --git a/examples/DotNetCore/WebJobs/WebJobHelloWorld/README.md b/examples/DotNetCore/WebJobs/WebJobHelloWorld/README.md new file mode 100644 index 00000000..458f77bb --- /dev/null +++ b/examples/DotNetCore/WebJobs/WebJobHelloWorld/README.md @@ -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://.azconfig.io + + # Linux/macOS + export AppConfigurationEndpoint=https://.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 \ No newline at end of file diff --git a/examples/DotNetCore/WebJobs/WebJobHelloWorld/WebJobHelloWorld/Functions.cs b/examples/DotNetCore/WebJobs/WebJobHelloWorld/WebJobHelloWorld/Functions.cs index 3f00e2f0..d2cc79ca 100644 --- a/examples/DotNetCore/WebJobs/WebJobHelloWorld/WebJobHelloWorld/Functions.cs +++ b/examples/DotNetCore/WebJobs/WebJobHelloWorld/WebJobHelloWorld/Functions.cs @@ -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); } } diff --git a/examples/DotNetCore/WebJobs/WebJobHelloWorld/WebJobHelloWorld/Program.cs b/examples/DotNetCore/WebJobs/WebJobHelloWorld/WebJobHelloWorld/Program.cs index 3a14d457..e3529264 100644 --- a/examples/DotNetCore/WebJobs/WebJobHelloWorld/WebJobHelloWorld/Program.cs +++ b/examples/DotNetCore/WebJobs/WebJobHelloWorld/WebJobHelloWorld/Program.cs @@ -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."); } } } diff --git a/examples/DotNetCore/WebJobs/WebJobHelloWorld/WebJobHelloWorld/WebJobHelloWorld.csproj b/examples/DotNetCore/WebJobs/WebJobHelloWorld/WebJobHelloWorld/WebJobHelloWorld.csproj index f21be24c..e2262b3e 100644 --- a/examples/DotNetCore/WebJobs/WebJobHelloWorld/WebJobHelloWorld/WebJobHelloWorld.csproj +++ b/examples/DotNetCore/WebJobs/WebJobHelloWorld/WebJobHelloWorld/WebJobHelloWorld.csproj @@ -2,16 +2,19 @@ Exe - netcoreapp2.2 + net8.0 + enable + enable - - - - - - + + + + + + +