Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions examples/DotNetCore/WebJobs/WebJobHelloWorld/README.md
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
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);
}
}
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.");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.2</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Azure.WebJobs" Version="3.0.4" />
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions" Version="3.0.1" />
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.Storage" Version="3.0.3" />
<PackageReference Include="Microsoft.Extensions.Configuration.AzureAppConfiguration" Version="2.0.0-preview-009200001-1437" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="2.2.0" />
<PackageReference Include="Azure.Identity" Version="1.14.0" />
<PackageReference Include="Microsoft.Azure.WebJobs" Version="3.0.41" />
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions" Version="5.0.0" />
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.Storage.Queues" Version="5.3.4" />
<PackageReference Include="Microsoft.Extensions.Configuration.AzureAppConfiguration" Version="8.2.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="8.0.1" />
</ItemGroup>

<ItemGroup>
Expand Down