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:
- Install the
protobuf-netpackage:
dotnet add protobuf-net- Add the
ProtobufContentProcessorcontent processor:
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; }}- Apply the
ProtobufContentProcessorcontent processor:
- Per-request configuration:
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:
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.