Ejemplo n.º 1
0
 /// <summary>Get the serialized representation of the request body.</summary>
 /// <typeparam name="T">The request body type.</typeparam>
 /// <param name="content">The request body content.</param>
 /// <param name="request">The HTTP request to handle.</param>
 /// <param name="formatter">The media type formatter which will serialize the request body.</param>
 protected string GetSerialized <T>(T content, HttpRequestMessage request, MediaTypeFormatterBase formatter)
 {
     using MemoryStream stream = new MemoryStream();
     using StreamReader reader = new StreamReader(stream);
     formatter.Serialize(typeof(string), content, stream, request.Content, this.NullTransportContext);
     stream.Position = 0;
     return(reader.ReadToEnd());
 }
 /// <summary>Get the serialized representation of the request body.</summary>
 /// <typeparam name="T">The request body type.</typeparam>
 /// <param name="content">The request body content.</param>
 /// <param name="request">The HTTP request to handle.</param>
 /// <param name="formatter">The media type formatter which will serialize the request body.</param>
 protected string GetSerialized <T>(T content, HttpRequestMessage request, MediaTypeFormatterBase formatter)
 {
     using (MemoryStream stream = new MemoryStream())
         using (StreamReader reader = new StreamReader(stream))
         {
             formatter.Serialize(typeof(string), content, stream, request.Content, null);
             stream.Position = 0;
             return(reader.ReadToEnd());
         }
 }
        /// <summary>Get the serialized representation of the request body.</summary>
        /// <param name="type">The request body type.</param>
        /// <param name="content">The request body content.</param>
        /// <param name="request">The HTTP request to handle.</param>
        /// <param name="formatter">The media type formatter which will serialize the request body.</param>
        protected object GetDeserialized(Type type, string content, HttpRequestMessage request, MediaTypeFormatterBase formatter)
        {
            using (MemoryStream stream = new MemoryStream())
                using (StreamWriter writer = new StreamWriter(stream))
                {
                    // write content
                    writer.Write(content);
                    writer.Flush();
                    stream.Position = 0;

                    // deserialize
                    return(formatter.Deserialize(type, stream, request.Content, null));
                }
        }