6.6Adding Protobuf Support

Created on Aug 17, 2026~3 min read

Protobuf (Protocol Buffers) is a language-neutral, platform-neutral, extensible serialization format for structured data developed by Google, used for communication protocols, data storage, and more.

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

  1. Install the protobuf-net package:
bash
dotnet add protobuf-net
  1. Add the ProtobufContentProcessor content processor:
cs
public class ProtobufContentProcessor : HttpContentProcessorBase{    /// <inheritdoc />    public override bool CanProcess(HttpContentProcessorContext context) =>        context.ContentType == "application/x-protobuf";    /// <inheritdoc />    public override HttpContent? Process(HttpContentProcessorContext context)    {        // Attempt to resolve the HttpContent type        if (TryProcess(context, out var httpContent))        {            return httpContent;        }        byte[] content;        if (context.RawContent is byte[] bytes)        {            content = bytes;        }        else        {            // Convert the raw request content to a byte array            using var ms = new MemoryStream();            Serializer.Serialize(ms, context.RawContent);            content = ms.ToArray();        }        // 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 ProtobufContentProcessor content processor:
  • Per-request configuration:
cs
HttpRequestBuilder.Post("https://furion.net/")    .AddHttpContentProcessors(() => [ new ProtobufContentProcessor() ])    .SetContent(new MyProtobufMessage { Id = 1, Name = "Furion" }, "application/x-protobuf");
  • Global configuration:

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

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

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