/// <summary> /// Runs a delegate in the context of an operation. /// </summary> /// <typeparam name="T">The return type from the function.</typeparam> /// <typeparam name="TCorrelationContext">The type of correlation context that will be used by the provider.</typeparam> /// <param name="provider">The Operation Provider that will create operations.</param> /// <param name="operationName">The name of the operation that will be created.</param> /// <param name="ctx">The correlation context to restore for this operation.</param> /// <param name="function">The delegate Action</param> /// <returns>The result of the async operation, awaited.</returns> public static async Task <T> RunAsync <T, TCorrelationContext>(this IOperationProvider <TCorrelationContext> provider, string operationName, TCorrelationContext ctx, RunOperationAsyncDelegate <T> function) { if (provider == null) { throw new ArgumentNullException(nameof(provider)); } if (string.IsNullOrWhiteSpace(operationName)) { throw new ArgumentNullException(nameof(operationName)); } if (function == null) { throw new ArgumentNullException(nameof(function)); } using (IOperation createdOperation = provider.CreateOperation(operationName, ctx)) { try { T result = await function(createdOperation); createdOperation.SetOperationResult(OperationResult.Success); return(result); } catch (Exception ex) { createdOperation.SetOperationResult(ex); System.Runtime.ExceptionServices.ExceptionDispatchInfo.Capture(ex).Throw(); throw ex; } } }
/// <summary> /// Runs a delegate in the context of an operation. /// </summary> /// <typeparam name="TCorrelationContext">The type of correlation context that will be used by the provider.</typeparam> /// <param name="provider">The Operation Provider that will create operations.</param> /// <param name="operationName">The name of the operation that will be created.</param> /// <param name="action">The delegate Action</param> /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns> public static async Task RunAsync(this IOperationProvider provider, string operationName, RunOperationAsyncDelegate action) { if (provider == null) { throw new ArgumentNullException(nameof(provider)); } if (string.IsNullOrWhiteSpace(operationName)) { throw new ArgumentNullException(nameof(operationName)); } if (action == null) { throw new ArgumentNullException(nameof(action)); } using (IOperation createdOperation = provider.CreateOperation(operationName)) { try { await action(createdOperation); createdOperation.SetOperationResult(OperationResult.Success); } catch (Exception ex) { createdOperation.SetOperationResult(ex); System.Runtime.ExceptionServices.ExceptionDispatchInfo.Capture(ex).Throw(); throw ex; } } }