Skip to content

ORLEANS0221: Run synchronous work with DurableTask.RunInline

PropertyValue
SeverityInfo
CategoryUsage
Code fixNone

A durable task body awaits a DurableTask.Run step where it defines it, optionally named or shielded, and the step’s lambda awaits nothing, returns no task, and doesn’t read the CancellationToken which the step receives:

public async DurableTask<Charge> ReserveAsync(string orderKey, decimal amount)
{
// A synchronous state change, which the step runs as a child execution with a journal write of its own.
await DurableTask.Run(_ => { _status.Value = OrderStatus.Reserved; }).WithId("reserve");
return await DurableTask.Run(ct => _payments.ChargeAsync(orderKey, amount, ct)).WithId("charge");
}

DurableTask.Run runs its delegate as a child execution, with its own DurableTask.Current and cancellation scope, when the body awaits the step, possibly in a later turn. The step commits its result in a journal write of its own.

DurableTask.RunInline runs synchronous work in the current work item and records its outcome with the execution’s next journal write, together with the state changes which the effect makes. Consecutive effects commit in the same journal write. In the fix, the status change and its record commit with the execution’s next journal write instead of a write of their own.

A step and an effect at the same call site use the same identifier, and the execution records which kind ran. A replay which finds a step recorded where the body now runs an effect diverges, so a deployment with pending executions branches the change on DurableTask.GetVersion.

For effects and steps, see Effects and steps.

Run the work as an effect. Pass the delegate to DurableTask.RunInline with the step’s name:

public async DurableTask<Charge> ReserveInlineAsync(string orderKey, decimal amount)
{
// The effect runs in the current work item, and its record commits with the execution's next journal write.
DurableTask.RunInline("reserve", () => { _status.Value = OrderStatus.Reserved; });
return await DurableTask.Run(ct => _payments.ChargeAsync(orderKey, amount, ct)).WithId("charge");
}

Branch the change on a version. While executions which recorded the step are pending, the body runs the effect for new executions and keeps the step for executions which recorded it:

public async DurableTask<Charge> ReserveVersionedAsync(string orderKey, decimal amount)
{
if (InlineReservation.IsAtLeast(1))
{
DurableTask.RunInline("reserve", () => { _status.Value = OrderStatus.Reserved; });
}
else
{
// Executions which recorded the step replay it.
#pragma warning disable ORLEANS0221
await DurableTask.Run(_ => { _status.Value = OrderStatus.Reserved; }).WithId("reserve");
#pragma warning restore ORLEANS0221
}
return await DurableTask.Run(ct => _payments.ChargeAsync(orderKey, amount, ct)).WithId("charge");
}

Suppress the diagnostic for a step which needs a child execution of its own, for example for its own cancellation scope, and in the branch which executions that recorded the step replay.