将 MessagePack 与 AspNet Core WebAPI 和控制台应用程序结合使用

问题描述 投票:0回答:2

我目前正在尝试在包含 2 个项目的解决方案中实现 MessagePack:AspNet Core WebAPI 和一个简单的控制台应用程序。添加了以下包:

如何将对象反序列化回客户端,这里是代码片段,另外,当将对象从客户端回发到 api 时,我是否只在客户端上序列化并将其发送到 api 中的 Post 方法,这将获取一个字符串,获取该字符串并再次反序列化它,我还需要以某种方式将类型传递给控制器。

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; }
          }
      }
c# asp.net-core asp.net-core-webapi msgpack
2个回答
0
投票

要从客户端使用 TryAddWithoutValidation 在 post 方法中发送内容:

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);

0
投票

这个链接显示了它

简而言之:

    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);
}
© www.soinside.com 2019 - 2024. All rights reserved.