Last post Oct 17, 2019 06:43 PM by mkonanki
Oct 16, 2019 03:52 PM|mkonanki|LINK
Could you please help me i am writing middleware for request and response .
i was using stream but need to convert PipeWriter and PipeReader.
Could you please help me how read body from PipeWriter .
i have written code for PipeReader.
private async Task<string> GetStringFromPipeReader(PipeReader reader)
StringBuilder stringBuilder = new StringBuilder();
// await some data being available
ReadResult read = await reader.ReadAsync();
ReadOnlySequence<byte> buffer = read.Buffer;
// check whether we've reached the end
// and processed everything
if (buffer.IsEmpty && read.IsCompleted)
break; // exit loop
// process what we received
foreach (var segment in buffer)
string asciString = Encoding.ASCII.GetString(
// tell the pipe that we used everything
private async Task WriteSomeDataAsync(PipeWriter writer, string jsonValue)
// use an oversized size guess
Memory<byte> workspace = writer.GetMemory();
// write the data to the workspace
int bytes = Encoding.ASCII.GetBytes(
// tell the pipe how much of the workspace
// we actually want to commit
// this is **not** the same as Stream.Flush!
Oct 16, 2019 06:55 PM|PatriceSc|LINK
You tried https://docs.microsoft.com/en-us/aspnet/core/fundamentals/middleware/request-response?view=aspnetcore-3.0 ?
Also please never post code without telling what explicitly what happens. It's not uncommon to spot multiple possible issues and then we have to gig deeper to guess which is the current problem you may have and sometimes it turns out to be something that
couldn't be even guessed from the code...
Meanwhile telling us what happens take 30 seconds of extra time.
Oct 17, 2019 06:43 PM|mkonanki|LINK
Thanks for your reply, i can able to read body from httpRequest using PipeReader , is there any way to read body from HttpResponse using pipeWriter.