Skip to content

Commit b9d8e67

Browse files
committed
art2-3
1 parent 140360a commit b9d8e67

1 file changed

Lines changed: 104 additions & 102 deletions

File tree

articles/app-service/webjobs-sdk-get-started.md

Lines changed: 104 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Tutorial for event-driven background processing with the WebJobs SDK
33
description: Learn how to enable your web apps to run background tasks. Use this tutorial to get started with the WebJobs SDK.
44
author: ggailey777
55
ms.devlang: csharp
6-
ms.date: 01/17/2025
6+
ms.date: 03/12/2026
77
ms.author: glenga
88
ms.topic: tutorial
99

@@ -37,6 +37,7 @@ You learn how to:
3737
- An Azure account with an active subscription. [Create an account for free](https://azure.microsoft.com/pricing/purchase-options/azure-account?cid=msft_learn).
3838

3939
## Create a console app
40+
4041
In this section, you start by creating a project in Visual Studio 2022. Next, you add tools for Azure development, code publishing, and functions that listen for triggers and call functions. Last, you set up console logging that disables a legacy monitoring tool and enables a console provider with default filtering.
4142

4243
> [!NOTE]
@@ -64,11 +65,12 @@ Install the latest WebJobs NuGet package. This package includes Microsoft.Azure.
6465

6566
1. In the following command, replace `<4_X_VERSION>` with the current version number you found in step 1.
6667

67-
```powershell
68-
Install-Package Microsoft.Azure.WebJobs.Extensions -version <4_X_VERSION>
69-
```
70-
> [!NOTE]
71-
> The sample code in this article works with package versions 4.x. Make sure you use a 4.x version because you get build errors when using package versions 5.x.
68+
```powershell
69+
Install-Package Microsoft.Azure.WebJobs.Extensions -version <4_X_VERSION>
70+
```
71+
72+
> [!NOTE]
73+
> The sample code in this article works with package versions 4.x. Make sure you use a 4.x version because you get build errors when using package versions 5.x.
7274
7375
1. In the **Package Manager Console**, execute the command. The extension list appears and automatically installs.
7476

@@ -78,34 +80,34 @@ The host is the runtime container for functions that listens for triggers and ca
7880

7981
1. Select the **Program.cs** tab, remove the existing contents, and add these `using` statements:
8082

81-
```cs
82-
using System.Threading.Tasks;
83-
using Microsoft.Extensions.Hosting;
84-
```
83+
```cs
84+
using System.Threading.Tasks;
85+
using Microsoft.Extensions.Hosting;
86+
```
8587

8688
1. Also under **Program.cs**, add the following code:
8789

88-
```cs
89-
namespace WebJobsSDKSample
90-
{
91-
class Program
92-
{
93-
static async Task Main()
94-
{
95-
var builder = new HostBuilder();
96-
builder.ConfigureWebJobs(b =>
97-
{
98-
b.AddAzureStorageCoreServices();
99-
});
100-
var host = builder.Build();
101-
using (host)
102-
{
103-
await host.RunAsync();
104-
}
105-
}
106-
}
107-
}
108-
```
90+
```cs
91+
namespace WebJobsSDKSample
92+
{
93+
class Program
94+
{
95+
static async Task Main()
96+
{
97+
var builder = new HostBuilder();
98+
builder.ConfigureWebJobs(b =>
99+
{
100+
b.AddAzureStorageCoreServices();
101+
});
102+
var host = builder.Build();
103+
using (host)
104+
{
105+
await host.RunAsync();
106+
}
107+
}
108+
}
109+
}
110+
```
109111

110112
In ASP.NET Core, host configurations are set by calling methods on the [`HostBuilder`](/dotnet/api/microsoft.extensions.hosting.hostbuilder) instance. For more information, see [.NET Generic Host](/aspnet/core/fundamentals/host/generic-host). The `ConfigureWebJobs` extension method initializes the WebJobs host. In `ConfigureWebJobs`, initialize specific binding extensions, such as the Storage binding extension, and set properties of those extensions.
111113

@@ -188,61 +190,61 @@ Starting with version 3 of the WebJobs SDK, to connect to Azure Storage services
188190
189191
1. In the following command, replace `<5_X_VERSION>` with the current version number you found in step 1. Each type of NuGet Package has a unique version number.
190192

191-
```powershell
192-
Install-Package Microsoft.Azure.WebJobs.Extensions.Storage -Version <5_X_VERSION>
193-
```
193+
```powershell
194+
Install-Package Microsoft.Azure.WebJobs.Extensions.Storage -Version <5_X_VERSION>
195+
```
194196

195197
1. In the **Package Manager Console**, execute the command with the current version number at the `PM>` entry point.
196198

197199
1. Also run this command to update the `Microsoft.Azure.WebJobs.Host.Storage` package to version 4.1.0:
198200

199-
```powershell
200-
Install-Package Microsoft.Azure.WebJobs.Host.Storage -Version 4.1.0
201-
```
201+
```powershell
202+
Install-Package Microsoft.Azure.WebJobs.Host.Storage -Version 4.1.0
203+
```
202204

203205
1. Continuing in **Program.cs**, in the `ConfigureWebJobs` extension method, add the `AddAzureStorageQueues` method on the [`HostBuilder`](/dotnet/api/microsoft.extensions.hosting.hostbuilder) instance (before the `Build` command) to initialize the Storage extension. At this point, the `ConfigureWebJobs` method looks like this:
204206

205-
```cs
206-
builder.ConfigureWebJobs(b =>
207-
{
208-
b.AddAzureStorageCoreServices();
209-
b.AddAzureStorageQueues();
210-
});
211-
```
207+
```cs
208+
builder.ConfigureWebJobs(b =>
209+
{
210+
b.AddAzureStorageCoreServices();
211+
b.AddAzureStorageQueues();
212+
});
213+
```
212214
1. Add the following code in the `Main` method after the `builder` is instantiated:
213215

214-
```csharp
215-
builder.UseEnvironment(EnvironmentName.Development);
216-
```
216+
```csharp
217+
builder.UseEnvironment(EnvironmentName.Development);
218+
```
217219

218-
Running in [development mode](webjobs-sdk-how-to.md#host-development-settings) reduces the [queue polling exponential backoff](../azure-functions/functions-bindings-storage-queue-trigger.md?tabs=csharp#polling-algorithm). This polling algorithm can significantly delay the amount of time it takes for the runtime to find the message and invoke the function. You should remove this line of code or switch to `Production` when you're done with development and testing.
220+
Running in [development mode](webjobs-sdk-how-to.md#host-development-settings) reduces the [queue polling exponential backoff](../azure-functions/functions-bindings-storage-queue-trigger.md?tabs=csharp#polling-algorithm). This polling algorithm can significantly delay the amount of time it takes for the runtime to find the message and invoke the function. You should remove this line of code or switch to `Production` when you're done with development and testing.
219221

220-
The `Main` method should now look like the following example:
222+
The `Main` method should now look like the following example:
221223

222-
```csharp
223-
static async Task Main()
224-
{
225-
var builder = new HostBuilder();
226-
builder.UseEnvironment(EnvironmentName.Development);
227-
builder.ConfigureWebJobs(b =>
228-
{
229-
b.AddAzureStorageCoreServices();
230-
b.AddAzureStorageQueues();
231-
});
232-
builder.ConfigureLogging((context, b) =>
233-
{
234-
b.SetMinimumLevel(LogLevel.Error);
235-
b.AddFilter("Function", LogLevel.Information);
236-
b.AddFilter("Host", LogLevel.Debug);
237-
b.AddConsole();
238-
});
239-
var host = builder.Build();
240-
using (host)
241-
{
242-
await host.RunAsync();
243-
}
244-
}
245-
```
224+
```csharp
225+
static async Task Main()
226+
{
227+
var builder = new HostBuilder();
228+
builder.UseEnvironment(EnvironmentName.Development);
229+
builder.ConfigureWebJobs(b =>
230+
{
231+
b.AddAzureStorageCoreServices();
232+
b.AddAzureStorageQueues();
233+
});
234+
builder.ConfigureLogging((context, b) =>
235+
{
236+
b.SetMinimumLevel(LogLevel.Error);
237+
b.AddFilter("Function", LogLevel.Information);
238+
b.AddFilter("Host", LogLevel.Debug);
239+
b.AddConsole();
240+
});
241+
var host = builder.Build();
242+
using (host)
243+
{
244+
await host.RunAsync();
245+
}
246+
}
247+
```
246248

247249
### Create a queue triggered function
248250

@@ -254,23 +256,23 @@ The `QueueTrigger` attribute tells the runtime to call this function when a new
254256

255257
1. In *Functions.cs*, replace the generated template with the following code:
256258

257-
```cs
258-
using Microsoft.Azure.WebJobs;
259-
using Microsoft.Extensions.Logging;
259+
```cs
260+
using Microsoft.Azure.WebJobs;
261+
using Microsoft.Extensions.Logging;
260262

261-
namespace WebJobsSDKSample
262-
{
263-
public static class Functions
264-
{
265-
public static void ProcessQueueMessage([QueueTrigger("queue")] string message, ILogger logger)
266-
{
267-
logger.LogInformation(message);
268-
}
269-
}
270-
}
271-
```
263+
namespace WebJobsSDKSample
264+
{
265+
public static class Functions
266+
{
267+
public static void ProcessQueueMessage([QueueTrigger("queue")] string message, ILogger logger)
268+
{
269+
logger.LogInformation(message);
270+
}
271+
}
272+
}
273+
```
272274

273-
You should mark the *Functions* class as `public static` in order for the runtime to access and execute the method. In the preceding code sample, when a message is added to a queue named `queue`, the function executes and the `message` string is written to the logs. The queue being monitored is in the default Azure Storage account, which you create next.
275+
You should mark the *Functions* class as `public static` in order for the runtime to access and execute the method. In the preceding code sample, when a message is added to a queue named `queue`, the function executes and the `message` string is written to the logs. The queue being monitored is in the default Azure Storage account, which you create next.
274276

275277
The `message` parameter doesn't have to be a string. You can also bind to a JSON object, a byte array, or a [CloudQueueMessage](/dotnet/api/microsoft.azure.storage.queue.cloudqueuemessage) object. [See Queue trigger usage](../azure-functions/functions-bindings-storage-queue-trigger.md?tabs=csharp#usage). Each binding type (such as queues, blobs, or tables) has a different set of parameter types that you can bind to.
276278

@@ -298,11 +300,11 @@ The WebJobs SDK looks for the storage connection string in the Application Setti
298300

299301
1. In the new file, add a `AzureWebJobsStorage` field, as in the following example:
300302

301-
```json
302-
{
303-
"AzureWebJobsStorage": "{storage connection string}"
304-
}
305-
```
303+
```json
304+
{
305+
"AzureWebJobsStorage": "{storage connection string}"
306+
}
307+
```
306308

307309
1. Replace *{storage connection string}* with the connection string that you copied previously.
308310

@@ -335,7 +337,7 @@ To trigger the function, build and run the project locally and create a message
335337

336338
1. Go back to the **Queue** window and refresh it. The message is gone. Your function running locally, successfully processed the message.
337339

338-
1. Close the console window or type **Ctrl+C**.
340+
1. Close the console window or type **Ctrl+C**.
339341

340342
It's now time to publish your WebJobs SDK project to Azure.
341343

@@ -516,14 +518,14 @@ Input bindings simplify code that reads data. For this example, the queue messag
516518

517519
1. In **Program.cs**, in the `ConfigureWebJobs` extension method, add the `AddAzureStorageBlobs` method on the [`HostBuilder`](/dotnet/api/microsoft.extensions.hosting.hostbuilder) instance (before the `Build` command) to initialize the Storage extension. At this point, the `ConfigureWebJobs` method looks like this:
518520

519-
```cs
520-
builder.ConfigureWebJobs(b =>
521-
{
522-
b.AddAzureStorageCoreServices();
523-
b.AddAzureStorageQueues();
524-
b.AddAzureStorageBlobs();
525-
});
526-
```
521+
```cs
522+
builder.ConfigureWebJobs(b =>
523+
{
524+
b.AddAzureStorageCoreServices();
525+
b.AddAzureStorageQueues();
526+
b.AddAzureStorageBlobs();
527+
});
528+
```
527529

528530
1. Create a blob container in your storage account.
529531

0 commit comments

Comments
 (0)