Thrift MSBuild Task

Thrift has an interface definition language (IDL), which is used to define a thrift service and structures. This interface is saved in a .thrift file and ran through the Thrift compiler to generate C#, Java, Perl or a code for dozen other languages.

A quick googling shows there is a MSBuild task to integrate the Thrift Compiler into a MSBuild, but I couldn’t find an example of how to use it.

Using the Thrift MSbuild Task

Below are the fairly straight-forward steps to use the Thrift MSBuild Task.

1. Create a Visual Studio Project,  .Thrift file and import the Thrift library using nuGet.

2. Compile ThriftBuild into a DLL

Create a Class Library project.

Add references to:

  • Microsoft.Build.Framework
  • Microsoft.Build.Tasks.v4.0
  • Microsoft.Build.Utilitities.v4.0

Compile! This will output a .dll. In my eample it is named ThriftMSBuildTask.dll.

3. Add the Thrift MSBuild task to a project.

1. Unload and edit the project.

2. Make the following change to the unloaded project file.

<UsingTask AssemblyFile=".\Thrift\ThriftMSBuildTask.dll" TaskName="ThriftMSBuildTask.ThriftBuild" />
  <Target Name="BeforeBuild">
    <ThriftBuild OutputName=".\ThriftLib.dll" ThriftDefinitionDir=".\Thrift\" ThriftExecutable=".\Thrift\thrift-0.8.0.exe" ThriftLibrary="..\packages\Thrift.\lib\net35\Thrift.dll" />

3. Compile the project.

4. Add a reference to the ThriftLib.dll. It will be located in the same folder as Thrift.dll.

5. Modify the .thrift IDL and compile away! The IDL generates the C# code before compiling the project. This will provide immediate feedback if the IDL has made a change which no longer compiles!

Final Thoughts

It takes very little time to hookup the thrift compiler to MSBuild. It is definitely worth doing if you are making regular changes to the Thrift IDL.

The ThriftBuild.cs code is not the greatest C# code and could use some love. There also needs to be a better location for distributing this MSBuild task. I might look at creating a nuGet package for it.

My sample code including the MSbuildTask.dll can be found here:

This entry was posted in Thrift and tagged , , . Bookmark the permalink.

1 Response to Thrift MSBuild Task

  1. David V says:

    Thanks, this is very useful! Note for users, you will need to add the Thrift transport, protocol and client connection boilerplate code to the sample program. Also, the first time through, until you have the ThriftLib.dll generated, you should comment out the client code usage in your main to avoid the unresolved reference problem. Once the client is built the first time this issue goes away.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s