ORLEANS0213: Return a task from asynchronous code in durable task bodies
| Property | Value |
|---|---|
| Severity | Warning |
| Category | Usage |
| Code fix | None |
A durable task body declares an async void local function, or an async lambda which converts to a delegate that returns void, such as Action. ORLEANS0222 reports such a lambda which DurableTask.Run or DurableTask.RunInline receives:
public async DurableTask CloseAccountAsync(string customer){ await Close(customer).WithId("close");
// The body returns before the email is sent, and a failure to send escapes the body. NotifyClosed(customer);
async void NotifyClosed(string recipient) => await email.SendAsync(recipient, "Your account is closed", DurableTask.Current.CancellationToken);}Rule description
Section titled “Rule description”A durable task body observes the operations which it awaits: their results, their failures, and their cancellation. Asynchronous void code can’t be awaited. In the example:
- The body returns, and the task’s result commits, before the email is sent.
- A failure to send escapes the body, so the task completes successfully, and the exception surfaces outside the task.
- Durable operations which the code performs after its first
awaitrun after the body completed, outside the task’s execution.
For the rules and the replay model, see Replay-safety rules.
How to fix violations
Section titled “How to fix violations”Return a DurableTask and await it as a step. The body observes the step’s outcome, and a replay returns its record:
public async DurableTask CloseAccountAndNotifyAsync(string customer){ await Close(customer).WithId("close"); await NotifyClosed(customer).WithId("notify");
// A step which the body awaits: the body observes its completion and its failure, and a replay returns its record. async DurableTask NotifyClosed(string recipient) => await email.SendAsync(recipient, "Your account is closed", DurableTask.Current.CancellationToken);}Return a Task from callbacks. Use Func<Task> instead of Action for asynchronous callbacks, and await the callback in a step:
public async DurableTask CloseAccountWithCallbackAsync(string customer, Func<string, Task> onClosed){ await Close(customer).WithId("close");
// A delegate which returns a task, run as a step which the body awaits. await DurableTask.Run(_ => onClosed(customer)).WithId("on-closed");}When to suppress
Section titled “When to suppress”Don’t suppress this diagnostic. An event handler whose signature requires void belongs outside the durable task body, where it can start a durable task through its grain.
