Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class JsonContent : HttpContent
- {
- public object SerializationTarget { get; private set; }
- public JsonContent(object serializationTarget)
- {
- SerializationTarget = serializationTarget;
- this.Headers.ContentType = new MediaTypeHeaderValue("application/json");
- }
- protected override async Task SerializeToStreamAsync(Stream stream, TransportContext context)
- {
- using (StreamWriter writer = new StreamWriter(stream))
- using (JsonTextWriter jsonWriter = new JsonTextWriter(writer))
- {
- JsonSerializer ser = new JsonSerializer();
- ser.Serialize(jsonWriter, SerializationTarget);
- }
- // Should I dispose here or not ? When I do, the stream is 'closed'
- // When I do not dispose, the body seems to be empty
- // Should I Wrap this in a await Task.Run(() => {});
- // To make it 'async'
- }
- protected override bool TryComputeLength(out long length)
- {
- //we don't know. can't be computed up-front
- length = -1;
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement