title: Unicode receiving with ASP.NET core websocket
body: <p>I'm trying to build a websocket server using ASP.NET core 1.1 websocket middleware that can handle text messages. My strategy is to use a fixed-size buffer to keep reading and decoding until the websocket message ends. 
  The middleware setup goes like this:</p>
  
  <pre><code>public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
      {
          loggerFactory.AddConsole(Configuration.GetSection("Logging"));
          loggerFactory.AddDebug();
  
          var logger = loggerFactory.CreateLogger("websocket");
  
          app.UseWebSockets();
  
          app.Use(async (http, next) =&gt;
          {
              if (!http.WebSockets.IsWebSocketRequest)
              {
                  await next();
                  return;
              }
  
              var websocket = await http.WebSockets.AcceptWebSocketAsync();
  
              while (websocket.State == WebSocketState.Open)
              {
                  var buffer = new ArraySegment&lt;byte&gt;(new byte[32]);
                  var charbuffer = new char[32];
                  try
                  {
                      var sb = new StringBuilder();
                      var decoder = Encoding.UTF8.GetDecoder();
  
                      //aha, we got a message
                      var detectResult = await websocket.ReceiveAsync(buffer, CancellationToken.None);
                      var receiveResult = detectResult;
  
                      while (!receiveResult.EndOfMessage)
                      {
                          var charLen = decoder.GetChars(buffer.Array, 0, receiveResult.Count, charbuffer, 0);
                          logger.LogInformation($"Decoded {charLen} byte(s) from wire");
                          sb.Append(charbuffer, 0, charLen);
                          receiveResult = await websocket.ReceiveAsync(buffer, CancellationToken.None);
                      }
                      var charLenFinal = decoder.GetChars(buffer.Array, 0, receiveResult.Count, charbuffer, 0);
                      logger.LogInformation($"Decoded {charLenFinal} byte(s) from wire");
                      sb.Append(charbuffer, 0, charLenFinal);
  
                      var message = sb.ToString();
                      logger.LogInformation($"decoded message: {message}");
                      await websocket.SendAsync(new ArraySegment&lt;byte&gt;(Encoding.UTF8.GetBytes("got it")), WebSocketMessageType.Text, true, CancellationToken.None);
                  }
                  catch (Exception ex)
                  {
                      logger.LogError(ex.Message);
                      logger.LogError(ex.InnerException?.Message ?? string.Empty);
                  }
              }
          });
      }
  </code></pre>
  
  <p>Now the code works well for text that include only ASCII characters. But when I tried to send Unicode text message (Vietnamese) whose length is longer than the buffer size, an exception occurs</p>
  
  <pre><code>fail: websocket[0]
    The remote party closed the WebSocket connection without completing the close handshake.
  fail: websocket[0]
       at System.Net.WebSockets.ManagedWebSocket.&lt;ReceiveAsyncPrivate&gt;d__60.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
       at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
       at MvcApp.Startup.&lt;&gt;c__DisplayClass5_0.&lt;&lt;Configure&gt;b__0&gt;d.MoveNext()
  </code></pre>
  
  <p>The exception occurs at the line</p>
  
  <pre><code>var detectResult = await websocket.ReceiveAsync(buffer, CancellationToken.None);
  </code></pre>
  
  <p>What could be the reason for this error?</p>