Skip to content

running Durable Functions/Tasks as a self-hosted service in ASP.NET Core

License

Notifications You must be signed in to change notification settings

PejmanNik/durabletask-dotnet-aspnet

Repository files navigation

Self-Hosted Durable Task Worker in Asp .Net Core

This project enables running DurableTask without a sidecar project, allowing the worker to be self-hosted. It provides a seamless way to integrate Durable Task functionality directly into your Asp .Net Core project.

Usage

Install the NuGet version (Sisu.DurableTask.AspNetCore) package.

dotnet add package Sisu.DurableTask.AspNetCore

Note

To respect the DurableTask domain and reserve it for official packages, I added a prefix to the project assembly. In Finnish culture, sisu represents determination, perseverance, and resilience, which felt like a fitting touch!

Then, register the host with your preferred orchestration service

using DurableTask.AspNetCore;
using DurableTask.AzureStorage;

var orchestrationServiceAndClient = new AzureStorageOrchestrationService(new()
{
    StorageAccountClientProvider = new StorageAccountClientProvider("...."),
});

builder.Services.AddSelfHostedDurableTaskHub(orchestrationServiceAndClient);

Finally, register the durabletask-dotnet services and add UseSelfHosted to both the worker and client.

using Microsoft.DurableTask;
using Microsoft.DurableTask.Client;
using Microsoft.DurableTask.Worker;

// Add all the generated tasks
builder.Services.AddDurableTaskWorker(builder =>
{
    builder
        .AddTasks(r => r.AddAllGeneratedTasks());
        .UseSelfHosted();
});

builder.Services.AddDurableTaskClient(b => b.UseSelfHosted());

Now you can inject the DurableTaskClient into your classes to schedule or manage durable tasks. For more detailed examples, check out the samples folder.

Under the Hood

The durabletask-dotnet project is built on top of the Durable Task Framework. It provides an easy way to run durable tasks using Dependency Injection and IHostedService as a background service in your application. One of its standout features is a type-safe source generator for orchestrators and activities, making it a breeze to work with. However, "It's specifically designed to connect to a "sidecar" process, such as the Azure Functions .NET Isolated host, a special purpose sidecar container, or potentially even Dapr.", so it use gRPC to communicate with the sidecar. In this project the gRPC communication is replaced with a direct call to the DurableTaskHub service that runs in the same process.

Acknowledgements

durabletask-dotnet for providing the core Durable Task framework.