Esempio n. 1
0
        private static void OnSuccess <T>(Task task, IAgent agent, ISegment segment, bool holdTransactionOpen, Action <T> onComplete, TaskContinueWithOption options, TaskContinuationOptions?continuationOptions) where T : Task
        {
            segment.RemoveSegmentFromCallStack();

            if (task == null)
            {
                return;
            }

            ITransaction transaction = null;

            if (holdTransactionOpen)
            {
                transaction = agent.CurrentTransaction;
                transaction.Hold();
            }

            if (task.IsCompleted)
            {
                EndSegment(task);
            }
            else if (options == TaskContinueWithOption.None)
            {
                if (!continuationOptions.HasValue)
                {
                    task.ContinueWith(EndSegment);
                }
                else
                {
                    task.ContinueWith(EndSegment, continuationOptions.Value);
                }
            }
            else
            {
                task.ContinueWith(EndSegment, CancellationToken.None, TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Default);
            }

            void EndSegment(Task completedTask)
            {
                agent.HandleExceptions(EndSegmentWithPossibleException);

                void EndSegmentWithPossibleException()
                {
                    onComplete?.Invoke(completedTask as T);

                    if (completedTask != null && completedTask.IsFaulted)
                    {
                        segment.End(completedTask.Exception);
                    }
                    else
                    {
                        segment.End();
                    }

                    transaction?.Release();
                }
            }
        }
Esempio n. 2
0
        public static AfterWrappedMethodDelegate GetAsyncDelegateFor <T>(IAgent agent, ISegment segment, TaskContinueWithOption options) where T : Task
        {
            return(GetDelegateFor <Task>(
                       onFailure: segment.End,
                       onSuccess: InvokeOnSuccess
                       ));

            void InvokeOnSuccess(Task task)
            {
                OnSuccess <Task>(task, agent, segment, false, null, options, null);
            }
        }