Mark Needham

Thoughts on Software Development

C# Thrift Examples

with 8 comments

As I mentioned in my earlier post I have been working with Facebook’s Thrift messaging project.

Unfortunately there are not currently any C# examples of how to use the Data Transfer Objects the Thrift compiler generates for us on the official wiki.

We managed to figure out how to do it by following the Java instructions and converting them into C# code. Before writing any code we need to import Thrift.dll into our Visual Studio project.

Assuming that we have the following Thrift definition file:

namespace csharp Test.Thrift
 
struct FooBarMessageThrift {
1: string Foo
2: string Bar
}

When we run the Thrift compiler we will end up with the FooBarMessageThrift class. I won’t post this class here as it’s all codegen.

The easiest way to transport this class around is by converting it to a byte array and transporting that:

1
2
3
4
5
6
7
8
var fooBarMessage = new FooBarMessageThrift {Foo = "foo", Bar = "bar"};
var stream = new MemoryStream();
 
TProtocol tProtocol = new TBinaryProtocol(new TStreamTransport(stream, stream));
 
fooBarMessage.Write(tProtocol);
 
byte[] content = stream.ToArray();

To read the byte array back into FooBarMessageThrift we do this:

1
2
3
4
5
var stream = new MemoryStream(content);
TProtocol tProtocol = new TBinaryProtocol(new TStreamTransport(stream, stream));
 
var barFooMessageThrift = new BarFooMessageThrift();
barFooMessageThrift.Read(tProtocol);

‘content’ in this example is the byte[] created in the first example, and that’s all there is to it!

Written by Mark Needham

August 29th, 2008 at 1:39 am

Posted in .NET,Messaging

Tagged with , , ,

  • http://www.pelotonics.com Daniel Marashlian

    Hi Mark,

    I have a set of thrift files from Evernote’s API that released yesterday. I’m trying to integrate it into our application, though we’re in .NET (C# and ASP.NET). I’ve been trying to read up on how to take a set of thrift files and “compile” them for .NET and Visual Studio. I’m assuming it would create a .dll file that I could just reference.

    If you have any ideas how to do this, or a good reference, I would really appreciate it.

    Daniel Marashlian

  • http://www.markhneedham.com Mark Needham

    Hi,

    I haven’t played around with anything from the Evernote API but the way I understand it what you would need to do is run the Thrift compiler (probably http://wiki.apache.org/thrift/ThriftInstallationWin32 if you’re using C#) against the .thrift files.

    We had the following directory structure setup:

    ..
    /Thrift
    –/Messages
    —-/Message1.thrift
    —-/Message2.thrift
    –generate.bat

    Inside generate.bat was this:

    @echo off
    for %%i in (Messages\*.thrift) do thrift –gen csharp -I . %%i
    xcopy /E /Y gen-csharp\* ..

    That last line copied the C# files which Thrift generated into the appropriate place in the code base.

    I guess you could then put those files together into a dll – they are just generated C# files.

    Hope that helps,
    Mark

  • http://michaelgreene.org Michael Greene

    There is an MSBuild task included in Thrift that you can use to automate the generate.bat process and integrate it into your existing build environment (e.g. Visual Studio)

  • Hannes Baldursson

    @Daniel
    Like Mark said, but note that you also need to reference the thrift codebase.

    I’ve written a wiki page with a step-by-step process. Should be easy to follow. I’ll try to update the wiki as I learn and encourage everyone and anyone who knows anything about thrift to do the same. It’s got a poor documentation to say the least.

    http://wiki.apache.org/thrift/ThriftUsageC%23

  • rideon88

    I’m using hbase v.20 with thrift trying to make Client.getRows(myTableName) to work. The getRows() method is supposed to return a List of TRowResult but it’s only returning the first row in the table. Have you successfully used Thrift to return all of the desired table rows?

  • http://www.gettingcirrius.com Richard

    Mark,

    Thank you for the code snippets on .NET serialization/deserialization using Thrift. The Java mechanism for performing these tasks was quite obvious, but I was struggling to find the .NET way.

    Richard

  • Jeffery Xu

    good 

  • cloud

    i download Thrift from his website ,but i have not extends method write() and read()? is need extra lib ? can you share?