ORLEANS0206: Run each durable task definition once
| Property | Value |
|---|---|
| Severity | Warning |
| Category | Usage |
| Code fix | None |
A local variable holds a definition which an async DurableTask method in the same compilation returned, and code runs it a second time: it awaits, schedules, or posts the variable twice, or in a loop which doesn’t create the definition:
public async DurableTask RemindAsync(string customer, int reminders){ var remind = SendReminder(customer); for (var i = 0; i < reminders; i++) { await DurableTask.Delay(TimeSpan.FromDays(1)).WithId($"wait-{i}");
// The definition ran in the first iteration, so the second iteration throws. await remind.WithId($"remind-{i}"); }}Rule description
Section titled “Rule description”An async DurableTask method returns a single-use definition: it holds the method’s state machine, which runs once. The first iteration of the loop runs the definition. The second iteration’s run throws InvalidOperationException with the message A deferred durable task definition can execute only once., which fails the task after its first reminder.
For definitions, see Starting and observing tasks.
How to fix violations
Section titled “How to fix violations”Call the method for each run. Each call creates a definition for one run:
public async DurableTask RemindEachDayAsync(string customer, int reminders){ for (var i = 0; i < reminders; i++) { await DurableTask.Delay(TimeSpan.FromDays(1)).WithId($"wait-{i}");
// Each call creates a new definition for this iteration's run. await SendReminder(customer).WithId($"remind-{i}"); }}Keep a factory instead of a definition. A local function or a delegate which calls the method creates a definition each time the body runs it:
public async DurableTask RemindTwiceAsync(string customer){ // A factory creates a definition for each run. DurableTask Remind() => SendReminder(customer);
await Remind().WithId("first-reminder"); await DurableTask.Delay(TimeSpan.FromDays(3)).WithId("grace"); await Remind().WithId("final-reminder");}When to suppress
Section titled “When to suppress”Suppress the diagnostic when the paths which reach the second run are mutually exclusive, so each execution runs the definition once.
