Quantcast
Channel: Brian Pedersen's Sitecore and .NET Blog
Viewing all articles
Browse latest Browse all 285

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

$
0
0

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:

 


Viewing all articles
Browse latest Browse all 285

Trending Articles