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.