David Reiss | 06a9312 | 2009-03-30 21:22:52 +0000 | [diff] [blame^] | 1 | using System; |
| 2 | using Thrift; |
| 3 | using Thrift.Protocol; |
| 4 | using Thrift.Server; |
| 5 | using Thrift.Transport; |
| 6 | |
| 7 | |
| 8 | namespace CSharpTutorial |
| 9 | { |
| 10 | public class CSharpClient |
| 11 | { |
| 12 | public static void Main() |
| 13 | { |
| 14 | try |
| 15 | { |
| 16 | TTransport transport = new TSocket("localhost", 9090); |
| 17 | TProtocol protocol = new TBinaryProtocol(transport); |
| 18 | Calculator.Client client = new Calculator.Client(protocol); |
| 19 | |
| 20 | transport.Open(); |
| 21 | |
| 22 | client.ping(); |
| 23 | Console.WriteLine("ping()"); |
| 24 | |
| 25 | int sum = client.add(1, 1); |
| 26 | Console.WriteLine("1+1={0}", sum); |
| 27 | |
| 28 | Work work = new Work(); |
| 29 | |
| 30 | work.op = Operation.DIVIDE; |
| 31 | work.num1 = 1; |
| 32 | work.num2 = 0; |
| 33 | try |
| 34 | { |
| 35 | int quotient = client.calculate(1, work); |
| 36 | Console.WriteLine("Whoa we can divide by 0"); |
| 37 | } |
| 38 | catch (InvalidOperation io) |
| 39 | { |
| 40 | Console.WriteLine("Invalid operation: " + io.why); |
| 41 | } |
| 42 | |
| 43 | work.op = Operation.SUBTRACT; |
| 44 | work.num1 = 15; |
| 45 | work.num2 = 10; |
| 46 | try |
| 47 | { |
| 48 | int diff = client.calculate(1, work); |
| 49 | Console.WriteLine("15-10={0}", diff); |
| 50 | } |
| 51 | catch (InvalidOperation io) |
| 52 | { |
| 53 | Console.WriteLine("Invalid operation: " + io.why); |
| 54 | } |
| 55 | |
| 56 | SharedStruct log = client.getStruct(1); |
| 57 | Console.WriteLine("Check log: {0}", log.value); |
| 58 | |
| 59 | transport.Close(); |
| 60 | } |
| 61 | catch (TApplicationException x) |
| 62 | { |
| 63 | Console.WriteLine(x.StackTrace); |
| 64 | } |
| 65 | |
| 66 | } |
| 67 | } |
| 68 | } |