Click here to Skip to main content
15,398,590 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
We're working on a Web API and need to read (part of) the stream in middleware for logging purposes. I came across the HttpRequestStreamReader[^] at some point, however, I can't find why this would be preferred over a regular StreamReader. Does anyone know?

What I have tried:

Reading the documentation, searching the web
Posted
Comments
Gerry Schmitz 1-Jun-22 1:42am
   
It may just be a namespace packaging convenience ("web utilities"). And the http version doesn't have a "block read".
Member 15627495 1-Jun-22 3:48am
   
Http is at network/server level,

streamreader prefers hard-drives/memory

sure it's a special class for Http. dedicated to http. a highway for the use.
Maarten Kools 1-Jun-22 4:08am
   
But is there a real difference in implementation? Because I skimmed through the code, and the only obvious difference I can see is that the HttpRequestStreamReader rents a buffer from the pool, while the StreamReader just creates a new buffer. So I assume this alleviates memory pressure. Aside from that, it reads a stream, and doesn't seem to care where the stream comes from.

1 solution

You're coding in .Net environment :

The Memory is often managed and auto-clean ( finalize function in class / dispose for lots of components / and GC as big helper ).

As I told you, you evolve in 'network buffer' for request / web server.

The HttpRequestStreamReader and streamreader are both stream, sure.
a streamreader can handle the http one.

but for simple use, the dedicated class HttpRequestStreamReader offers you dedicated features in its class.
that the point,

it's not for "handle level" in .Net, it's for "facilities and uses, and features".

StreamReader is from "system.IO"
HttpRequestStreamReader is part of ASP namespace, but a TextStream ( reader )
HttpRequestStreamReader Class (Microsoft.AspNetCore.WebUtilities) | Microsoft Docs[^]
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900