Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

StreamReader/Writer are disposing Request/Response body #7

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/ThothSerializer.fs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type ThothSerializer (?isCamelCase : bool, ?extra : ExtraCoders, ?skipNullField
fun (next: HttpFunc) (ctx: HttpContext) ->
task {
ctx.SetContentType "application/json; charset=utf-8"
use stream = new System.IO.StreamWriter(ctx.Response.Body, Utf8EncodingWithoutBom, DefaultBufferSize)
use stream = new System.IO.StreamWriter(ctx.Response.Body, Utf8EncodingWithoutBom, DefaultBufferSize, true)
use jsonWriter = new JsonTextWriter(stream)
do! body.WriteToAsync(jsonWriter)
return Some ctx
Expand All @@ -37,7 +37,7 @@ type ThothSerializer (?isCamelCase : bool, ?extra : ExtraCoders, ?skipNullField
ctx.SetStatusCode 200
ctx.SetContentType "application/json; charset=utf-8"
use stream =
new System.IO.StreamWriter(ctx.Response.Body, Utf8EncodingWithoutBom, DefaultBufferSize)
new System.IO.StreamWriter(ctx.Response.Body, Utf8EncodingWithoutBom, DefaultBufferSize, true)
use jsonWriter = new JsonTextWriter(stream)
jsonWriter.WriteStartArray()
for item in items do
Expand All @@ -53,7 +53,7 @@ type ThothSerializer (?isCamelCase : bool, ?extra : ExtraCoders, ?skipNullField

static member ReadBodyRaw (ctx: HttpContext) =
task {
use stream = new System.IO.StreamReader(ctx.Request.Body, Utf8EncodingWithoutBom)
use stream = new System.IO.StreamReader(ctx.Request.Body, Utf8EncodingWithoutBom, true, DefaultBufferSize, true)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @MaxDeg ,

I don't know the SteamReaderAPI, why is this one having 3 new parameters instead of just one like the others?
Should we use the 3 parameters or use something like ?leaveOpen = true?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello,

Sadly StreamReader only have one constructor that allow to provide leaveOpen option as last parameter. None of those parameters are optional so we need to provide them all.

use jsonReader = new JsonTextReader(stream)
let! json = JValue.ReadFromAsync jsonReader
return json
Expand Down