Declaring Thrift types - SolalPirelli/ThriftSharp GitHub Wiki
[ThriftStruct( "Person" )]
public sealed class Person
{
[ThriftField( 1, true, "name" )]
public string Name { get; set; }
[ThriftField( 2, false, "age", DefaultValue = 18 )]
public int Age { get; set; }
}
This is equivalent to the following Thrift IDL:
struct Person {
1: required string name;
2: optional i32 age = 18;
}
Properties not marked with the ThriftField
attribute are ignored.
[ThriftEnum]
public enum Gender
{
Male = 1,
Female = 2
}
This is equivalent to the following Thrift IDL:
enum Gender {
male = 1,
female = 2
}
[ThriftService( "DirectoryService" )]
public interface IDirectoryService
{
[ThriftMethod( "search" )]
Task<Person[]> SearchAsync( [ThriftParameter( 1, "name" )] string name );
}
This is equivalent to the following Thrift IDL:
service DirectoryService {
list<Person> search( 1: string name );
}
As you may have noticed, the second method's return type is wrapped in a Task<T>
; it's an asynchronous call. Synchronous calls are not supported.
Exceptions are declared in the same way as structs:
[ThriftStruct( "NoPictureFound" )]
public sealed class NoPictureFoundException : Exception
{
[ThriftField( 1, true, "error" )]
public string Error { get; set; }
}
and used by declaring them on a service method :
[ThriftService( "PictureService" )]
public interface IPictureService
{
[ThriftMethod( "getPictureUrl" )]
[ThriftThrows( 1, "npf", typeof( NoPictureFoundException ) )]
Task<string> GetPictureUrlAsync( [ThriftParameter( 1, "p" )] Person person );
}
The above two declarations are equivalent to the following Thrift IDL:
exception NoPictureFound {
1: required string error;
}
service PictureService {
string getPictureUrl( 1: Person p ) throws (1: NoPictureFound npf);
}