ORLEANS0216: Post durable tasks from grains or durable task bodies
| Property | Value |
|---|---|
| Severity | Warning |
| Category | Usage |
| Code fix | None |
Code outside a grain and outside a durable task body calls Post on a durable task definition or configuration, for example in a client or a web endpoint:
public sealed class CheckoutEndpoint(IOrderWorkflows orders){ // A client has no durable outbox, so Post throws InvalidOperationException. public void Checkout(Guid orderId) => orders.PlaceOrderAsync(orderId).WithId($"place-order-{orderId:N}").Post();}Rule description
Section titled “Rule description”Post is synchronous: it stages the start in the durable outbox of the grain which runs the calling code, and the start commits with that grain’s next journal write, together with the state changes which the calling code made. A grain method, a durable task body, and a DurableTask.Run delegate run in a grain. A client has no durable outbox, so Post throws InvalidOperationException there, and the checkout fails.
For how tasks start, see Starting and observing tasks.
How to fix violations
Section titled “How to fix violations”Schedule the task with ScheduleAsync. It completes once the task’s grain has accepted and stored the task, and returns the task’s handle. Name the task with WithId, so a retry after an ambiguous failure, such as a timeout, reaches the same task:
public sealed class ScheduledCheckoutEndpoint(IOrderWorkflows orders){ // Returns once the order's grain has accepted and stored the task. A retry with the same ID reaches the same task. public async Task<string> CheckoutAsync(Guid orderId) { var workflow = await orders.PlaceOrderAsync(orderId).WithId($"place-order-{orderId:N}").ScheduleAsync(); return workflow.Id.ToString(); }}Post from a durable grain. An ordinary method of a durable grain posts the start, which commits atomically with the grain’s state changes in its next journal write:
public sealed class BasketGrain : DurableGrain{ private readonly IOrderWorkflows _orders; private readonly IDurableValue<Guid?> _checkedOut;
public BasketGrain(IOrderWorkflows orders) { _orders = orders; _checkedOut = StateManager.GetOrAddValue<Guid?>("checked-out"); }
// In a durable grain, Post stages the start in the grain's outbox, and it commits with the grain's state change. public async Task CheckoutAsync(Guid orderId) { _checkedOut.Value = orderId; _ = _orders.PlaceOrderAsync(orderId).WithId($"place-order-{orderId:N}").Post(); await WriteStateAsync(); }}When to suppress
Section titled “When to suppress”Suppress the diagnostic in a helper which only grains or durable task bodies call, such as a static method which a workflow uses to start follow-up work.
