giraffe-fsharp / Giraffe

A native functional ASP.NET Core web framework for F# developers.

Home Page:https://giraffe.wiki

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Never decalre reader with `use` on `ctx.Request.Body`

xperiandri opened this issue · comments

this

use reader = new StreamReader(ctx.Request.Body, Encoding.UTF8)

disposes body, because the reader disposes underlying stream.
Which must not be done in any case!

I did not get it, what is the problem with disposing the request body after reading it?

The problem is that Giraffe is not an owner of it. The owner of it is an appropriate ASP.NET core middleware.
Giraffe must not dispose the stuff it is not owner of.
It leads to unexpected behavior for the consumer.

I see, thanks for the explanation. I agree then!

Looks like the leaveOpen parameter could be specified to allow disposal of the StreamReader without disposing the underlying resource: doc reference

use reader = new StreamReader(ctx.Request.Body, Encoding.UTF8, leaveOpen = true)