Web 的 MessagePack 格式化程序 API(不是 ASP.NET 核心)
MessagePack formatter for Web API (not ASP.NET Core)
我们正在尝试在我们的 Web API 上实施 MessagePack。但是,我无法找到可靠的最新格式化程序。我找到了 WebApiContrib.Formatting.MsgPack 格式化程序,但最后一次更新是在 2014 年,当我尝试使用它时它会抛出 "Method not found" 异常。
我也查看了一个 example here 但代码包含一些方法引用,这些方法引用不再出现在 nuget 包中。
似乎有一个更新的 ASP.NET 核心库和一组格式化程序,但我们不打算很快转移到核心。
有人有可靠的 MessagePack 格式化程序的提示吗?
如果您拥有所有 serialization/deserialization 代码,那么实现自定义媒体类型格式化程序是一项非常容易的任务。因此,如果找不到合适的解决方案,您可以自己实施。这是工作示例:
public class MessagePackFormatter : MediaTypeFormatter
{
public MessagePackFormatter()
{
SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/x-msgpack"));
}
public override bool CanReadType(Type type)
{
return true;
}
public override bool CanWriteType(Type type)
{
return true;
}
public override Task WriteToStreamAsync(Type type, object value, Stream writeStream, HttpContent content, TransportContext transportContext)
{
if (type == null)
{
throw new ArgumentNullException(nameof(type));
}
if (writeStream == null)
{
throw new ArgumentNullException(nameof(writeStream));
}
MessagePackSerializer.NonGeneric.Serialize(type, writeStream, value, ContractlessStandardResolver.Instance);
return Task.FromResult(0);
}
public override Task<object> ReadFromStreamAsync(Type type, Stream readStream, HttpContent content, IFormatterLogger formatterLogger)
{
if (type == null)
{
throw new ArgumentNullException(nameof(type));
}
if (readStream == null)
{
throw new ArgumentNullException(nameof(readStream));
}
var value = MessagePackSerializer.NonGeneric.Deserialize(type, readStream, ContractlessStandardResolver.Instance);
return Task.FromResult(value);
}
}
如您所见,它非常薄,只需为 serialization/deserialization 调用 MessagePackSerializer.NonGeneric
class。我建议开始使用这种简单的格式化程序,如果您在特定情况下遇到一些问题,只需调整代码即可解决问题。您还可以检查 existing implementation for .Net Core 以处理一些特殊情况。
我们正在尝试在我们的 Web API 上实施 MessagePack。但是,我无法找到可靠的最新格式化程序。我找到了 WebApiContrib.Formatting.MsgPack 格式化程序,但最后一次更新是在 2014 年,当我尝试使用它时它会抛出 "Method not found" 异常。
我也查看了一个 example here 但代码包含一些方法引用,这些方法引用不再出现在 nuget 包中。
似乎有一个更新的 ASP.NET 核心库和一组格式化程序,但我们不打算很快转移到核心。
有人有可靠的 MessagePack 格式化程序的提示吗?
如果您拥有所有 serialization/deserialization 代码,那么实现自定义媒体类型格式化程序是一项非常容易的任务。因此,如果找不到合适的解决方案,您可以自己实施。这是工作示例:
public class MessagePackFormatter : MediaTypeFormatter
{
public MessagePackFormatter()
{
SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/x-msgpack"));
}
public override bool CanReadType(Type type)
{
return true;
}
public override bool CanWriteType(Type type)
{
return true;
}
public override Task WriteToStreamAsync(Type type, object value, Stream writeStream, HttpContent content, TransportContext transportContext)
{
if (type == null)
{
throw new ArgumentNullException(nameof(type));
}
if (writeStream == null)
{
throw new ArgumentNullException(nameof(writeStream));
}
MessagePackSerializer.NonGeneric.Serialize(type, writeStream, value, ContractlessStandardResolver.Instance);
return Task.FromResult(0);
}
public override Task<object> ReadFromStreamAsync(Type type, Stream readStream, HttpContent content, IFormatterLogger formatterLogger)
{
if (type == null)
{
throw new ArgumentNullException(nameof(type));
}
if (readStream == null)
{
throw new ArgumentNullException(nameof(readStream));
}
var value = MessagePackSerializer.NonGeneric.Deserialize(type, readStream, ContractlessStandardResolver.Instance);
return Task.FromResult(value);
}
}
如您所见,它非常薄,只需为 serialization/deserialization 调用 MessagePackSerializer.NonGeneric
class。我建议开始使用这种简单的格式化程序,如果您在特定情况下遇到一些问题,只需调整代码即可解决问题。您还可以检查 existing implementation for .Net Core 以处理一些特殊情况。