Read from Azure Queue with Azure WebJobs 3.0 and .NET Core

From WebJobs 2.0 to WebJobs 3.0 Microsoft have completely rewritten the way Azure WebJobs is used. The reasons are probably noble, but they require you to redo your work when upgrading. So I made this template that allows me to start up a Azure WebJob in .NET Core.

STEP 1: THE PROJECT

The project is a .NET Core Console Application.

STEP 2: THE NUGET PACKAGES

These packages change with the next upgrade but for WebJobs 3.0 you will need these packages:

<ItemGroup>
  <PackageReference Include="WindowsAzure.Storage" Version="9.3.3" />
  <PackageReference Include="Microsoft.Azure.WebJobs" Version="3.0.5" />
  <PackageReference Include="Microsoft.Azure.WebJobs.Extensions" Version="3.0.2" />
  <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.Storage" Version="3.0.4" />
  <PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="2.2.0" />
  <PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="2.2.0" />
  <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.2.0" />
  <PackageReference Include="Microsoft.Extensions.Logging" Version="2.2.0" />
  <PackageReference Include="Microsoft.Extensions.Logging.Console" Version="2.2.0" />
  <PackageReference Include="Newtonsoft.Json" Version="12.0.1" />
</ItemGroup>

STEP 3: THE APPSETTINGS.JSON

Remember to include your AzureWebJobsStorage connection string in the appsettings.json:

{
  "ConnectionStrings": {
    "AzureWebJobsStorage": "DefaultEndpointsProtocol=https;AccountName=xxx;AccountKey=xxx=="
  }
}

STEP 4: THE PROGRAM.CS

This program.cs will start your console application as a Azure Webjob:

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace FeedTestEventReceiver
{
  class Program
  {
    public static void Main(string[] args)
    {
      var host = new HostBuilder()
        .ConfigureServices(
        (hostContext, services) =>
        {
          services.Configure<HostOptions>(
            option =>
            {
              option.ShutdownTimeout = System.TimeSpan.FromSeconds(20);
            }
          );
        }
        )
        .ConfigureLogging( (context, b) => { b.AddConsole(); })
        .ConfigureWebJobs( c => { c.AddAzureStorage(); } 
        )
        .ConfigureServices( (context, services) => 
        {
          services.AddTransient<QueueService, QueueService>();
        }
      )
      .Build();

      host.RunAsync().Wait();
    }

  }
}

The major differences between 2.0 and 3.0 are:

  • The HostBuilder is used to configure the WebJob
  • Your Webjob endpoints are now configured using ConfigureServices. You  will need to change the highlighted line of code and include your own endpoint.

STEP 5: THE QUEUSERVICE

This is just an example on how to implement a WebJob endpoint. This class is added in the AddTransient line of code in the program.cs and will listen to a queue for events:

using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;
using System.Threading.Tasks;
using Microsoft.WindowsAzure.Storage;

namespace FeedTestEventReceiver
{
  public class QueueService
  {
    public async Task ReadFromQueue([QueueTrigger("queuename")] string message, ILogger log)
    {
        log.LogInformation($"{message}");
    }
  }
}

STEP 6: THE TEST

To test the WebJob, simply start the console application with F5, and the WebJob will run locally listening for events int the specified queue:

Azure WebJob

Azure WebJob

MORE TO READ:

 

Advertisements

About briancaos

Developer at Pentia A/S since 2003. Have developed Web Applications using Sitecore Since Sitecore 4.1.
This entry was posted in .NET Core, c# and tagged , , , . Bookmark the permalink.

1 Response to Read from Azure Queue with Azure WebJobs 3.0 and .NET Core

  1. Baran Ozdemir says:

    Hi Brian,

    Thanks for providing with a nice and easy sample. Interestingly it does not trigger the ReadFromQueue when I add a new message to the queue. The queue name is correct. I checked everything one by one. Has it happened to you?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.