public static async Task LeaseStock([ActivityTrigger] LeaseStockCommand command, ILogger logger) { logger.LogWarning("Ordering item"); // Simulate a hard work ordering the item await Task.Delay(100); // Oh wait! If there was no stock, throw. if (string.Equals(command.Item, "inexistent", StringComparison.InvariantCultureIgnoreCase)) { logger.LogWarning("There was no stock aivalable!"); throw new InvalidOperationException("There is no stock of product 'unexistent'"); } }
private static async Task <StockLeasedEvent> LeaseStock(IDurableOrchestrationContext context, LeaseStockCommand command) { // Start LeaseStock activity await context.CallActivityAsync("Example2-Events-LeaseStock", command); // Wait for completion var stockLeasedTask = context.WaitForExternalEvent <StockLeasedEvent>($"stock-leased"); var stockLeaseDeniedTask = context.WaitForExternalEvent <StockLeaseDeniedEvent>($"stock-lease-denied"); var completedTask = await Task.WhenAny( stockLeasedTask, stockLeaseDeniedTask); if (completedTask == stockLeaseDeniedTask) { throw new CompensationEventDetectedException(stockLeaseDeniedTask.Result); } return(stockLeasedTask.Result); }