Exemple #1
0
        /// <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="ctx">The correlation context to restore for this operation.</param>
        /// <param name="action">The delegate Action</param>
        /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
        public static async Task RunAsync <TCorrelationContext>(this IOperationProvider <TCorrelationContext> provider, string operationName, TCorrelationContext ctx, 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, ctx))
            {
                try
                {
                    await action(createdOperation);

                    createdOperation.SetOperationResult(OperationResult.Success);
                }
                catch (Exception ex)
                {
                    createdOperation.SetOperationResult(ex);
                    System.Runtime.ExceptionServices.ExceptionDispatchInfo.Capture(ex).Throw();
                    throw ex;
                }
            }
        }
Exemple #2
0
        /// <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="function">The delegate Action</param>
        public static T Run <T, TCorrelationContext>(this IOperationProvider <TCorrelationContext> provider, string operationName, RunOperationDelegate <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))
            {
                try
                {
                    T result = function(createdOperation);
                    createdOperation.SetOperationResult(OperationResult.Success);
                    return(result);
                }
                catch (Exception ex)
                {
                    createdOperation.SetOperationResult(ex);
                    System.Runtime.ExceptionServices.ExceptionDispatchInfo.Capture(ex).Throw();
                    throw ex;
                }
            }
        }
Exemple #3
0
        /// <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>
        public static void Run(this IOperationProvider provider, string operationName, RunOperationDelegate 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
                {
                    action(createdOperation);
                    createdOperation.SetOperationResult(OperationResult.Success);
                }
                catch (Exception ex)
                {
                    createdOperation.SetOperationResult(ex);
                    System.Runtime.ExceptionServices.ExceptionDispatchInfo.Capture(ex).Throw();
                    throw ex;
                }
            }
        }