Describe the bug
I'm trying a bit around with EmbedIO and I realized that the first connection always takes really long until it gets closed after a response has been sent.
Usually this is not that much noticable when KeepAlive is set to true, since only the first request takes long and subsequent request are fast.
However I wanted to write a custom api middleware which closes the socket after each request, so I set KeepAlive is set to false, now every request takes really long.
To Reproduce
I have this minimal test setup
using System;
using Unosquare.Labs.EmbedIO;
using Unosquare.Labs.EmbedIO.Constants;
class EmbedIOTest
{
static void Main(string[] args)
{
using (var ws = new WebServer(9696))
{
ws.RegisterModule(new TestModule());
ws.RunAsync();
Console.ReadKey();
}
}
}
class TestModule : WebModuleBase
{
public override string Name => "TestModule";
public TestModule()
{
AddHandler(ModuleMap.AnyPath, HttpVerbs.Any, (ctx, ct) =>
{
ctx.Response.KeepAlive = false;
return ctx.StringResponseAsync("test");
});
}
}
The responses seem to differ on clients:
I'm running everything on localhost.
And I recommend using postman, as it shows the problem the most clear.
Expected behavior
The connection should be correctly closed.
Desktop:
I created the branch Issue267-KeepAlive
Seems like upgrading to 2.5.0 has fixed it, thanks.
Well, I have to reopen the issue because I have found the problem. Seems like I messed up my testsetup (I always referenced the ns1.3 dll).
The problem occours with the netstandard1.3 build of embedio.
This preprocessor directive is here to be because standard1.3 streams do not have a close method.
https://github.com/unosquare/embedio/blob/cc0d80cc049bbb71553ca182c734ce01bede6be0/src/Unosquare.Labs.EmbedIO/System.Net/ResponseStream.cs#L45-L49
The problem is that the Close method in the netstandard1.3 never gets called (Per visual studio 'find references' and manual checking)
My best suggestion would be to change the close to:
#if !NETSTANDARD1_3
public override void Close()
{
Dispose();
}
#endif
protected override void Dispose(bool disposing)
{
if (_disposed) return;
_disposed = true;
if (!disposing) return;
var ms = GetHeaders()
// ...
Let me check!
@Splamy I did some changes, can you check them? Or do you need a nuget?
I tested master and it works now, thanks.
I just published a new nuget: https://www.nuget.org/packages/EmbedIO/2.7.2
Let me know if I can close this issue.
Works like a charm. Thanks again.