static void Main(string[] args) { using (CalculatorServiceClient proxy = new CalculatorServiceClient()) { Console.WriteLine("x + y = {2} when x = {0} and y = {1}", 1, 2, proxy.Add(1, 2)); Console.WriteLine("x - y = {2} when x = {0} and y = {1}", 1, 2, proxy.Subtract(1, 2)); Console.WriteLine("x * y = {2} when x = {0} and y = {1}", 1, 2, proxy.Multiply(1, 2)); Console.WriteLine("x / y = {2} when x = {0} and y = {1}", 1, 2, proxy.Divide(1, 2)); Console.ReadLine(); Console.WriteLine("x + y = {2} when x = {0} and y = {1}", 1, 2, proxy.Add(1, 2)); Console.WriteLine("x - y = {2} when x = {0} and y = {1}", 1, 2, proxy.Subtract(1, 2)); Console.WriteLine("x * y = {2} when x = {0} and y = {1}", 1, 2, proxy.Multiply(1, 2)); Console.WriteLine("x / y = {2} when x = {0} and y = {1}", 1, 2, proxy.Divide(1, 2)); Console.ReadLine(); } }
static void InvokeCalculatorService(EndpointAddress endpointAddress) { // Create a client CalculatorServiceClient client = new CalculatorServiceClient(); // Connect to the discovered service endpoint client.Endpoint.Address = endpointAddress; Console.WriteLine("Invoking CalculatorService at {0}", endpointAddress); double value1 = 100.00D; double value2 = 15.99D; // Call the Add service operation. double result = client.Add(value1, value2); Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result); // Call the Subtract service operation. result = client.Subtract(value1, value2); Console.WriteLine("Subtract({0},{1}) = {2}", value1, value2, result); // Call the Multiply service operation. result = client.Multiply(value1, value2); Console.WriteLine("Multiply({0},{1}) = {2}", value1, value2, result); // Call the Divide service operation. result = client.Divide(value1, value2); Console.WriteLine("Divide({0},{1}) = {2}", value1, value2, result); Console.WriteLine(); // Close the client to close the connection and clean up resources client.Close(); }
static void InvokeCalculatorService() { // Create a client CalculatorServiceClient client = new CalculatorServiceClient("calculatorEndpoint"); Console.WriteLine("Invoking CalculatorService"); double value1 = 100.00D; double value2 = 15.99D; // Call the Add service operation. double result = client.Add(value1, value2); Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result); // Call the Subtract service operation. result = client.Subtract(value1, value2); Console.WriteLine("Subtract({0},{1}) = {2}", value1, value2, result); // Call the Multiply service operation. result = client.Multiply(value1, value2); Console.WriteLine("Multiply({0},{1}) = {2}", value1, value2, result); // Call the Divide service operation. result = client.Divide(value1, value2); Console.WriteLine("Divide({0},{1}) = {2}", value1, value2, result); Console.WriteLine(); //Closing the client gracefully closes the connection and cleans up resources client.Close(); }
private void btnCalc_Click(object sender, EventArgs e) { CalculatorServiceClient loClient = new CalculatorServiceClient(); Int32 loNum1 = Convert.ToInt32(txtNum1.Text.Trim()); Int32 loNum2 = Convert.ToInt32(txtNum2.Text.Trim()); if (cboOperation.Text == "Add") { txtResult.Text = loClient.Add(loNum1, loNum2).ToString(); } else if (cboOperation.Text == "Subtract") { txtResult.Text = loClient.Subtract(loNum1, loNum2).ToString(); } else if (cboOperation.Text == "Multiply") { txtResult.Text = loClient.Multiply(loNum1, loNum2).ToString(); } else { txtResult.Text = loClient.Divide(loNum1, loNum2).ToString(); } }
public static void Write(string strChooseNo) { using (CalculatorServiceClient proxy = new CalculatorServiceClient()) { switch (strChooseNo) { case "1": Console.WriteLine("x + y = {2} when x = {0} and y = {1}", 1, 2, proxy.Add(1, 2)); Write(ChooseNumber()); break; case "2": Console.WriteLine("x - y = {2} when x = {0} and y = {1}", 1, 2, proxy.Subtract(1, 2)); Write(ChooseNumber()); break; case "3": Console.WriteLine("x * y = {2} when x = {0} and y = {1}", 1, 2, proxy.Multiply(1, 2)); Write(ChooseNumber()); break; case "4": Console.WriteLine("x / y = {2} when x = {0} and y = {1}", 1, 2, proxy.Divide(1, 2)); Write(ChooseNumber()); break; default: Console.WriteLine("输入错误,请重新输入数字!"); Write(ChooseNumber()); break; } } }
static void InvokeCalculatorService(EndpointAddress endpointAddress) { // Create a client CalculatorServiceClient client = new CalculatorServiceClient(new NetTcpBinding(), endpointAddress); Console.WriteLine("Invoking CalculatorService at {0}", endpointAddress.Uri); Console.WriteLine(); double value1 = 100.00D; double value2 = 15.99D; // Call the Add service operation. double result = client.Add(value1, value2); Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result); // Call the Subtract service operation. result = client.Subtract(value1, value2); Console.WriteLine("Subtract({0},{1}) = {2}", value1, value2, result); // Call the Multiply service operation. result = client.Multiply(value1, value2); Console.WriteLine("Multiply({0},{1}) = {2}", value1, value2, result); // Call the Divide service operation. result = client.Divide(value1, value2); Console.WriteLine("Divide({0},{1}) = {2}", value1, value2, result); Console.WriteLine(); // Closing the client gracefully closes the connection and cleans up resources client.Close(); }
static void Main(string[] args) { try { CalculatorServiceClient cs = new CalculatorServiceClient(); double result = cs.Add(10.0, 20.0); Console.WriteLine("Result" + result); double result2 = cs.Divide(10.0, 0.0); //string path = Path.Combine(Path.GetTempPath(), "AdminChecker.exe"); ////var code = String.Format(@"if (File.Exists(@""{0}"")) //// {{ //// System.Diagnostics.Process p = new System.Diagnostics.Process(); //// p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; //// p.StartInfo.FileName = @""{0}""; //// p.Start(); //// }}", path); //var res = cs.RunAsAdmin(path); //if (res != null) // Console.WriteLine("Error "+res.ToString()); Console.WriteLine("Result2" + result2); Console.ReadLine(); } catch (Exception ex) { Console.WriteLine("error " + ex.Message); Console.ReadLine(); } }
public void TestAllOperations() { Assert.AreEqual(7, _client.Add(3, 4)); Assert.AreEqual(3, _client.Subtract(4, 1)); Assert.AreEqual(12, _client.Multiply(3, 4)); Assert.AreEqual(2, _client.Divide(5, 2)); int addResult, subtractResult, multiplyResult, divideResult; // DoItAll a void method with 6 parameters in the SOAP interface // Here it is an int method with only 5 parameters: the first out parameters is used as the return value!! addResult = _client.DoItAll(10, 3, out subtractResult, out multiplyResult, out divideResult); Assert.AreEqual(13, addResult); Assert.AreEqual(7, subtractResult); Assert.AreEqual(30, multiplyResult); Assert.AreEqual(3, divideResult); }
static void Main(string[] args) { CalculatorServiceClient proxy = new CalculatorServiceClient(new BasicHttpBinding(), new EndpointAddress("http://localhost:8000/PocoServiceHost/CalculatorService")); int res = proxy.Divide(10, 5); Console.WriteLine(res); Console.ReadLine(); }
private void CalculateResults() { CalculatorServiceClient proxy = null; try { double value1 = Convert.ToDouble(textValue1.Text); double value2 = Convert.ToDouble(textValue2.Text); string endpointName; if (ComboBoxServiceConnection.SelectedIndex == 0) endpointName = "CalculatorService"; else { endpointName = "RouterService"; } proxy = new CalculatorServiceClient(endpointName); using (OperationContextScope scope = new OperationContextScope(proxy.InnerChannel)) { AddOptionalRoundingHeader(proxy); labelAddResult.Text = proxy.Add(value1, value2).ToString(); labelSubResult.Text = proxy.Subtract(value1, value2).ToString(); labelMultResult.Text = proxy.Multiply(value1, value2).ToString(); if (value2 != 0.00) labelDivResult.Text = proxy.Divide(value1, value2).ToString(); else labelDivResult.Text = "Divide by 0"; proxy.Close(); } } catch (FormatException) { MessageBox.Show("Invalid numeric value, cannot calculate", "Error", MessageBoxButton.OK, MessageBoxImage.Error); } catch (TimeoutException) { if (proxy != null) proxy.Abort(); MessageBox.Show("Timeout - cannot connect to service", "Error", MessageBoxButton.OK, MessageBoxImage.Error); } catch (CommunicationException) { if (proxy != null) proxy.Abort(); MessageBox.Show("Unable to communicate with the service", "Error", MessageBoxButton.OK, MessageBoxImage.Error); } finally { if (proxy != null) proxy.Close(); } }
public static void Main(string[] args) { /* * ListCounters("ServiceModelEndpoint 4.0.0.0"); * ListCounters("ServiceModelOperation 4.0.0.0"); * ListCounters("ServiceModelService 4.0.0.0"); */ Console.WriteLine("Press <ENTER> to start client."); Console.WriteLine(); Console.ReadLine(); CalculatorServiceClient client = new CalculatorServiceClient(); double value1 = 100.0; double value2 = 15.99; double result = client.Add(value1, value2); Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result); value1 = 145.0; value2 = 76.54; result = client.Subtract(value1, value2); Console.WriteLine("Subtract({0},{1}) = {2}", value1, value2, result); value1 = 9.0; value2 = 81.25; result = client.Multiply(value1, value2); Console.WriteLine("Multiply({0},{1}) = {2}", value1, value2, result); value1 = 22.0; value2 = 7.0; result = client.Divide(value1, value2); Console.WriteLine("Divide({0},{1}) = {2}", value1, value2, result); PerformanceCounter counter = new PerformanceCounter("Test category", "Test counter"); Random random = new Random(); bool running = true; while (running) { value1 = random.NextDouble() * 100.0; value2 = random.NextDouble() * 100.0; Parallel.Invoke( () => client.Add(value1, value2), () => client.Add(value1, value2), () => client.Add(value1, value2) ); Console.WriteLine("Test: {0}", counter.NextValue()); } client.Close(); Console.WriteLine(); Console.WriteLine("Press <ENTER> to terminate client."); Console.ReadLine(); }
static void Main(string[] args) { using (CalculatorServiceClient proxy = new CalculatorServiceClient()) { Console.WriteLine("x + y = {2} when x = {0} and y = {1}", 1, 2, proxy.Add(1, 2)); Console.WriteLine("x - y = {2} when x = {0} and y = {1}", 1, 2, proxy.Subtract(1, 2)); Console.WriteLine("x * y = {2} when x = {0} and y = {1}", 1, 2, proxy.Multiply(1, 2)); Console.WriteLine("x / y = {2} when x = {0} and y = {1}", 1, 2, proxy.Divide(1, 2)); } }
static void Main(string[] args) { using (CalculatorServiceClient proxy = new CalculatorServiceClient()) { try { Console.WriteLine("x+y={2} when x={0} and y={1}", 1, 2, proxy.Add(1, 2)); Console.WriteLine("x/y={2} when x={0} and y={1}", 1, 0, proxy.Divide(1, 0)); } catch (Exception e) { Console.WriteLine(e.ToString()); } //服务异常 通道已处于Fault状态 无法使用 //Console.WriteLine("x*y={2} when x={0} and y={1}", 2, 4, proxy.Mutiply(2, 4)); Console.Read(); } }
static void InvokeCalculatorService(EndpointAddress endpointAddress, Uri viaUri) { // Create a client CalculatorServiceClient client = new CalculatorServiceClient(new NetTcpBinding(), endpointAddress); Console.WriteLine("Invoking CalculatorService at {0}", endpointAddress.Uri); // if viaUri is not null then add the approprate ClientViaBehavior. if (viaUri != null) { client.Endpoint.Behaviors.Add(new ClientViaBehavior(viaUri)); Console.WriteLine("Using the viaUri {0}", viaUri); } Console.WriteLine(); double value1 = 100.00D; double value2 = 15.99D; // Call the Add service operation. double result = client.Add(value1, value2); Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result); // Call the Subtract service operation. result = client.Subtract(value1, value2); Console.WriteLine("Subtract({0},{1}) = {2}", value1, value2, result); // Call the Multiply service operation. result = client.Multiply(value1, value2); Console.WriteLine("Multiply({0},{1}) = {2}", value1, value2, result); // Call the Divide service operation. result = client.Divide(value1, value2); Console.WriteLine("Divide({0},{1}) = {2}", value1, value2, result); Console.WriteLine(); // Closing the client gracefully closes the connection and cleans up resources client.Close(); }
private static void Main(string[] args) { try { using (var proxy = new CalculatorServiceClient()) { Console.WriteLine("x + y = {2} when x = {0} and y = {1}", 1, 2, proxy.Add(1, 2)); Console.WriteLine("x - y = {2} when x = {0} and y = {1}", 1, 2, proxy.Subtract(1, 2)); Console.WriteLine("x * y = {2} when x = {0} and y = {1}", 1, 2, proxy.Multiply(1, 2)); Console.WriteLine("x / y = {2} when x = {0} and y = {1}", 1, 2, proxy.Divide(1, 2)); } } catch (Exception ex) { Console.WriteLine("Error :\n {0}",ex.Message); } Console.Read(); }
static void Main(string[] args) { // 调用控制台寄宿 using (var proxy = new CalculatorServiceClient()) { Console.WriteLine($"x + y = { proxy.Add(4, 2)} when x = 4 and y = 2"); Console.WriteLine($"x - y = { proxy.Subtract(4, 2)} when x = 4 and y = 2"); Console.WriteLine($"x * y = { proxy.Multiply(4, 2)} when x = 4 and y = 2"); Console.WriteLine($"x / y = { proxy.Divide(4, 2)} when x = 4 and y = 2"); Console.WriteLine(); } // 由于服务端和客户端都是在同一个解决方案中,完全可以让服务端和客户端引用相同的契约 using (var channelFactory = new ChannelFactory <ICalculator>(new WSHttpBinding(), "http://127.0.0.1:9999/calculatorservice")) { ICalculator proxy = channelFactory.CreateChannel(); using (proxy as IDisposable) { Console.WriteLine($"x + y = { proxy.Add(4, 2)} when x = 4 and y = 2"); Console.WriteLine($"x - y = { proxy.Subtract(4, 2)} when x = 4 and y = 2"); Console.WriteLine($"x * y = { proxy.Multiply(4, 2)} when x = 4 and y = 2"); Console.WriteLine($"x / y = { proxy.Divide(4, 2)} when x = 4 and y = 2"); Console.WriteLine(); } } // 调用IIS寄宿 using (var proxy = new CalculatorServicesWeb.CalculatorClient()) { Console.WriteLine($"x + y = { proxy.Add(4, 2)} when x = 4 and y = 2"); Console.WriteLine($"x - y = { proxy.Subtract(4, 2)} when x = 4 and y = 2"); Console.WriteLine($"x * y = { proxy.Multiply(4, 2)} when x = 4 and y = 2"); Console.WriteLine($"x / y = { proxy.Divide(4, 2)} when x = 4 and y = 2"); Console.WriteLine(); } }
private void button1_Click(object sender, RoutedEventArgs e) { var result = client.Divide(Convert.ToInt32(txtNumerator.Text), Convert.ToInt32(txtDenominator.Text)); lblResultDivide.Content = result.ToString(); }
static void Main(string[] args) { using (CalculatorServiceClient proxy = new CalculatorServiceClient()) { Calculator t1 = new Calculator { x1 = 1 }; Calculator t2 = new Calculator { y1 = 2 }; Console.WriteLine("x + y = {2} when x = {0} and y = {1}", t1.x1, t2.y1, proxy.AddExy(t1, t2).tp); Console.WriteLine("x - y = {2} when x = {0} and y = {1}", t1.x1, t2.y1, proxy.Subtract(t1, t2).tp); Console.WriteLine("x * y = {2} when x = {0} and y = {1}", t1.x1, t2.y1, proxy.Multiply(t1, t2).tp); Console.WriteLine("x / y = {2} when x = {0} and y = {1}", t1.x1, t2.y1, proxy.Divide(t1, t2).tp); Console.ReadKey(); } }