Example #1
0
        public async Task Invoke(DownstreamContext context)
        {
            await _next.Invoke(context);

            if (context.IsError)
            {//自定义输出结果
                var errmsg     = context.Errors[0].Message;
                int httpstatus = _codeMapper.Map(context.Errors);
                var errResult  = new BaseResult()
                {
                    errcode = httpstatus, errmsg = errmsg
                };
                var message = errResult.ToJson();
                context.HttpContext.Response.StatusCode = (int)HttpStatusCode.OK;
                await context.HttpContext.Response.WriteAsync(message);

                return;
            }
            else if (context.HttpContext.Response.StatusCode != (int)HttpStatusCode.OK)
            {//标记失败,不做任何处理,自定义扩展时预留
            }
            else if (context.DownstreamResponse == null)
            {//添加如果管道强制终止,不做任何处理,修复未将对象实例化错误
            }
            else
            {//继续请求下游地址返回
                Logger.LogDebug("no pipeline errors, setting and returning completed response");
                await _responder.SetResponseOnHttpContext(context.HttpContext, context.DownstreamResponse);
            }
        }