ORLEANS0204: Return the outputs of a DurableTask.RunInline effect
| Property | Value |
|---|---|
| Severity | Warning |
| Category | Usage |
| Code fix | None |
The delegate which DurableTask.RunInline receives assigns, increments, or decrements a variable which the enclosing body declares:
public async DurableTask<string> IssueAsync(){ var number = 0; var reference = Guid.Empty; DurableTask.RunInline("allocate", () => { // A replay returns the recorded result without running the effect, so these assignments don't happen. number = ++_lastNumber.Value; reference = Guid.NewGuid(); });
await Print(number, reference).WithId("print"); return $"{number}-{reference:N}";}Rule description
Section titled “Rule description”DurableTask.RunInline runs its effect once for the running task and records the effect’s result. A replay returns the recorded result without running the effect, so the assignments in the delegate happen only in the run which runs it. In every replay, number stays 0 and reference stays Guid.Empty: the replay passes other arguments to the recorded print step and returns another ticket than the first run computed.
The state changes which the effect makes, such as the increment of _lastNumber, are correct: they commit with the record, and the replay finds them in the grain’s state.
For recorded effects, see Recorded effects, local steps, and calls to other grains.
How to fix violations
Section titled “How to fix violations”Return every output. Return a tuple from the effect, and deconstruct it in the body:
public async DurableTask<string> IssueOnceAsync(){ // The effect returns its outputs, so every replay receives the recorded values. var (number, reference) = DurableTask.RunInline("allocate", () => (++_lastNumber.Value, Guid.NewGuid()));
await Print(number, reference).WithId("print"); return $"{number}-{reference:N}";}Return a record. A type with an Orleans serializer names the outputs, and the body can return it:
[GenerateSerializer, Immutable]public sealed record Allocation(int Number, Guid Reference, DateTimeOffset IssuedAt);
public async DurableTask<Allocation> IssueAllocationAsync(){ var allocation = DurableTask.RunInline( "allocate", () => new Allocation(++_lastNumber.Value, Guid.NewGuid(), DateTimeOffset.UtcNow));
await Print(allocation.Number, allocation.Reference).WithId("print"); return allocation;}Variables which the delegate declares, and grain state which it changes, need no change.
When to suppress
Section titled “When to suppress”Suppress the diagnostic for an assignment which the body reads only inside the effect, or only for diagnostics in the run which ran the effect.
