MessagePack C#设置默认解析器

时间:2019-03-14 18:04:06

标签: c# msgpack

我正在使用REST API设置DotNet Core 2应用程序。我想使用MessagePack进行消息传输,但是我无法确定设置默认解析程序的正确方法。

类声明,构造函数和API端点

[ApiVersion(Versions.V1)]
[Route("v{version:apiVersion}/")]
[Produces(MimeTypes.Json, MimeTypes.MessagePack)]
[ProducesResponseType((int) HttpStatusCode.InternalServerError)]
[ApiController]
public class FileProcessingController : ControllerBase
{

    public FileProcessingController()
    {
        // some code here
        MessagePackSerializer.SetDefaultResolver(
           MessagePack.Resolvers.ContractlessStandardResolver.Instance);
    }

  [HttpPost("[action]")]
  [ProducesResponseType(typeof(someObject), (int) HttpStatusCode.OK)]
  [ProducesResponseType((int) HttpStatusCode.BadRequest)]
  public async Task<IActionResult> ProcessIncomingFile([FromBody] RequestObject request)
  {
     // business code here
  }
}

在我的startup.cs文件的ConfigurationServices()中,

  services
    .AddMvc()
    .AddMessagePackFormatters()
    .SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

当我呼叫端点时,收到以下错误。

An unhandled exception has occurred while executing the request.
System.InvalidOperationException: code is invalid. code:217 format:str 8

在我的调用程序中,我可以使用以下代码进行序列化和反序列化。

var packed = MessagePack.MessagePackSerializer.Serialize(request.QueueRequest,
    MessagePack.Resolvers.ContractlessStandardResolver.Instance);
var json = MessagePackSerializer.ToJson(packed);
var unpacked =
    MessagePackSerializer.Deserialize<QueueRequest>(packed,
      MessagePack.Resolvers.ContractlessStandardResolver.Instance);

似乎setDefaultResolver无法正常工作。

0 个答案:

没有答案