This sample demonstrates a web application that leverages the durable task scheduler to manage recurring background tasks. Users can create, update, pause, resume, and delete schedules through REST endpoints provided by the ScheduleController. A dedicated orchestrator (CacheClearingOrchestrator) is used to perform periodic operations, such as clearing caches, by automatically triggering the scheduled tasks at configured intervals.
- .NET 8 SDK or later
- Docker (for running the emulator) installed
- Azure CLI (if using a deployed Durable Task Scheduler)
There are two ways to run this sample locally:
The emulator simulates a scheduler and taskhub in a Docker container, making it ideal for development and learning.
-
Pull the Docker Image for the Emulator:
docker pull mcr.microsoft.com/dts/dts-emulator:latest
-
Run the Emulator:
docker run -it -p 8080:8080 -p 8082:8082 mcr.microsoft.com/dts/dts-emulator:latest
Wait a few seconds for the container to be ready.
Note: The example code automatically uses the default emulator settings (endpoint: http://localhost:8080, taskhub: default). You don't need to set any environment variables.
Local development with a deployed scheduler:
-
Install the durable task scheduler CLI extension:
az upgrade az extension add --name durabletask --allow-preview true -
Create a resource group in a region where the Durable Task Scheduler is available:
az provider show --namespace Microsoft.DurableTask --query "resourceTypes[?resourceType=='schedulers'].locations | [0]" --out tableaz group create --name my-resource-group --location <location>
-
Create a durable task scheduler resource:
az durabletask scheduler create \ --resource-group my-resource-group \ --name my-scheduler \ --ip-allowlist '["0.0.0.0/0"]' \ --sku-name "Dedicated" \ --sku-capacity 1 \ --tags "{'myattribute':'myvalue'}" -
Create a task hub within the scheduler resource:
az durabletask taskhub create \ --resource-group my-resource-group \ --scheduler-name my-scheduler \ --name "my-taskhub" -
Grant the current user permission to connect to the
my-taskhubtask hub:subscriptionId=$(az account show --query "id" -o tsv) loggedInUser=$(az account show --query "user.name" -o tsv) az role assignment create \ --assignee $loggedInUser \ --role "Durable Task Data Contributor" \ --scope "/subscriptions/$subscriptionId/resourceGroups/my-resource-group/providers/Microsoft.DurableTask/schedulers/my-scheduler/taskHubs/my-taskhub"
The sample includes smart detection of the environment and configures authentication automatically:
- For local development with the emulator (when endpoint is http://localhost:8080), no authentication is required.
- For local development with a deployed scheduler, DefaultAzure authentication is used, which utilizes DefaultAzureCredential behind the scenes and tries multiple authentication methods:
- Managed Identity
- Environment variables (AZURE_CLIENT_ID, AZURE_TENANT_ID, AZURE_CLIENT_SECRET)
- Azure CLI login
- Visual Studio login
- and more
The connection string is constructed dynamically based on the environment:
// For local emulator
connectionString = $"Endpoint={schedulerEndpoint};TaskHub={taskHubName};Authentication=None";
// For Azure deployed emulator
connectionString = $"Endpoint={schedulerEndpoint};TaskHub={taskHubName};Authentication=DefaultAzure";Once you have set up either the emulator or deployed scheduler, follow these steps to run the sample:
-
If you're using a deployed scheduler, you need to set Environment Variables.
export ENDPOINT=$(az durabletask scheduler show \ --resource-group my-resource-group \ --name my-scheduler \ --query "properties.endpoint" \ --output tsv) export TASKHUB="my-taskhub"
-
Run the application
cd samples/durable-task-sdks/dotnet/ScheduleWebApp dotnet runThe application will start and listen on
http://localhost:5000by default.
Learn how to set up identity-based authentication when you deploy the app Azure.
The sample provides a list of schedule management endpoints that can be found at samples/durable-task-sdks/dotnet/ScheduleWebApp/ScheduleWebApp.http.
The file contains sample REST requests for testing the schedule management endpoints. It includes examples for creating, retrieving, listing, updating, pausing, resuming, and deleting schedules. These examples are designed for use with REST client tools (e.g., the VS Code REST Client extension).
A schedule defines how and when an orchestration is triggered on a recurring basis. In this sample, the schedule is used to automatically invoke the CacheClearingOrchestrator at regular intervals. The ScheduleCreationOptions object includes:
- id: A unique identifier for the schedule.
- orchestrationName: The name of the orchestrator that the schedule will trigger.
- interval: The recurring frequency (in TimeSpan format, e.g., hh:mm:ss) for the orchestrator invocation.
- orchestrationInput: Optional data passed to the orchestrator when it runs.
- startAt/endAt: Optional properties to set a specific start or end time for the recurrence.
- startImmediatelyIfLate: When set to true, ensures that if the schedule has missed its trigger time, it will run as soon as possible.
You can monitor your scheduled tasks using the Durable Task Scheduler dashboard. Get the dashboard URL using:
az durabletask taskhub show \
--resource-group my-resource-group \
--scheduler-name my-scheduler \
--name "schedule-webapp" \
--query "properties.dashboardUrl" \
--output tsvThe application uses the following configuration:
DURABLE_TASK_SCHEDULER_CONNECTION_STRING- Connection string for durable task schedulerASPNETCORE_ENVIRONMENT- Application environment (Development/Production)
These can be configured through:
- Environment variables
- User secrets (during development)
- appsettings.json