Integrating Nancy with protobuf-net

Yan Cui

I help clients go faster for less using serverless technologies.

This article is brought to you by

The real-time data platform that empowers developers to build innovative products faster and more reliably than ever before.

Learn more

Over the last couple of days I’ve been playing around with Nancy, one of a number of micro web frameworks now available for the .Net platform. You can read more about Nancy on their github readme page but in short it’s an ultra lightweight, easily extensible framework that lets you run a HTTP web server in a number of hosting environments (IIS, WCF, or inside a simple console app) and can integrate with a number of view engines (Razor, Spark, NDjango, etc).

Protobuf-net on the other hand, is Marc Gravell‘s .Net implementation of Google’s Protocol Buffer messaging format, which as I have shown, is over 10x faster than the BCL’s BinaryFormatter!

Getting Started

If you’re not familiar with how Nancy works, I’d recommend taking half an hour or so and go through the documentations first.

Deserializer

On the deserialization side, Nancy supports JSON and XML binding out of the box, to add protocol buffer support to the equation all you need to do is to create a class that implements the IBodyDeserializer interface and the Nancy bootstrapper will take care of the rest (it auto registers all the implementations of the relevant interfaces at startup).

However, if there are more than one implementation of IBodyDeserialize defined in the loaded assembly which can deserialize the content type application/x-protobuf then at runtime the deserializer will be chosen at random.

Serialization

To serialize the response body with protobuf-net, all you need to do in the handler is to return a Response object and serialize the object you want to return like this:

Alternatively, you can create a subtype of Response and take in the object you want to use as the response body and automatically serialize it, something along the line of:

Source Code

You can download the source code for the demo app here, there’s also a simple unit test using Nancy.Testing helper framework.

Whenever you’re ready, here are 4 ways I can help you:

  1. Production-Ready Serverless: Join 20+ AWS Heroes & Community Builders and 1000+ other students in levelling up your serverless game. This is your one-stop shop for quickly levelling up your serverless skills.
  2. Do you want to know how to test serverless architectures with a fast dev & test loop? Check out my latest course, Testing Serverless Architectures and learn the smart way to test serverless.
  3. I help clients launch product ideas, improve their development processes and upskill their teams. If you’d like to work together, then let’s get in touch.
  4. Join my community on Discord, ask questions, and join the discussion on all things AWS and Serverless.

1 thought on “Integrating Nancy with protobuf-net”

  1. Another option for the serialization side is to create an “AsProtoBuf()” extension method, which follows the convention of the other response formatters in Nancy (e.g. AsJson, AsXml, etc). Take a peek at the FormatterExtensions class and JsonResponse class for examples of this.

Leave a Comment

Your email address will not be published. Required fields are marked *