/// <summary> /// Creates a tcp message for use in the tcp processor for sending messgaes over tcp /// </summary> /// <param name="message">the message to wrap</param> public TcpMessage(Message message) { Ensure.NotNull(message, nameof(message)); MessageType = message.GetType(); Log.Debug("Message MsgId=" + message.MsgId + " MsgTypeId=" + message.GetType().Name + " to be wrapped."); var ms = new MemoryStream(); #pragma warning disable 618 using (var writer = new BsonDataWriter(ms)) #pragma warning restore 618 { writer.WriteStartObject(); writer.WritePropertyName(MessageType.FullName); writer.WriteValue(JsonConvert.SerializeObject(message, Settings.JsonSettings)); writer.WriteEnd(); } Data = new ArraySegment <byte>(ms.ToArray()); WrappedMessage = message; }
protected byte[] SerializeUsingNewtonsoftWriter <T>(T value, bool mustBeNested = false) { using (var memoryStream = new MemoryStream()) using (var newtonsoftWriter = new Newtonsoft.Json.Bson.BsonDataWriter(memoryStream)) { if (mustBeNested) { newtonsoftWriter.WriteStartObject(); newtonsoftWriter.WritePropertyName("x"); } var newtonsoftSerializer = new Newtonsoft.Json.JsonSerializer(); newtonsoftSerializer.Serialize(newtonsoftWriter, value); if (mustBeNested) { newtonsoftWriter.WriteEndObject(); } return(memoryStream.ToArray()); } }