Omicron Llama

Coding all day, every day.

ASP.NET Modules – reading InputStream with StreamReader

If, like me, if you’ve gotten into habit of wrapping most things that implement IDisposable in a ‘using’ statement, then you’ll be wondering why the hell you can’t read from the Requests’s InputStream property using a StreamReader without your request pipeline breaking.

Turns out, that you mustn’t dispose of the StreamReader immediately, as when you do this it will implicitly dispose of your InputStream also.

So, declare your StreamReader as a private member of your Module class, and only explicitly Dispose of it within the pre-canned Dispose method in the Module class.

Like this:

 

Goes without saying, remember to use BinaryReader if you’re not reading plain text in the input stream (in this case, I’m getting the payload of a POST request).

Leave a Reply

Your email address will not be published. Required fields are marked *