Azure / durabletask

Durable Task Framework allows users to write long running persistent workflows in C# using the async/await capabilities.
Apache License 2.0
1.47k stars 287 forks source link

Microsoft.Extensions.Hosting/DependencyInjection wrappers available #389

Open jviau opened 4 years ago

jviau commented 4 years ago

I have created wrapper libraries for using DurableTask with the Microsoft.Extensions.Hosting & DependencyInjection pattern. This allows all orchestrations, task activities, and middleware to be DI'd. It also adds a builder pattern for configuring a task hub worker. This is all done without any modifications to the DurableTask.Core framework.

The preview packages are available on nuget: https://www.nuget.org/packages/Vio.DurableTask.Hosting https://www.nuget.org/packages/Vio.DurableTask.DependencyInjection

See the repo, including instructions and samples here: jviau/durabletask-hosting

Quick sample:

Host.CreateDefaultBuilder()
    .ConfigureServices(services =>
    {
        // Can configure orchestrations, activities, and middleware in the service
        // container with any scope desired.
        services.AddSingleton<MySingletonOrchestration>();
        services.AddScoped<MyScopedMiddleware>(); // must implement "ITaskMiddleware"
    })
    .ConfigureTaskHubWorker((context, builder) =>
    {
        // add orchestration service
        builder.WithOrchestrationService(new LocalOrchestrationService());

        // add orchestration directly _not_ in service container. Will be treated as transient.
        builder.AddOrchestration<MyTransientOrchestration>();

        // will be fetched from service provider.
        builder.AddOrchestration<MySingletonOrchestration>();

        // will be fetched from service provider.
        builder.UseOrchestrationMiddleware<MyScopedMiddleware>();

        // same as orchestration: can be part of the services or not.
        builder.AddActivity<MyTransientActivity>();
    })
    .RunConsoleAsync(); // starts the worker.

Feel free to open issues on that repo for feature requests, or to discuss.

If plugs like this are not allowed or an issue is not the appropriate place to announce this, please close/remove this issue.

ConnorMcMahon commented 4 years ago

I think it might be worthwhile in our ReadMe to add a reference to some helpful projects built on top of Durable Task. We could link to this, as well as Durable Functions.