public static byte[] ToBytes(IMessageEncryptor encryptor, IList <Message> messages) { if (messages == null) { throw new ArgumentNullException(nameof(messages)); } using (var ms = new MemoryStream()) { var binaryWriter = new BinaryWriter(ms); var scaleoutMessage = new ScaleoutMessage(messages); var buffer = scaleoutMessage.ToBytes(); binaryWriter.Write(buffer.Length); binaryWriter.Write(buffer); return(encryptor.Encrypt(ms.ToArray())); } }