Skip to content
Demis Bellot edited this page Oct 25, 2016 · 19 revisions

Protocol Buffers is a high-performance, compact binary wire format invented by Google who use it internally so they can communicate with their internal network services at very high speed.

For .NET @marcgravell has developed protobuf-net - a robust implementation of the Protocol Buffers wire format that provides the fastest serialization option available for .NET.

ProtoBuf is a great addition to your ServiceStack's web services as it provides the fastest binary serializer to go along with the 2 fastest text serializers for .NET in JSON and JSV formats (already included by default).

Note: Until we can get marcgravell to succumb to internal pressure and add support for attribute-less POCOs you will need to add unique custom indexes for each property on your DTOs e.g: [DataMember(Order=N)].

Otherwise another fast binary serializer that supports attribute-less POCOs is the new MessagePack Format.

Installing via NuGet

As it requires an external protobuf-net.dll dependency ProtoBuf support is not automatically bundled inside ServiceStack, but it is easily installed with the ServiceStack.ProtoBuf NuGet package:

Install-Package ServiceStack.ProtoBuf

After the NuGet Package is added to your Project, enable the ProtoBuf format with:

Plugins.Add(new ProtoBufFormat());

The NuGet plugin also includes the ProtoBufServiceClient client below so you can easily call it from any C# Client.

Registering ProtoBuf Manually

The API for adding custom Formats and Content Types in ServiceStack is so easy we use it ourselves :) Where the CSV, HTML, Markdown and now ProtoBuf format are all registered in the same way by registering the new ContentType with your AppHost's ContentTypeFilters.

Adding support for ProtoBuf is equally simple. It can be added by calling a single method:

appHost.ContentTypeFilters.Register(ContentType.ProtoBuf,
	(reqCtx, res, stream) => ProtoBuf.Serializer.NonGeneric.Serialize(stream, res),
	ProtoBuf.Serializer.NonGeneric.Deserialize);

This makes the ProtoBuf format available in all of ServiceStack:

  • A new X-PROTOBUF column added for all services on the metadata pages
  • New /x-protobuf/syncreply/{Service} and /x-protobuf/asynconeway/{Service} pre-defined routes
  • Clients can request it with Accept: application/x-protobuf HTTP Header or ?format=x-protobuf query string

End to End happiness

However simply registering ProtoBuf is not enough to ensure end-to-end happiness so we also make it easy to create your own generic strong-typed ProtoBuf ServiceClient with the following code:

public class ProtoBufServiceClient : ServiceClientBase
{
    public override string Format
    {
        get { return "x-protobuf"; }
    }

    public ProtoBufServiceClient(string baseUri)
    {
        SetBaseUri(baseUri);
    }

    public ProtoBufServiceClient(string syncReplyBaseUri, string asyncOneWayBaseUri)
        : base(syncReplyBaseUri, asyncOneWayBaseUri) { }

    public override void SerializeToStream(IRequest req, object request, Stream stream)
    {
        Serializer.NonGeneric.Serialize(stream, request);
    }

    public override T DeserializeFromStream<T>(Stream stream)
    {
        return Serializer.Deserialize<T>(stream);
    }

    public override string ContentType
    {
        get { return MimeTypes.ProtoBuf; }
    }

    public override StreamDeserializerDelegate StreamDeserializer
    {
        get { return Deserialize; }
    }

    private static object Deserialize(Type type, Stream source)
    {
        return Serializer.NonGeneric.Deserialize(type, source);
    }
}

This now lets you call each of your services with a Strong Typed service client of your very own:

var client = new ProtoBufServiceClient(BaseUri);
var response = client.Send<HelloResponse>(new Hello { Name = "ProtoBuf" });

The above ProtoBufServiceClient works like all the other strong-typed ServiceClients in ServiceStack where it also implements IServiceClient and IRestClient interfaces so you can easily swap out your existing clients to take advantage of the performance boost offered by ProtoBuf with minimal effort!

