Tuesday, January 10, 2023

Azure Durable Functions in Azure Functions .NET Isolated Worker Process

Azure Durable Functions now supports .NET 7.0 running in the Isolated worker process. It's still in it's preview stage, but it's super exciting.

In this post, let's see how we can get ourselves started in Azure Durable Functions in Azure Functions .NET Isolated Worker Process.

First, after creating a Function App that targets .NET 7 Isolated functions worker, we need to install the NuGet package: Microsoft.Azure.Functions.Worker.Extensions.DurableTask. Make sure you have checked Included prerelease checkbox if you are trying to install the package through Visual Studio NuGet Package Manager.

Microsoft.Azure.Functions.Worker.Extensions.DurableTask
After installing the package, I have updated Function1.cs as follows adding a simple Durable Function.
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.DurableTask;
using Microsoft.Extensions.Logging;
 
namespace FunctionApp1;
 
public class Function1
{
    [Function(nameof(TriggerHello))]
    public async Task<HttpResponseData> TriggerHello([HttpTrigger(AuthorizationLevel.Anonymous, "get")] HttpRequestData req,
        [DurableClient] DurableClientContext durableContext,
        FunctionContext executionContext)
    {
        ILogger logger = executionContext.GetLogger(nameof(Function1));
 
        string instanceId = await durableContext.Client.ScheduleNewOrchestrationInstanceAsync(nameof(HelloPersonOrchestrator));
        logger.LogInformation("Created new orchestration with instance ID = {instanceId}", instanceId);
 
        return durableContext.CreateCheckStatusResponse(req, instanceId);
    }
 
    [Function(nameof(HelloPersonOrchestrator))]
    public static async Task<stringHelloPersonOrchestrator([OrchestrationTrigger] TaskOrchestrationContext context,
        FunctionContext executionContext)
    {
        ILogger logger = executionContext.GetLogger(nameof(HelloPersonOrchestrator));
 
        string result = await context.CallActivityAsync<string>(nameof(SayHello), "John Doe") + " ";
        result += await context.CallActivityAsync<string>(nameof(SayHello), "Jane Doe") + " ";
        result += await context.CallActivityAsync<string>(nameof(SayHello), "Joe Bloggs") + " ";
        result += await context.CallActivityAsync<string>(nameof(SayHello), "Fred Bloggs");
 
        logger.LogInformation("HelloPersonOrchestrator says: {output}", result);
 
        return result;
    }
 
    [Function(nameof(SayHello))]
    public static string SayHello([ActivityTrigger] string name, FunctionContext executionContext)
    {
        ILogger logger = executionContext.GetLogger(nameof(SayHello));
        logger.LogInformation("Saying hello to {name}", name);
        return $"Hello {name}!";
    }
}
And then we can call the HTTP  trigger to start the orchestration. And it works like a charm.
Output
Can't wait for this to go under GA.

Do try this out and if you see any issues, please do not hesitate to log an issue here: microsoft/durabletask-dotnet

Hope this helps.

Happy Coding.

Regards,
Jaliya

No comments:

Post a Comment