Azure Durable Function (C#)

Traditional Azure Function & AWS Lambda are stateless.

In Azure Durable Function, one of the impressive features is of saving state between each run, consider if you want a simple cloud service triggered everyday and no information needs to be preserved in database, but only some states to save for next execution, then Azure Durable Function is a good solution. It is of great use in Delta Query.

Here is an example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
    public static class ATDeltaQueryMain
    {
        [FunctionName("TestMain")]
        public static async void RunAsync([TimerTrigger("0 0 0 * * *")]TimerInfo myTimer,
            [OrchestrationClient]IDurableOrchestrationClient starter,
            ILogger log)
        {
            string instanceId = await starter.StartNewAsync("TestOrchestrator","");
            log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
        }

        [FunctionName("TestOrchestrator")]
        public static async Task RunOrchestrator(
            [OrchestrationTrigger] IDurableOrchestrationContext context)
        {

            EntityId testEntityId = new EntityId(nameof(counterSync), "uniqueEntityId");

            await context.CallEntityAsync(testEntityId, "operationName");

        }

        [FunctionName("counterSync")]
        public static async Task TicketSync([EntityTrigger] IDurableEntityContext ctx)
        {
            int count = ctx.GetState<int>();
            Console.WriteLine(count);
            count += 1;
            ctx.SetState(count);
        }
    }

Leave a Reply

Your email address will not be published. Required fields are marked *