コード例 #1
0
        /// <summary>
        /// 重载
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        protected override Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            Request = request;
            int code = Check();

            //校验不通过,直接返回,不做任何处理
            if (code != 0)
            {
                LogRecorder.FlushMonitor("Authorization头校验出错");
                return(Task.Factory.StartNew(() =>
                {
                    var result = ApiResult.Error(code);
                    return new HttpResponseMessage(HttpStatusCode.OK)
                    {
                        Content = new StringContent(JsonConvert.SerializeObject(result))
                    };
                }, cancellationToken));
            }
            //正常结束时清理调用上下文
            var resultTask = base.SendAsync(request, cancellationToken);

            resultTask.ContinueWith((task, state) =>
            {
                CallContext.LogicalSetData("ApiContext", null);
            }, null, cancellationToken);

            return(resultTask);
        }