倒带请求体流 [英] Rewind request body stream

查看:252
本文介绍了倒带请求体流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我重新实现的请求记录仪作为Owin中间件它记录所有传入请求的请求URL和身体。 我能够读取身体,但如果我这样做了身体参数在我的控制器为null。

我猜这是因为空流​​的位置是在结束所以一无所有当它试图反序列化人体阅读。我在网页API的previous版本类似的问题,但是却能将Stream位置返回到0。这个特殊的流抛出一个该流不支持查找操作例外。

在最新版本的Web API 2.0的我可以打电话给 Request.HttpContent.ReadAsStringAsync()我的请求记录里面,身体仍然会到达的控制器圆通。

我怎么能看完倒带流呢?

我如何读取请求主体不消耗呢?

 公共类RequestLoggerMiddleware:OwinMiddleware
{
    公共RequestLoggerMiddleware(OwinMiddleware旁边)
        :基地(下)
    {
    }    公众覆盖任务调用(IOwinContext上下文)
    {
        返回Task.Run(()=> {
            绳体=新的StreamReader(context.Request.Body).ReadToEnd();
            //日志体            context.Request.Body.Position = 0; //不能设置流位置返回到0
            Console.WriteLine(context.Request.Body.CanSeek); //打印假
            this.Next.Invoke(上下文);
        });
    }
}


 公共类SampleController:ApiController
{
    公共无效后(ModelClass体)
    {
        //身体现在是空,如果中间件读它
    }
}


解决方案

刚刚找到一个解决方案。以新流取代了原有的流包含的数据。

 公众覆盖任务调用(IOwinContext上下文)
    {
        返回Task.Run(()=> {
            绳体=新的StreamReader(context.Request.Body).ReadToEnd();
            //日志体            字节[] =的RequestData Encoding.UTF8.GetBytes(体);
            context.Request.Body =新的MemoryStream(的RequestData);
            this.Next.Invoke(上下文);
        });
    }

如果你正在处理大量数据,我敢肯定的FileStream 也将工作作为替代品。

I am re-implementing a request logger as Owin Middleware which logs the request url and body of all incoming requests. I am able to read the body, but if I do the body parameter in my controller is null.

I'm guessing it's null because the stream position is at the end so there is nothing left to read when it tries to deserialize the body. I had a similar issue in a previous version of Web API but was able to set the Stream position back to 0. This particular stream throws a This stream does not support seek operations exception.

In the most recent version of Web API 2.0 I could call Request.HttpContent.ReadAsStringAsync()inside my request logger, and the body would still arrive to the controller in tact.

How can I rewind the stream after reading it?

or

How can I read the request body without consuming it?

public class RequestLoggerMiddleware : OwinMiddleware
{
    public RequestLoggerMiddleware(OwinMiddleware next)
        : base(next)
    {
    }

    public override Task Invoke(IOwinContext context)
    {
        return Task.Run(() => {
            string body = new StreamReader(context.Request.Body).ReadToEnd();
            // log body

            context.Request.Body.Position = 0; // cannot set stream position back to 0
            Console.WriteLine(context.Request.Body.CanSeek); // prints false
            this.Next.Invoke(context);
        });
    }
}


public class SampleController : ApiController 
{
    public void Post(ModelClass body)
    {
        // body is now null if the middleware reads it
    }
}

解决方案

Just found one solution. Replacing the original stream with a new stream containing the data.

    public override Task Invoke(IOwinContext context)
    {
        return Task.Run(() => {
            string body = new StreamReader(context.Request.Body).ReadToEnd();
            // log body

            byte[] requestData = Encoding.UTF8.GetBytes(body);
            context.Request.Body = new MemoryStream(requestData);
            this.Next.Invoke(context);
        });
    }

If you are dealing with larger amounts of data, I'm sure a FileStream would also work as the replacement.

这篇关于倒带请求体流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