Beispiel #1
0
        Task <HttpResponseMessage> DoEnd(Task <HttpResponseMessage> t1, HttpRequestMessage request, CancellationToken cancellationToken)
        {
            t1.Wait(cancellationToken);
            HttpResponseMessage result;

            if (t1.IsCanceled)
            {
                LogRecorder.MonitorTrace("操作被取消");
                result = request.ToResponse(ApiResult.Error(ErrorCode.Ignore, "服务器正忙", "操作被取消"));
                LogRecorder.EndAllStepMonitor();
                LogRecorder.EndMonitor();
                return(Task <HttpResponseMessage> .Factory.StartNew(() => result, cancellationToken));
            }
            if (t1.IsFaulted)
            {
                LogRecorder.MonitorTrace(t1.Exception?.Message);
                LogRecorder.Exception(t1.Exception);
                result = request.ToResponse(ApiResult.Error(ErrorCode.UnknowError, "未知错误", t1.Exception?.Message));
            }
            else
            {
                result = t1.Result;
            }

            /*
             * result.ContinueWith((task, state) => , null,
             *  TaskContinuationOptions.AttachedToParent | TaskContinuationOptions.ExecuteSynchronously);
             */
            return(Task <HttpResponseMessage> .Factory.StartNew(() =>
            {
                OnEnd(request, result, cancellationToken);
                return result;
            }, cancellationToken));
        }