diff --git a/examples/CSharp/Example/Program.cs b/examples/CSharp/Example/Program.cs index 34edf5b..138cdbd 100644 --- a/examples/CSharp/Example/Program.cs +++ b/examples/CSharp/Example/Program.cs @@ -4,47 +4,57 @@ using System.Text; using System.Text.Json; using System.Text.Json.Nodes; -class TestSrvimpl : Example.TestServiceServer { - public TestSrvimpl(): base() {} - public override async Task AddValuesSingleParam(AddValueRequest request, int ctx) { +class TestSrvimpl : Example.TestServiceServer +{ + public TestSrvimpl() : base() { } + public override async Task AddValuesSingleParam(AddValueRequest request, int ctx) + { var res = new Example.AddValueResponse(); res.value = 1; return res; } - public override async Task AddValuesMultipleParams(double value1,double value2,int ctx) { + public override async Task AddValuesMultipleParams(double value1, double value2, int ctx) + { return value1 + value2; } - public override async Task ReturningVoid(double param1,int ctx) { - + public override async Task ReturningVoid(double param1, int ctx) + { + } - public override void OnEvent(string param1,int ctx) { + public override void OnEvent(string param1, int ctx) + { Console.WriteLine($"OnEvent {param1}"); } - public override async Task> FunctionWithArrayAsParamAndReturn(List values1,List values2, int ctx) { + public override async Task> FunctionWithArrayAsParamAndReturn(List values1, List values2, int ctx) + { var l = new List(); l.Append(1); return l; } - public override async Task ThrowingError(int ctx) { + public override async Task ThrowingError(int ctx) + { throw new Exception("This is a remote error :)"); } } -class CopyTransportS2 : Example.JRpcTransport { +class CopyTransportS2 : Example.JRpcTransport +{ CopyTransportS1 tr1; public Queue backlog = new Queue(); - public CopyTransportS2(CopyTransportS1 tr1) { + public CopyTransportS2(CopyTransportS1 tr1) + { this.tr1 = tr1; } - public override Task Write(string data) { + public override Task Write(string data) + { Console.WriteLine("--> " + data); this.tr1.SendPacketEvent(data); @@ -52,28 +62,34 @@ class CopyTransportS2 : Example.JRpcTransport { } } -class CopyTransportS1 : Example.JRpcTransport { +class CopyTransportS1 : Example.JRpcTransport +{ public Queue backlog = new Queue(); public CopyTransportS2 tr2; - public CopyTransportS1() { + public CopyTransportS1() + { this.tr2 = new CopyTransportS2(this); } - public override Task Write(string data) { + public override Task Write(string data) + { Console.WriteLine("<-- " + data); this.tr2.SendPacketEvent(data); return Task.CompletedTask; } } -class Program { - public static void Main() { +class Program +{ + public static void Main() + { Program.Start().Wait(); } - public static async Task Start() { + public static async Task Start() + { var server = new Example.JRpcServer(); server.AddService(new TestSrvimpl()); var transport = new CopyTransportS1(); @@ -82,11 +98,19 @@ class Program { var client = new Example.JRpcClient(transport.tr2); var testService = new Example.TestServiceClient(client); - var result = await testService.AddValuesMultipleParams(1,2); + var result = await testService.AddValuesMultipleParams(1, 2); Console.WriteLine($"Add 1 + 2 = {result}"); - await testService.ThrowingError(); + try + { + await testService.ThrowingError(); + throw new Exception("No error was issued, even though it was expected!"); + } + catch (JRpcException err) + { + Console.WriteLine("Error successfully catched: ", err.Message); + } } }