title: HttpSelfHostServer connection actively refused
body: <p>I'm encountering a very strange issue with <code>HttpSelfHostServer</code> in .NET: The HTTP server <strong>works fine for 12-24 hours</strong> (I haven't figured out the real cause of its failure), and after that it starts actively refusing connections from clients. The odd thing: Not even killing the server process and restarting it helps; Only a clean reboot of the server machine will fix the problem. I have reproduced the exact same behavior on 3 different machines, so it's not a problem with my local configuration. To me it looks like a problem somewhere deep down inside windows or .NET.</p>
  
  <p>Here's the exception I get on client machines:</p>
  
  <pre><code>System.AggregateException: One or more errors occurred. ---&gt; System.Net.Http.HttpRequestException: An error occurred while sending the request. ---&gt; System.Net.WebException: Die zugrunde liegende Verbindung wurde geschlossen: Unerwarteter Fehler beim Senden.. ---&gt; System.IO.IOException: Von der Übertragungsverbindung können keine Daten gelesen werden: Eine vorhandene Verbindung wurde vom Remotehost geschlossen. ---&gt; System.Net.Sockets.SocketException: Eine vorhandene Verbindung wurde vom Remotehost geschlossen
     at System.Net.Sockets.Socket.EndReceive(IAsyncResult asyncResult)
     at System.Net.Sockets.NetworkStream.EndRead(IAsyncResult asyncResult)
     --- End of inner exception stack trace ---
     at System.Net.TlsStream.EndWrite(IAsyncResult asyncResult)
     at System.Net.PooledStream.EndWrite(IAsyncResult asyncResult)
     at System.Net.ConnectStream.WriteHeadersCallback(IAsyncResult ar)
     --- End of inner exception stack trace ---
     at System.Net.HttpWebRequest.EndGetRequestStream(IAsyncResult asyncResult, TransportContext&amp; context)
     at System.Net.Http.HttpClientHandler.GetRequestStreamCallback(IAsyncResult ar)
     --- End of inner exception stack trace ---
     --- End of inner exception stack trace ---
     at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
     at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)
     at System.Threading.Tasks.Task`1.get_Result()
     at ProELS.Car.Client.ProELSCarClient.SendMessageEnvelope(MessageEnvelope envelope, String host) in c:\Source\Kunden\ARB\ProELS.Car\ProELS.Car.Client\ProELSCarClient.cs:line 520
     at ProELS.Car.Client.ProELSCarClient.SendMessageEnvelope(MessageEnvelope envelope) in c:\Source\Kunden\ARB\ProELS.Car\ProELS.Car.Client\ProELSCarClient.cs:line 485
     at ProELS.Car.Client.ProELSCarClient.TestAuthentication() in c:\Source\Kunden\ARB\ProELS.Car\ProELS.Car.Client\ProELSCarClient.cs:line 239
     at ProELS.Car.TestClient.Form1.btnTryAuthentication_Click(Object sender, EventArgs e) in c:\Source\Kunden\ARB\ProELS.Car.TestClient\ProELS.Car.TestClient\Form1.cs:line 170
     at System.Windows.Forms.Control.OnClick(EventArgs e)
     at System.Windows.Forms.Button.OnClick(EventArgs e)
     at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
     at System.Windows.Forms.Control.WmMouseUp(Message&amp; m, MouseButtons button, Int32 clicks)
     at System.Windows.Forms.Control.WndProc(Message&amp; m)
     at System.Windows.Forms.ButtonBase.WndProc(Message&amp; m)
     at System.Windows.Forms.Button.WndProc(Message&amp; m)
     at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&amp; m)
     at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&amp; m)
     at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
  ---&gt; (Inner Exception #0) System.Net.Http.HttpRequestException: An error occurred while sending the request. ---&gt; System.Net.WebException: Die zugrunde liegende Verbindung wurde geschlossen: Unerwarteter Fehler beim Senden.. ---&gt; System.IO.IOException: Von der Übertragungsverbindung können keine Daten gelesen werden: Eine vorhandene Verbindung wurde vom Remotehost geschlossen. ---&gt; System.Net.Sockets.SocketException: Eine vorhandene Verbindung wurde vom Remotehost geschlossen
     at System.Net.Sockets.Socket.EndReceive(IAsyncResult asyncResult)
     at System.Net.Sockets.NetworkStream.EndRead(IAsyncResult asyncResult)
     --- End of inner exception stack trace ---
     at System.Net.TlsStream.EndWrite(IAsyncResult asyncResult)
     at System.Net.PooledStream.EndWrite(IAsyncResult asyncResult)
     at System.Net.ConnectStream.WriteHeadersCallback(IAsyncResult ar)
     --- End of inner exception stack trace ---
     at System.Net.HttpWebRequest.EndGetRequestStream(IAsyncResult asyncResult, TransportContext&amp; context)
     at System.Net.Http.HttpClientHandler.GetRequestStreamCallback(IAsyncResult ar)
     --- End of inner exception stack trace ---&lt;---
  </code></pre>
  
  <p>And here's the code I use to start the server:</p>
  
  <pre><code> private void StartCarServer()
          {
  
              HttpSelfHostServer carServer;
              var config = new ExtendedHttpSelfHostConfiguration("https://localhost:8082");
  
              config.Routes.MapHttpRoute("API Default", "api/{controller}/{id}", new { id = RouteParameter.Optional });
              config.Routes.MapHttpRoute("DefaultApiWithAction", "api/{controller}/{action}/{id}", new { id = RouteParameter.Optional });
  
              carServer = new HttpSelfHostServer(config);
              carServer.OpenAsync().Wait();
          }
  </code></pre>
  
  <p>My only possible explanation is that maybe the <code>HttpSelfHostServer</code> is not really made for clean TLS/SSL usage, since it was a pretty bad configuration nightmare to get it working in the first place?</p>