public static byte[] WriteCompletionMessage(MemoryBufferWriter writer, string protocolName) { // Written as a MessagePack 'arr' containing at least these items: // * A 'str': The name of the HubProtocol used for the serialization of the Completion Message // * [A serialized Completion Message which is a 'bin'] // Any additional items are discarded. var completionMessage = writer.DetachAndReset(); var msgPackWriter = new MessagePackWriter(writer); msgPackWriter.WriteArrayHeader(2); msgPackWriter.Write(protocolName); msgPackWriter.WriteBinHeader(completionMessage.ByteLength); foreach (var segment in completionMessage.Segments) { msgPackWriter.WriteRaw(segment.Span); } completionMessage.Dispose(); msgPackWriter.Flush(); return(writer.ToArray()); }