-
Notifications
You must be signed in to change notification settings - Fork 6
/
MessagePackMediaTypeFormatter.cs
43 lines (36 loc) · 1.22 KB
/
MessagePackMediaTypeFormatter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
using System;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Http.Formatting;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using MessagePack;
namespace WcfVsWebApiVsAspNetCoreBenchmark
{
public class MessagePackMediaTypeFormatter<T>: MediaTypeFormatter
{
public override bool CanReadType(Type type)
{
return true;
}
public override bool CanWriteType(Type type)
{
return true;
}
public override Task<object> ReadFromStreamAsync(Type type, Stream readStream, HttpContent content, IFormatterLogger formatterLogger)
{
var obj = MessagePackSerializer.NonGeneric.Deserialize(type, readStream);
return Task.FromResult(obj);
}
public override Task WriteToStreamAsync(Type type, object value, Stream writeStream, HttpContent content, TransportContext transportContext)
{
MessagePackSerializer.NonGeneric.Serialize(type, writeStream, value);
return Task.CompletedTask;
}
public MessagePackMediaTypeFormatter()
{
SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/x-msgpack"));
}
}
}