HTTPnet
HTTPnet copied to clipboard
Disposing response body stream
In your example files are sent to client this way:
context.HttpContext.Response.Body = File.OpenRead(filename);
But Stream returned by function OpenRead is never disposed explicitly. It is not possible to write to the file until Stream is disposed later by garbage collector.
I think that it is possible to dispose it after call to ResponseWriter.WriteAsync in file HttpSessionHandler.cs. Something like this:
try
{
var httpContext = new HttpContext(httpRequest, httpResponse, _clientSession, this);
await _clientSession.HandleHttpRequestAsync(httpContext);
if (httpContext.Response != null)
{
await ResponseWriter.WriteAsync(httpContext.Response, _clientSession.CancellationToken);
HttpNetTrace.Verbose(nameof(HttpSessionHandler), "Response '{0}' sent to '{1}'.", httpContext.Response.StatusCode, _clientSession.Client.Identifier);
}
if (httpContext.CloseConnection)
{
_clientSession.Close();
}
}
finally
{
httpResponse.Body?.Dispose();
}
Hi, thanks for reporting this issue. I wil fix this in the next version. Best regards Christian