Skip to content
This repository has been archived by the owner on May 15, 2022. It is now read-only.

mocsharp/ProtobufFormatter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ProtobufFormatter

A Protobuf WebAPI formatter

See Protocol Buffer Basics: C# to get started.

#Server

HttpConfiguration config = new HttpConfiguration();
config.Formatters.Insert(0, new ProtoBufFormatter());
...
app.UseWebApi(config);

#Client ##Get

HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Accept.Clear(); //optional
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-protobuf"));
var req = await client.GetAsync("http://localhost:12345/api/home");
req.EnsureSuccessStatusCode();
var person = await req.Content.ReadAsAsync<Person>(GetFormatter());

//----------------------------------------

private static List<MediaTypeFormatter> GetFormatter()
{
  var formatters = new List<MediaTypeFormatter>();
  formatters.Add(new HolxProtoBufFormatter());
  return formatters;
}

##Post

Person newPerson = new Person()
{
  Email = "[email protected]",
  Id = 50,
  Name = "Joe"
};

using (var ms = new MemoryStream())
{
  using(var gs = new CodedOutputStream(ms))
  {
    newPerson.WriteTo(gs);
  }
  var content = new ByteArrayContent(ms.ToArray());
  content.Headers.ContentType = new MediaTypeHeaderValue("application/x-protobuf");
  var res = await client.PostAsync("http://localhost:12345/api/home/some-post-api", content);
  res.EnsureSuccessStatusCode();
}

About

A Protobuf WebAPI formatter

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages