ASP.NET Core MVC 2.2批处理请求中间件

时间:2018-12-11 22:28:21

标签: asp.net-mvc .net-core odata c#-7.0 asp.net-core-middleware

我想要一个简单的中间件,可以将多个请求组合成一个请求,并将结果作为单个数组响应返回。

我不想使用OData,因为它太重了,而且我不喜欢它。

我不知道如何将一个HttpContext分成多个小的内部子HttpContext's

这是我的尝试:

    public static IApplicationBuilder BatchRequest(this IApplicationBuilder app)
    {
        app.Map("/api/batch", builder =>
        {
            builder.Use(async (context, next) =>
            {
                string[] paths = (context.Request.Query.Get("path") as StringValues?) ?? new string[] { };
                Stream originalBody = context.Response.Body;
                RecyclableMemoryStreamManager _recyclableMemoryStreamManager = new RecyclableMemoryStreamManager();

                IEnumerable<string> responses = await paths.SelectAsync(async path =>
                {
                    context.Request.Path = path;
                    MemoryStream newResponseBody = _recyclableMemoryStreamManager.GetStream();
                    context.Response.Body = newResponseBody;
                    await next.Invoke();
                    return RequestResponseLoggingMiddleware.ReadStreamInChunks(newResponseBody);
                });

                await context.Response.WriteAsync(responses.Serialize());
            });
        });

        return app;
    }

有这个例子,但是我不确定如何使用它:https://github.com/Tornhoof/HttpBatchHandler

请客气。

0 个答案:

没有答案