0

I am currently trying to implement MessagePack in a solution which contains 2 projects : AspNet Core WebAPI and a simple console app. The following package was added :

How fo I Deserialize the object back on the client, here is the code snippets, also when Posting back an object from the client to the api, do I just Serialize on the client and send it to the Post method in the api, which will take a string, take the string and Deserialize it again, I will need to pass the type somehow to the controller also.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using MessagePack;
using Microsoft.AspNetCore.Mvc;

namespace WebApplication1.Controllers
{
   [Route("api/[controller]")]
   public class ValuesController : Controller
   {
     [HttpGet]
     public IActionResult Get()
     {
        var results = new List<Superhero>();

        results.Add(new Superhero { HeroID = 1, HeroName = "Bruce Wayne" });
        results.Add(new Superhero { HeroID = 2, HeroName = "Selina Kyle" });
        results.Add(new Superhero { HeroID = 3, HeroName = "Clark Kent" });

        var bytes = MessagePackSerializer.Serialize(results);

        return Ok(bytes);
    }

    [HttpGet("{id}")]
    public string Get(int id)
    {
        return "value";
    }

    [HttpPost]
    public void Post([FromBody]string value)
    {
        // how to I Deserialize here ? what do I just post from client to
        // with the Serialized object and pass the type also ???
    }

    [HttpPut("{id}")]
    public void Put(int id, [FromBody]string value)
    {
    }

    [HttpDelete("{id}")]
    public void Delete(int id)
    {
    }
   }

  [MessagePackObject]
  public class Superhero
  {
    [Key(0)]
    public int HeroID { get; set; }

    [Key(1)]
    public string HeroName { get; set; }
  }
}

          using MessagePack;
      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Net.Http;
      using System.Text;
      using System.Threading.Tasks;

      namespace MessagePackExampleOne
      {
          class Program
          {
              static HttpClient client = new HttpClient();

              static void Main(string[] args)
              {
                  client.BaseAddress = new Uri("http://localhost:57752");
                  client.DefaultRequestHeaders.Accept.Clear();

                  HttpResponseMessage response = client.GetAsync("/api/values").Result;
                  if (response.IsSuccessStatusCode)
                  {
                      var result = response.Content.ReadAsStringAsync().Result;

                      //how to Deserialize this objec ??

                      // Console.WriteLine(MessagePackSerializer.ToJson(result));
                      // var mc2 = MessagePackSerializer.Deserialize<List<Superhero>>(result);
                  }

                  Console.Read();
              }


          }

          [MessagePackObject]
          public class Superhero
          {
              [Key(0)]
              public int HeroID { get; set; }

              [Key(1)]
              public string HeroName { get; set; }
          }
      }
Optimus Prime
  • 43
  • 1
  • 11
  • 2
    Don't post code in the form of images. Read- https://stackoverflow.com/help/how-to-ask and provide a [MCVE] – Souvik Ghosh Apr 26 '18 at 06:59
  • You really need to post your code. What would we do with screenshots, starring at them? And code can be copy-pasted. – Evk Apr 26 '18 at 07:24
  • Did you even bother to read the documentation? https://msgpack.org/index.html when you scroll down in the languages section and select "C# /msgpack" you get an example on how to serialize and deserialize it. Also seen on https://github.com/msgpack/msgpack-cli which is linked from the main page – Tseng Apr 26 '18 at 07:34
  • apologies, I will edit the original post – Optimus Prime Apr 26 '18 at 07:34
  • 2
    In the `public void Post([FromBody] string value)` there is a way to bind to an object to use something like this `public void Post([FromBody] MyClass model)`? – Oscar Canek Mar 20 '19 at 14:54

2 Answers2

0

to send something in post method from client use TryAddWithoutValidation:

var x = MessagePackSerializer.Serialize(MyCLassObj to send);
var content = new ByteArrayContent(x);
content.Headers.TryAddWithoutValidation("Content-Type", "application/x-msgpack");

httpResponse = await httpClient.PostAsync("/api...", content,token);
stef
  • 45
  • 6
0

This link shows it

In short:

    using (var httpClient = new HttpClient())
{
    httpClient.BaseAddress = new Uri("https://localhost:44362/");
    var result = httpClient.GetAsync("api/values").Result;
    var bytes = result.Content.ReadAsByteArrayAsync().Result;
    var data = MessagePackSerializer.Deserialize<IEnumerable<string>>(bytes);
}
deathrace
  • 908
  • 4
  • 22
  • 48