Thursday, 16 July 2009

protobuf-net; now with added Orcas

A long running gripe with protobuf-net is that while the code generation (from .proto) works, it isn't easy to integrate into your development process. Well, wait no longer! I'm very happy to announce that protobuf-net now has Visual Studio 2008 support

(important note: this does not include the "Express" editions of Visual Studio).

A big vote of thanks goes to Shaun Cooley, who kindly created me the initial "cut" which I've extended. I did try this before, but I drowned in the complexity that is Visual Studio extensibility.

So what do I get?

Initially, the installer includes a "Custom Tool" (registered against the .proto extension), and an item template (not currently registered by the tooling):

The Custom Tool

This means you can add new text file called "mymessage.proto", and it will automatically start running C# (see below) code-generation on that .proto - writing a .cs file to match your .proto. This includes things like error-processing, so if you have errors in your .proto you can click on them and it will take you to the right place in the file. It will also add the protobuf-net reference if you don't already have it.

proto-vs

For existing files, you can set the Custom Tool manually; the string you need is "ProtoBufTool" (original, huh?).

At the moment, protobuf-net lets you pass a number of options on the command line. This same functionality is available in the IDE too - just (ab)use the "Custom Tool Namespace" - it accepts a semi-colon delimited set of arguments (the first being reserved for the actual namespace to use if your file doesn't specify a package) - i.e. if you use the string ";fixCase", it will apply C#-style case fixups (so "name" becomes "Name", etc). The arguments are exactly the same as on the command line tool (protogen.exe).

And if you think this is a bit ugly; I have "plans" (see below).

The template

The installer also includes (in the loosest sense) a custom item template, so you can use Add -> New Item to get a new .proto file with a suitable skeleton (including things like the project's namespace, etc). It doesn't currently install this template - and I'll be honest; this is simply because I haven't yet had time to crack the installer specifics of this (it is.... fiddly).

To install this manually:

  • copy the BrotoBuf.zip file from (protobuf-net install dir)\ItemTemplates\csharp\data\1033 into (VS install location - Microsoft Visual Studio 9.0)\Common7\IDE\ItemTemplates\CSharp\Data\1033
  • from the VS2008 command prompt, execute "devenv /installvstemplates" (it may take a minute or two)

Enough; where is it?

The installer is available from the project home page.

What next

The support at the moment is limited; I have a number of things I want to do:

  • Add VB support! (the main problem here is detecting the project's language; the code generation itself works)
  • Register the ItemTemplate during installation
  • Move a lot of the command-line switches into custom .proto attributes
  • Fix the annoying UTF8 BOM issue once and for all...
  • etc

I hope you find it useful; working with VS extensibility has certainly been a different set of challenges - I'll blog about some of those separately.

10 comments:

zuken21 said...

The ProtoBuf.zip template can be copy to

[User Directory]\Documents\Visual Studio 2008\Templates\ItemTemplates\Visual C#\

It will appear in "My Templates" category

Duc

Marc Gravell said...

Cheers; that would indeed be simpler - not quite as satisfying, of course ;-p

Anonymous said...

I've downloaded http://protobuf-net.googlecode.com/files/protobuf-net%20r282.zip can I use this and still get the pretty VS integration?

Christian said...

Marc,
When running custom tool in VS2008 I get the error,

The custom tool 'ProtoBufTool' failed.

Marc Gravell said...

@Christian - config? x86/x64 etc? Also - does the file contain a valid .proto *with the correct encoding* (you might want to check the error-list window, which gives details on how to fix the encoding)

mikezat said...

Hi! I am trying to upgrade our custom service hosted WCF application to protobuf. Do I need to add reference to protobuf in all client/service projects in addition to adding new protobuf behavior and extension to config files?

Marc Gravell said...

yes, both client and server need to know. Note that you might also need to double check that the "Order" property on DataMember matches client and server - there is an attribute you can add in a partial class to offset them all - sometimes it is 1-off, if you see what I mean.

Anonymous said...

Any chance a VS2012 version is on the way?

Marc Gravell said...

@Anonymous that sounds like a good idea... Adding to my list

Michael Merrill said...

Has this tool been implemented for VS2012 yet?