public async Task WriteElevationRequest(ElevationRequest elevationRequest) { // Using Binary instead of Newtonsoft.JSON to reduce load times. var ms = new System.IO.MemoryStream(); new BinaryFormatter() { TypeFormat = System.Runtime.Serialization.Formatters.FormatterTypeStyle.TypesAlways, Binder = new MySerializationBinder() } .Serialize(ms, elevationRequest); ms.Seek(0, System.IO.SeekOrigin.Begin); byte[] lengthArray = BitConverter.GetBytes(ms.Length); Logger.Instance.Log($"ElevationRequest length {ms.Length}", LogLevel.Debug); await ControlStream.WriteAsync(lengthArray, 0, sizeof(int)).ConfigureAwait(false); await ControlStream.WriteAsync(ms.ToArray(), 0, (int)ms.Length).ConfigureAwait(false); await ControlStream.FlushAsync().ConfigureAwait(false); }