//Constructor - create a message from another message. public MyMessage(MyMessage message) { this.n1 = message.n1; this.n2 = message.n2; this.operation = message.operation; this.result = message.result; }
static void Main() { // Create a client with given client endpoint configuration CalculatorClient client = new CalculatorClient(); // Perform addition using a typed message. MyMessage request = new MyMessage(); request.N1 = 100D; request.N2 = 15.99D; request.Operation = "+"; MyMessage response = ((ICalculator)client).Calculate(request); Console.WriteLine("Add({0},{1}) = {2}", request.N1, request.N2, response.Result); // Perform subtraction using a typed message. request = new MyMessage(); request.N1 = 145D; request.N2 = 76.54D; request.Operation = "-"; response = ((ICalculator)client).Calculate(request); Console.WriteLine("Subtract({0},{1}) = {2}", request.N1, request.N2, response.Result); // Perform multiplication using a typed message. request = new MyMessage(); request.N1 = 9D; request.N2 = 81.25D; request.Operation = "*"; response = ((ICalculator)client).Calculate(request); Console.WriteLine("Multiply({0},{1}) = {2}", request.N1, request.N2, response.Result); // Perform multiplication using a typed message. request = new MyMessage(); request.N1 = 22D; request.N2 = 7D; request.Operation = "/"; response = ((ICalculator)client).Calculate(request); Console.WriteLine("Divide({0},{1}) = {2}", request.N1, request.N2, response.Result); //Closing the client gracefully closes the connection and cleans up resources client.Close(); Console.WriteLine(); Console.WriteLine("Press <ENTER> to terminate client."); Console.ReadLine(); }
// Perform a calculation. public MyMessage Calculate(MyMessage request) { MyMessage response = new MyMessage(request); switch (request.Operation) { case "+": response.Result = request.N1 + request.N2; break; case "-": response.Result = request.N1 - request.N2; break; case "*": response.Result = request.N1 * request.N2; break; case "/": response.Result = request.N1 / request.N2; break; default: response.Result = 0.0D; break; } return response; }