Community Resources



  1. Getting Started

    1. Creating your first project
    2. Create Service from scratch
    3. Your first webservice explained
    4. Example Projects Overview
    5. Learning Resources
  2. Designing APIs

    1. ServiceStack API Design
    2. Designing a REST-ful service with ServiceStack
    3. Simple Customer REST Example
    4. How to design a Message-Based API
    5. Software complexity and role of DTOs
  3. Reference

    1. Order of Operations
    2. The IoC container
    3. Configuration and AppSettings
    4. Metadata page
    5. Rest, SOAP & default endpoints
    6. SOAP support
    7. Routing
    8. Service return types
    9. Customize HTTP Responses
    10. Customize JSON Responses
    11. Plugins
    12. Validation
    13. Error Handling
    14. Security
    15. Debugging
    16. JavaScript Client Library (ss-utils.js)
  4. Clients

    1. Overview
    2. C#/.NET client
      1. .NET Core Clients
    3. Add ServiceStack Reference
      1. C# Add Reference
      2. F# Add Reference
      3. VB.NET Add Reference
      4. Swift Add Reference
      5. Java Add Reference
    4. Silverlight client
    5. JavaScript client
      1. Add TypeScript Reference
    6. Dart Client
    7. MQ Clients
  5. Formats

    1. Overview
    2. JSON/JSV and XML
    3. HTML5 Report Format
    4. CSV Format
    5. MessagePack Format
    6. ProtoBuf Format
  6. View Engines 4. Razor & Markdown Razor

    1. Markdown Razor
  7. Hosts

    1. IIS
    2. Self-hosting
    3. Messaging
    4. Mono
  8. Security

    1. Authentication
    2. Sessions
    3. Restricting Services
    4. Encrypted Messaging
  9. Advanced

    1. Configuration options
    2. Access HTTP specific features in services
    3. Logging
    4. Serialization/deserialization
    5. Request/response filters
    6. Filter attributes
    7. Concurrency Model
    8. Built-in profiling
    9. Form Hijacking Prevention
    10. Auto-Mapping
    11. HTTP Utils
    12. Dump Utils
    13. Virtual File System
    14. Config API
    15. Physical Project Structure
    16. Modularizing Services
    17. MVC Integration
    18. ServiceStack Integration
    19. Embedded Native Desktop Apps
    20. Auto Batched Requests
    21. Versioning
    22. Multitenancy
  10. Caching

  11. Caching Providers

  12. HTTP Caching 1. CacheResponse Attribute 2. Cache Aware Clients

  13. Auto Query

  14. Overview

  15. Why Not OData

  16. AutoQuery RDBMS

  17. AutoQuery Data 1. AutoQuery Memory 2. AutoQuery Service 3. AutoQuery DynamoDB

  18. Server Events

    1. Overview
    2. JavaScript Client
    3. C# Server Events Client
    4. Redis Server Events
  19. Service Gateway

    1. Overview
    2. Service Discovery
  20. Encrypted Messaging

    1. Overview
    2. Encrypted Client
  21. Plugins

    1. Auto Query
    2. Server Sent Events
    3. Swagger API
    4. Postman
    5. Request logger
    6. Sitemaps
    7. Cancellable Requests
    8. CorsFeature
  22. Tests

    1. Testing
    2. HowTo write unit/integration tests
  23. ServiceStackVS

    1. Install ServiceStackVS
    2. Add ServiceStack Reference
    3. TypeScript React Template
    4. React, Redux Chat App
    5. AngularJS App Template
    6. React Desktop Apps
  24. Other Languages

    1. FSharp
      1. Add ServiceStack Reference
    2. VB.NET
      1. Add ServiceStack Reference
    3. Swift
    4. Swift Add Reference
    5. Java
      1. Add ServiceStack Reference
      2. Android Studio & IntelliJ
      3. Eclipse
  25. Amazon Web Services

  26. ServiceStack.Aws

  27. PocoDynamo

  28. AWS Live Demos

  29. Getting Started with AWS

  30. Deployment

    1. Deploy Multiple Sites to single AWS Instance
      1. Simple Deployments to AWS with WebDeploy
    2. Advanced Deployments with OctopusDeploy
  31. Install 3rd Party Products

    1. Redis on Windows
    2. RabbitMQ on Windows
  32. Use Cases

    1. Single Page Apps
    2. HTML, CSS and JS Minifiers
    3. Azure
    4. Connecting to Azure Redis via SSL
    5. Logging
    6. Bundling and Minification
    7. NHibernate
  33. Performance

    1. Real world performance
  34. Other Products

    1. ServiceStack.Redis
    2. ServiceStack.OrmLite
    3. ServiceStack.Text
  35. Future

    1. Roadmap
Clone this wiki locally