Advertisement
Guest User

Untitled

a guest
Sep 25th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.05 KB | None | 0 0
  1. public class JsonContent : HttpContent
  2. {
  3. public object SerializationTarget { get; private set; }
  4. public JsonContent(object serializationTarget)
  5. {
  6. SerializationTarget = serializationTarget;
  7. this.Headers.ContentType = new MediaTypeHeaderValue("application/json");
  8. }
  9. protected override async Task SerializeToStreamAsync(Stream stream, TransportContext context)
  10. {
  11. using (StreamWriter writer = new StreamWriter(stream))
  12. using (JsonTextWriter jsonWriter = new JsonTextWriter(writer))
  13. {
  14. JsonSerializer ser = new JsonSerializer();
  15. ser.Serialize(jsonWriter, SerializationTarget);
  16. }
  17.  
  18.  
  19.  
  20. // Should I dispose here or not ? When I do, the stream is 'closed'
  21.  
  22. // When I do not dispose, the body seems to be empty
  23.  
  24. // Should I Wrap this in a await Task.Run(() => {});
  25. // To make it 'async'
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32. }
  33.  
  34. protected override bool TryComputeLength(out long length)
  35. {
  36. //we don't know. can't be computed up-front
  37. length = -1;
  38. return false;
  39. }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement