public void Deserialize(IReceiveContext context) { Envelope result; using (var inputStream = new NonClosingStream(context.BodyStream)) using (var bsonReader = new BsonReader(inputStream)) { result = Deserializer.Deserialize <Envelope>(bsonReader); } context.SetUsingEnvelope(result); context.SetMessageTypeConverter(new JsonMessageTypeConverter(Deserializer, result.Message as JToken, result.MessageType)); }
public void Send(string queryString) { _client = new TcpClient(_host, _port) { ReceiveTimeout = 5000, NoDelay = true }; var headerString = new StringBuilder(); headerString.AppendFormat("GET {0} HTTP/1.1\nHost: {1}\n", queryString, _host); RequestHeaders["Connection"] = "close"; foreach (var headerName in RequestHeaders.AllKeys) { headerString.AppendFormat("{0}: {1}\n", headerName, RequestHeaders.Get(headerName)); } Stream = new NonClosingStream(_client.GetStream()); var queryHeaders = Encoding.ASCII.GetBytes(headerString.ToString() + '\n'); Stream.Write(queryHeaders, 0, queryHeaders.Length); // No using statement, we don't want the stream to be disposed var textReader = new UnbufferedStreamReader(Stream); foreach (var line in textReader.ReadUntil("\r\n\r\n") .Split(new[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries) .Where(line => !string.IsNullOrEmpty(line))) { if (line.Contains(':')) { ResponseHeaders.Add(line); } else if (line.IndexOf("HTTP/", StringComparison.Ordinal) == 0) { StatusCode = int.Parse(line.Substring(9, 3)); } } if (!LogRequest) { return; } Console.WriteLine("{2}: http://{0}{1}", _host, queryString, StatusCode); Console.WriteLine(ResponseHeaders.ToString()); }
public void TestNonClosingStream() { MemoryStream ms = new MemoryStream(new byte[5]); Assert.AreEqual(5, ms.Length); using (Stream s = new NonClosingStream(ms)) s.Close(); Assert.AreEqual(5, ms.Length); Assert.AreEqual(0, ms.Position); Assert.IsTrue(ms.CanRead); Assert.AreEqual(5, ms.Read(new byte[10], 0, 10)); }
public void Serialize <T>(Stream output, ISendContext <T> context) where T : class { context.SetContentType(ContentTypeHeaderValue); Envelope envelope = Envelope.Create(context); using (var outputStream = new NonClosingStream(output)) using (var bsonWriter = new BsonWriter(outputStream)) { Serializer.Serialize(bsonWriter, envelope); bsonWriter.Flush(); } }
public void Deserialize(IReceiveContext context) { Envelope result; using (var inputStream = new NonClosingStream(context.BodyStream)) { result = ProtoBuf.Serializer.Deserialize <Envelope>(inputStream); } if (result == null || result.Message == null) { throw new Exception("ProtoBufMessageSerializer::Deserialize. Error Deserializing message."); } context.SetUsingEnvelope(result); context.SetMessageTypeConverter(new StaticMessageTypeConverter(result.Message)); }
public void Serialize <T>(Stream output, ISendContext <T> context) where T : class { try { context.SetContentType(ContentTypeHeaderValue); Envelope envelope = Envelope.Create(context); var json = new StringBuilder(1024); using (var stringWriter = new StringWriter(json, CultureInfo.InvariantCulture)) using (var jsonWriter = new JsonTextWriter(stringWriter)) { jsonWriter.Formatting = Newtonsoft.Json.Formatting.None; JsonMessageSerializer.Serializer.Serialize(jsonWriter, envelope); jsonWriter.Flush(); stringWriter.Flush(); } using (var stringReader = new StringReader(json.ToString())) using (var jsonReader = new JsonTextReader(stringReader)) { var document = (XDocument)XmlSerializer.Deserialize(jsonReader, typeof(XDocument)); using (var nonClosingStream = new NonClosingStream(output)) using (var streamWriter = new StreamWriter(nonClosingStream)) using (var xmlWriter = new XmlTextWriter(streamWriter)) { document.WriteTo(xmlWriter); } } } catch (SerializationException) { throw; } catch (Exception ex) { throw new SerializationException("Failed to serialize message", ex); } }
private static Stream ConvertToNonClosingStream(Stream source) { var result = new NonClosingStream(); if (source.CanSeek) { source.Seek(0, SeekOrigin.Begin); } source.CopyTo(result); if (source.CanSeek) { source.Seek(0, SeekOrigin.Begin); } result.Seek(0, SeekOrigin.Begin); return result; }
private static Stream ConvertToNonClosingStream(Stream source) { var result = new NonClosingStream(); if (source.CanSeek) { source.Seek(0, SeekOrigin.Begin); } source.CopyTo(result); if (source.CanSeek) { source.Seek(0, SeekOrigin.Begin); } result.Seek(0, SeekOrigin.Begin); return(result); }
public void Deserialize(IReceiveContext context) { try { XDocument document; using (var nonClosingStream = new NonClosingStream(context.BodyStream)) using (var xmlReader = new XmlTextReader(nonClosingStream)) document = XDocument.Load(xmlReader); var json = new StringBuilder(1024); using (var stringWriter = new StringWriter(json, CultureInfo.InvariantCulture)) using (var jsonWriter = new JsonTextWriter(stringWriter)) { jsonWriter.Formatting = Newtonsoft.Json.Formatting.None; XmlSerializer.Serialize(jsonWriter, document.Root); } Console.WriteLine("Json: {0}", json); Envelope result; using (var stringReader = new StringReader(json.ToString())) using (var jsonReader = new JsonTextReader(stringReader)) { result = JsonMessageSerializer.Deserializer.Deserialize <Envelope>(jsonReader); } context.SetUsingEnvelope(result); context.SetMessageTypeConverter(new JsonMessageTypeConverter(JsonMessageSerializer.Deserializer, result.Message as JToken, result.MessageType)); } catch (SerializationException) { throw; } catch (Exception ex) { throw new SerializationException("Failed to deserialize message", ex); } }