The NuGet package Microsoft.Azure.Functions.Worker.Extensions.DurableTask is key here together with Microsoft.DurableTask.Generators, introducing the TaskOrchestrationContext, the context used on Orchestrator Functions, and the base classes for Orchestrators TaskOrchestrator and Activities TaskActivity.
Class-based types are only possible with source generators, allowing for context extensions to be generated on the fly, and consumed in Orchestrators.
These packages are not compatible with Durable Functions for the in-process .NET worker. It only works with the newer out-of-process .NET Isolated worker.
Have you heard about the Durable Task Framework before? This is explained on the Coding Night NZ YouTube video, but you can also check more details on the DTF Github.
It is worth mentioning that class-based types are likely to change, so it is better to continue with function-syntax for now.
The Example
This is a simple chaining pattern example, to demo the Isolated Durable Function. If you are not familiar with the Chaining pattern, check this article.
In this article, I’m not explaining the major components of Azure Durable Functions, but you can get more details about it in this article, or through the Coding Night NZ YouTube video.
This is a simple demo where the Starter exposes the HTTPTrigger endpoint, which calls the Orchestrator that in a chain Say Hello to the cities.
Starter Function (aka DurableClient)
1
2
3
4
5
6
7
8
9
10
11
12
13
[Function(nameof(StarterAsync))]publicasyncTask<HttpResponseData>StarterAsync([HttpTrigger(AuthorizationLevel.Function,"get","post")]HttpRequestDatareq,[DurableClient]DurableClientContextdurableContext){_logger.LogInformation("C# HTTP trigger function processed a request.");stringinstanceId=awaitdurableContext.Client.ScheduleNewOrchestrationInstanceAsync(nameof(SayHelloOrchestrator));_logger.LogInformation("Created new orchestration with instance ID = {instanceId}",instanceId);returndurableContext.CreateCheckStatusResponse(req,instanceId);}
This is an HTTPTrigger function that can be triggered with this CURL request:
[DurableTask(nameof(SayHelloActivity))]publicclassSayHelloActivity:TaskActivity<string,string>{privatereadonlyILogger_logger;publicSayHelloActivity(ILoggerFactoryloggerFactory){_logger=loggerFactory.CreateLogger<SayHelloActivity>();}publicoverrideTask<string>RunAsync(TaskActivityContextcontext,stringcityName){_logger.LogInformation("Saying hello to {name}",cityName);returnTask.FromResult($"Hello, {cityName}!");}}
Join the conversation! Share your thoughts and connect with other readers.