6.5Adding MessagePack Support

Updated on Aug 20, 2026~4 min read

MessagePack is a compact, efficient binary serialization format designed for data exchange across multiple languages. Compared with JSON, MessagePack offers higher performance and a smaller data footprint. Although it is a binary format, MessagePack was designed with cross-language convenience in mind, and it is now widely used in many programming languages such as Python, Ruby, JavaScript, C++, and C#.

To enable MessagePack support in your project, follow these steps:

  1. Install the MessagePack package:
bash
dotnet add package MessagePack
  1. Add the MessagePackContentProcessor content processor:
cs
public class MessagePackContentProcessor : HttpContentProcessorBase{    /// <inheritdoc />    public override bool CanProcess(HttpContentProcessorContext context) =>        context.ContentType == "application/msgpack";    /// <inheritdoc />    public override HttpContent? Process(HttpContentProcessorContext context)    {        // Attempt to resolve the HttpContent type        if (TryProcess(context, out var httpContent))        {            return httpContent;        }        // Convert the raw request content to a byte array        var content = context.RawContent as byte[] ?? MessagePackSerializer.Serialize(context.RawContent);        // Initialize a ByteArrayContent instance        var byteArrayContent = new ByteArrayContent(content);        byteArrayContent.Headers.ContentType = new MediaTypeHeaderValue(context.ContentType)        {            CharSet = context.Encoding?.WebName        };        return byteArrayContent;    }}
  1. Apply the MessagePackContentProcessor content processor:
  • Per-request configuration:
cs
HttpRequestBuilder.Post("https://furion.net/")    .AddHttpContentProcessors(() => [ new MessagePackContentProcessor() ])    .SetContent(new MessagePackModel { Id = 1, Name = "Furion" }, "application/msgpack");
  • Global configuration:

In the Startup.cs or Program.cs file, configure and register the HttpRemote service to enable the MessagePackContentProcessor content processor:

cs
services.AddHttpRemote(builder =>{    builder.AddHttpContentProcessors(() => [ new MessagePackContentProcessor() ]);});

This way you can send data in the application/msgpack format via HTTP remote requests in your project.