How to write a continuous Azure Webjob

Ning Zhu 0 Reputation points
2025-02-21T13:07:42.77+00:00

From this page, https://learn.microsoft.com/en-us/azure/app-service/webjobs-sdk-get-started, it only provide a sample for event driven webjobs, could you please provide a sample for continuous webjobs with WebJobs SDK along with some features, e.g. logging access other Azure services?

Thanks a lot

Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
8,930 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Bhargavi Naragani 5,270 Reputation points Microsoft External Staff Moderator
    2025-02-24T06:11:52.45+00:00

    Hi @Ning Zhu,

    Azure WebJobs SDK specialize in event-driven (triggered) WebJobs, but implementing a continuous WebJob is certainly possible.

    Here's a sample example of a continuous WebJob in C#:

    using Microsoft.Azure.WebJobs;
    using Microsoft.Extensions.Logging;
    using System;
    using System.Threading;
    namespace ContinuousWebJob
    {
        class Program
        {
            static void Main(string[] args)
            {
                var config = new JobHostConfiguration
                {
                    DashboardConnectionString = Environment.GetEnvironmentVariable("AzureWebJobsDashboard"),
                    StorageConnectionString = Environment.GetEnvironmentVariable("AzureWebJobsStorage")
                };
                if (config.IsDevelopment)
                {
                    config.UseDevelopmentSettings();
                }
                var host = new JobHost(config);
                // Keep running continuously
                while (true)
                {
                    try
                    {
                        Console.WriteLine("WebJob running at: " + DateTime.Now);
                        Thread.Sleep(10000); // Run every 10 seconds
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine($"Error: {ex.Message}");
                    }
                }
            }
        }
    }
    

    It just runs within a loop, sleeping for a specified interval. You can modify Thread.Sleep() according to your requirement.

    There will be writing messages to the console directly, which will be visible in the WebJob logs on the Azure portal. You can also use ILogger for more formal logging.

    You can add Azure SDKs to your project easily and access services such as Storage, Cosmos DB, Service Bus, etc. For example:

    using Azure.Storage.Blobs;
    var blobServiceClient = new BlobServiceClient("<your_connection_string>");
    var containerClient = blobServiceClient.GetBlobContainerClient("my-container");
    await containerClient.CreateIfNotExistsAsync();
    

    Zip the project output and deploy it through the Azure Portal, Azure CLI, or Visual Studio's Publish feature.

    Make sure to set your WebJob type as Continuous when creating it in the portal.

    Reference:
    WebJobs in Azure App Service
    Get started with the WebJobs SDK

    If this answers your query, do click Accept Answer and Yes for was this answer helpful. And, if you have any further query do let us know.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.