-
Notifications
You must be signed in to change notification settings - Fork 4
Usage example
Solal Pirelli edited this page Sep 24, 2016
·
4 revisions
This page contains a full example of Thrift# usage.
Let's say you're working with the following Thrift definition file:
struct FeedItem {
1: required i32 id;
2: required string title;
3: required i64 date; // Java timestamp: milliseconds from UNIX epoch
4: optional string content;
}
struct Feed {
1: required string name;
2: required list<FeedItem> items;
}
service NewsService {
list<Feed> getFeeds(1: string language);
}
There are three objects that have to be translated into C#: the FeedItem
and Feed
structs, and the NewsService
service. The definition for the structs is as follows:
[ThriftStruct( "FeedItem" )]
public sealed class FeedItem
{
[ThriftField( 1, true, "id" )]
public int Id { get; set; }
[ThriftField( 2, true, "title" )]
public string Title { get; set; }
[ThriftField( 3, true, "date", Converter = typeof( ThriftJavaDateConverter ) )]
public DateTime Date { get; set; }
[ThriftField( 4, false, "title" )]
public string Content { get; set; }
}
[ThriftStruct( "Feed" )]
public sealed class Feed
{
[ThriftField( 1, true, "name" )]
public string Name { get; set; }
[ThriftField( 2, true, "items" )]
public FeedItem[] Items { get; set; }
}
The service declaration is just as simple:
[ThriftService( "NewsService" )]
public interface INewsService
{
[ThriftMethod( "getFeeds" )]
Task<Feed[]> GetFeedsAsync( [ThriftParameter( 1, "language" )] string language );
}
The client code simply creates a ThriftCommunication
object and a proxy instance for the service:
// somewhere in an async method
var comm = ThriftCommunication.Binary()
.OverHttp( "http://example.org:1234/news" );
var service = ThriftProxy.Create<INewsService>( comm );
var feeds = await service.GetFeedsAsync( "en" );