|
| 1 | +// Copyright (c) ZeroC, Inc. |
| 2 | + |
| 3 | +// Create an Ice communicator to initialize the Ice runtime. |
| 4 | +using Ice.Communicator communicator = Ice.Util.initialize(ref args); |
| 5 | + |
| 6 | +// Create a plain proxy to the remote object. |
| 7 | +Ice.ObjectPrx greeter = Ice.ObjectPrxHelper.createProxy(communicator, "greeter:tcp -h localhost -p 4061"); |
| 8 | + |
| 9 | +// Create an encapsulation for the input parameter (the user name). |
| 10 | +var outputStream = new Ice.OutputStream(); |
| 11 | +outputStream.startEncapsulation(); |
| 12 | +outputStream.writeString(Environment.UserName); |
| 13 | +outputStream.endEncapsulation(); |
| 14 | + |
| 15 | +// Send a request using ice_invokeAsync and wait for the response. ice_invokeAsync can throws exceptions such as |
| 16 | +// Ice.ConnectionRefusedException (an Ice local exception), or Ice.ObjectNotExistException (another Ice local exception |
| 17 | +// reported by the server). |
| 18 | +(bool success, byte[] encapsulation) = |
| 19 | + await greeter.ice_invokeAsync( |
| 20 | + operation: "greet", |
| 21 | + mode: Ice.OperationMode.Normal, // as opposed to Idempotent |
| 22 | + inEncaps: outputStream.finished()); |
| 23 | + |
| 24 | +if (success) |
| 25 | +{ |
| 26 | + // Unmarshal the encapsulation to get the greeting. |
| 27 | + var inputStream = new Ice.InputStream(communicator, encapsulation); |
| 28 | + _ = inputStream.startEncapsulation(); |
| 29 | + string greeting = inputStream.readString(); |
| 30 | + inputStream.endEncapsulation(); |
| 31 | + |
| 32 | + Console.WriteLine(greeting); |
| 33 | +} |
| 34 | +else |
| 35 | +{ |
| 36 | + // success = false means the encapsulation holds a user exception. This should not happen since our implementation |
| 37 | + // does not throw/return any user exception. |
| 38 | + Console.WriteLine("greet failed with an unexpected user exception"); |
| 39 | +} |
0 commit comments