Skip to content

ORLEANS0222: Pass delegates which return a task for asynchronous work

PropertyValue
SeverityError
CategoryUsage
Code fixNone

DurableTask.Run or DurableTask.RunInline receives a delegate which returns void and binds an async function: an async lambda converted to Action or Action<CancellationToken>, an async void method, or a local which holds one:

public async DurableTask SubscribeAsync()
{
// SendWelcome returns at its first await, so the step records its completion before the email is sent.
await DurableTask.Run(SendWelcome).WithId("welcome");
}
private async void SendWelcome(CancellationToken cancellationToken) =>
await email.SendAsync(this.GetPrimaryKeyString(), "Welcome", cancellationToken);

An async void delegate returns to its caller at its first incomplete await. DurableTask.Run and DurableTask.RunInline record the delegate’s completion when it returns, so the record precedes the work which follows the await:

  • The step or the effect completes, and the body continues, while the work is still running.
  • A replay returns the record without running the delegate, so the work runs only in the first run.
  • An exception which the work throws after the await escapes the task instead of failing the step or the effect.

In the example, the step completes before the email is sent, and a failed send leaves the step’s record successful.

For steps and effects, see Effects and steps.

Return a task. DurableTask.Run with a delegate which returns a Task completes the step when the task completes, and records the task’s outcome:

public async DurableTask SubscribeAndWelcomeAsync()
{
// The step completes when the returned task completes, after the email is sent.
await DurableTask.Run(SendWelcomeAsync).WithId("welcome");
}
private Task SendWelcomeAsync(CancellationToken cancellationToken) =>
email.SendAsync(this.GetPrimaryKeyString(), "Welcome", cancellationToken);

Run the work in an async DurableTask method. The method is a step which the body awaits:

public async DurableTask SubscribeWithStepAsync() => await Welcome().WithId("welcome");
private async DurableTask Welcome() =>
await email.SendAsync(this.GetPrimaryKeyString(), "Welcome", DurableTask.Current.CancellationToken);

DurableTask.RunInline runs synchronous work only. Asynchronous work runs as a step.

Suppress the diagnostic only for fire-and-forget work which the task neither records nor waits for, and whose failures the delegate handles itself.