Exemple #1
0
        internal override async Task Excute(HttpContext ctx, ILifetimeScope scope)
        {
            OxygenIocContainer.BuilderIocContainer(scope);//仅在当前请求内创建上下文模型
            byte[] result = new byte[0];
            ctx.Response.ContentType = "application/json";
            var messageType = MessageType.Json;

            try
            {
                if (ctx.Request.ContentType == "application/x-msgpack")
                {
                    ctx.Response.ContentType = "application/x-msgpack";
                    messageType = MessageType.MessagePack;
                }
                if (ctx.Request.ContentType == null)
                {
                    ctx.Response.ContentType = "text/html";
                    messageType = MessageType.Html;
                }
                HttpContextExtension.ContextWapper.Value = new OxygenHttpContextWapper(Path, scope, ctx.Request.Headers.GetHeaderDictionary(), ctx.Request.Cookies.GetCookieDictionary(), ctx.Response);
                Tout localCallbackResult = null;
                if (noInput)
                {
                    localCallbackResult = await LocalMethodAopProvider.UsePipelineHandler(scope, HttpContextExtension.ContextWapper.Value, NoInputMethodDelegate);
                }
                else
                {
                    var messageobj = await messageHandler.ParseMessage <Tin>(ctx, messageType);

                    if (messageobj == default(Tin))
                    {
                        throw new FormatException($"参数反序列化失败,接口地址{Path},入参类型:{typeof(Tin).Name}");
                    }
                    localCallbackResult = await LocalMethodAopProvider.UsePipelineHandler(scope, messageobj, HttpContextExtension.ContextWapper.Value, MethodDelegate);
                }
                if (localCallbackResult != null)
                {
                    result = messageHandler.BuildMessage(localCallbackResult, messageType);
                }
            }
            catch (Exception e)
            {
                logger.LogError($"服务端消息处理异常: {e.GetBaseException()?.Message ?? e.Message}");
                ctx.Response.StatusCode = 502;
                if (e is FormatException)
                {
                    await ctx.Response.Body.WriteAsync(Encoding.UTF8.GetBytes(e.Message));
                }
            }
            finally
            {
                await ctx.Response.Body.WriteAsync(result, 0, result.Length);

                OxygenIocContainer.DisposeIocContainer();//注销上下文
            }
        }