title: Kestrel throw "Invalid UTF-8" when client send text with multiple-byte characters and length greater than buffer for ReceiveAsync
body: <p>Trying to use <code>System.Net.WebSockets.Websocket</code> with ASP.Net Core, but getting a problem when receiving Chinese text.</p>
  
  <p>This is my code:</p>
  
  <pre><code>while (_socket.State == WebSocketState.Open)
  {
      var result = await _socket.ReceiveAsync(_reciveBufferSegment, _source.Token);
  
      if (_source.Token.IsCancellationRequested) return;
      if (result.MessageType != WebSocketMessageType.Close)
      {
          if (_source.Token.IsCancellationRequested) return;
          await _recivedData.WriteAsync(_reciveBuffer, 0, result.Count);
          if (result.EndOfMessage)
          {
              _recivedData.Position = 0;
              var data = _recivedData.ToArray();
              _recivedData.SetLength(0);
              switch (result.MessageType)
              {
                  case WebSocketMessageType.Binary:
                      if (OnBinaryMessage != null)
                          Task.Run(() =&gt; OnBinaryMessage(this, data));
                      break;
                  case WebSocketMessageType.Text:
                      string decode = null;
                      try
                      {
                          decode = Encoding.UTF8.GetString(data);
                      }
                      catch
                      {
                          decode = "UTF8 decode failure";
                      }
                      if (OnTextMessage != null)
                          Task.Run(() =&gt; OnTextMessage(this, decode));
                      break;
              }
          }
      }
  }
  </code></pre>
  
  <p>If client sends a 50 bytes text with multiple-byte characters and server receive with buffer smaller than 50 and will cut multiple-byte characters, an <code>Invalid UTF-8</code> exception will occur.</p>
  
  <p>I port the same code to .Net Framework 4.52 and host on IIS and it works fine, did I miss something? Or any config that can disable the utf-8 check?</p>
  
  <p><a href="https://github.com/dynameis/WebSocketTest" rel="nofollow">testable solution github</a></p>