如何在 asp.net 核心 3.1 web api 中配置和使用 MessagePack?

How can one configure and use MessagePack in an asp.net core 3.1 web api?

我已按照 this article 开始在我的 asp.net 核心 3.1 应用程序中使用 MessagePack,但由于以下错误消息,它无法正常工作:

Severity Code Description Project File Line Suppression State Error CS1503 Argument 1: cannot convert from 'MessagePack.Resolvers.ContractlessStandardResolver' to 'MessagePack.MessagePackSerializerOptions' Gateway C:\Developments\POC\Gateway\Startup.cs 33 Active

解决此编译问题的另一种替代方法或解决方案是什么?

正如@Michael所说,MessagePackOutputFormatter后面需要的是MessagePackSerializerOptions类型,而出现当前错误是因为类型是ContractlessStandardResolver,不一致.

因此,您只需要修改代码如下:

 services.AddMvc().AddMvcOptions(option =>
            {
                option.OutputFormatters.Clear();
                option.OutputFormatters.Add(new MessagePackOutputFormatter(ContractlessStandardResolver.Options));
                option.InputFormatters.Clear();
                option.InputFormatters.Add(new MessagePackInputFormatter(ContractlessStandardResolver.Options));
            })
   .SetCompatibilityVersion(CompatibilityVersion.Version_2_2);