Created
September 10, 2012 19:43
-
-
Save filipw/3693339 to your computer and use it in GitHub Desktop.
MessagePack for Web API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Web; | |
using System.Net.Http.Formatting; | |
using MsgPack.Serialization; | |
using System.Threading.Tasks; | |
using System.IO; | |
using System.Net.Http.Headers; | |
using System.Net; | |
using System.Net.Http; | |
using System.Collections; | |
using MsgPack; | |
using WebApi.MessagePack.Models; | |
namespace WebApi.MessagePack.Infrastructure | |
{ | |
public class MessagePackMediaTypeFormatter : MediaTypeFormatter | |
{ | |
private const string mime = "application/x-msgpack"; | |
Func<Type, bool> IsAllowedType = (t) => | |
{ | |
if (!t.IsAbstract && !t.IsInterface && t != null && !t.IsNotPublic) | |
return true; | |
return false; | |
}; | |
public MessagePackMediaTypeFormatter() | |
{ | |
SupportedMediaTypes.Add(new MediaTypeHeaderValue(mime)); | |
} | |
public override bool CanReadType(Type type) | |
{ | |
if (type == null) throw new ArgumentNullException("type is null"); | |
return IsAllowedType(type); | |
} | |
public override bool CanWriteType(Type type) | |
{ | |
if (type == null) throw new ArgumentNullException("Type is null"); | |
return IsAllowedType(type); | |
} | |
public override Task WriteToStreamAsync(Type type, object value, Stream stream, HttpContent content, TransportContext transportContext) | |
{ | |
if (type == null) throw new ArgumentNullException("type is null"); | |
if (stream == null) throw new ArgumentNullException("Write stream is null"); | |
var tcs = new TaskCompletionSource<object>(); | |
if (typeof(IEnumerable).IsAssignableFrom(type)) { | |
value = (value as IEnumerable<object>).ToList(); | |
} | |
var serializer = MessagePackSerializer.Create<dynamic>(); | |
serializer.Pack(stream, value); | |
tcs.SetResult(null); | |
return tcs.Task; | |
} | |
public override Task<object> ReadFromStreamAsync(Type type, Stream stream, HttpContent content, IFormatterLogger formatterLogger) | |
{ | |
var tcs = new TaskCompletionSource<object>(); | |
if (content.Headers != null && content.Headers.ContentLength == 0) return null; | |
try | |
{ | |
var serializer = MessagePackSerializer.Create(type); | |
object result; | |
using (var unpacker = Unpacker.Create(stream)) | |
{ | |
unpacker.Read(); | |
result = serializer.UnpackFrom(unpacker); | |
} | |
tcs.SetResult(result); | |
} | |
catch (Exception e) | |
{ | |
if (formatterLogger == null) throw; | |
formatterLogger.LogError(String.Empty, e.Message); | |
tcs.SetResult(GetDefaultValueForType(type)); | |
} | |
return tcs.Task; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//READ SINGLE | |
var client = new HttpClient(); | |
var request = new HttpRequestMessage(HttpMethod.Get, "http://localhost:49745/api/values/1"); | |
request.Headers.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/x-msgpack")); | |
var result = client.SendAsync(request).Result; | |
var serializer = MessagePackSerializer.Create<WebApi.MessagePack.Models.Url>(); | |
Url data = serializer.Unpack(result.Content.ReadAsStreamAsync().Result); | |
//READ COLLECTION | |
var client = new HttpClient(); | |
var request = new HttpRequestMessage(HttpMethod.Get, "http://localhost:49745/api/values/"); | |
request.Headers.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/x-msgpack")); | |
var result = client.SendAsync(request).Result; | |
var serializer = MessagePackSerializer.Create<List<WebApi.MessagePack.Models.Url>>(); | |
List<Url> data = serializer.Unpack(result.Content.ReadAsStreamAsync().Result); | |
//POST | |
var request = new HttpRequestMessage(HttpMethod.Post, "http://localhost:49745/api/values/"); | |
request.Content = new ObjectContent<Url>( | |
new Url() { | |
Address = "http://www.strathewb.com", | |
Title = "Test", | |
CreatedAt = DateTime.Now, | |
CreatedBy = "Filip", | |
Description = "dummy" }, | |
new MessagePackMediaTypeFormatter()); | |
request.Content.Headers.ContentType.MediaType = "application/x-msgpack"; | |
StatusCode result5 = client.SendAsync(request5).Result.StatusCode; //204, POST is void |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment