title: ASP.NET MVC 4.5 WebSocket server loses connection after a few messages have been transferred
body: <p>I've just started to play around with WebSockets and ASP.NET and have run into a weird issue. I'm building a very primitive ASP.NET 4.5 WebAPI application that is supposed to function as an echo-server like so:</p>
  
  <pre><code>using Microsoft.Web.WebSockets;
  // ...
  
  namespace MyControllers
  {
      internal class EchoHandler : WebSocketHandler
      {
          public override void OnClose()
          {
              System.Diagnostics.Debug.Write("Close"); 
          }
  
          public override void OnError()
          {
              System.Diagnostics.Debug.Write("Error: " + this.Error.ToString()); 
          }
  
          public override void OnOpen()
          {
              System.Diagnostics.Debug.Write("Open"); 
          }
  
          public override void OnMessage(string message)
          {
              System.Diagnostics.Debug.Write("Message: " + message);
              this.Send("Echo: " + message);
          }
      }
  
      public class EchoController : ApiController
      {
          public HttpResponseMessage Get()
          {
              if (HttpContext.Current.IsWebSocketRequest)
              {
                  HttpContext.Current.AcceptWebSocketRequest(new EchoHandler());
                  return Request.CreateResponse(HttpStatusCode.SwitchingProtocols);
              }
              else
              {
                  return Request.CreateResponse(HttpStatusCode.BadRequest);
              }
          }
      }
  }
  </code></pre>
  
  <p>I'm connecting to this service using a Windows Store Application written in C#. The relevant code looks like this:</p>
  
  <pre><code>class WebsocketTest
  {
      private MessageWebSocket webSocket;
      private DataWriter messageWriter;
  
      private async Task Connect()
      {
          var server = new Uri("ws://127.0.0.1:81/");
  
          webSocket = new MessageWebSocket();
          webSocket.Control.MessageType = SocketMessageType.Utf8;
          webSocket.MessageReceived += messageWebSocket_MessageReceived;
          webSocket.Closed += messageWebSocket_Closed;
  
          await webSocket.ConnectAsync(server);
          messageWebSocket = webSocket;
          messageWriter = new DataWriter(webSocket.OutputStream);
      }
  
      private async Task Send(string message)
      {
          try
          {
              messageWriter.WriteString(message);
  
              await messageWriter.StoreAsync();
          }
          catch (Exception ex)
          {
              var error = WebSocketError.GetStatus(ex.GetBaseException().HResult);
          }
      }
  }
  </code></pre>
  
  <p>This works well for a while, but after an arbitrary number of messages have been sent back and forth, OnError() is invoked on the server and I get the following exception: "The I/O operation has been aborted because of either a thread exit or an application request" (It's the "this.Send(...)" that seems to be causing it). If I keep sending stuff on the client, I get a "ConnectionAborted" error when calling "dataWriter.StoreAsync()". 
  The error occurs every time, but it takes a varying number of messages before it does. Using longer messages seems to speed up the process. </p>
  
  <p>For testing, I also tried using plain AspNetWebSockets instead of a WebSocketHandler but with the same outcome.</p>
  
  <p>Any ideas?</p>
  
  <p>Thanks a ton in advance,
  Kai</p>