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) { CalculatorServiceClient calculatorServiceClient = new CalculatorServiceClient(); calculatorServiceClient.Add(1.0); Console.WriteLine("calculatorServiceClient.Add(1.0)={0}", calculatorServiceClient.GetResult()); calculatorServiceClient.Add(22.0); Console.WriteLine("calculatorServiceClient.Add(22.0)={0}", calculatorServiceClient.GetResult()); Console.Read(); }
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(); } }
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 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 Main(string[] args) { CalculatorServiceClient client = new CalculatorServiceClient(); Console.WriteLine(client.Add(100, 1000)); Console.WriteLine("Press Enter to close"); Console.ReadLine(); }
public static void Main() { try { // Instantiate the client with a CustomBinding which has DiscoveryClientBindingElement CalculatorServiceClient client = new CalculatorServiceClient( CreateCustomBindingWithDiscoveryElement(), DiscoveryClientBindingElement.DiscoveryEndpointAddress); Console.WriteLine("Discovering and invoking CalculatorService."); double value1 = 1023; double value2 = 1534; double value3 = 2342; // Call the Add service operation. double result = client.Add(value1, value2); Console.WriteLine("Adding({0}, {1}) = {2}", value1, value2, result); // Call the Subtract service operation. result = client.Subtract(value3, value2); Console.WriteLine("Subtracting ({0}, {1}) = {2}", value3, value2, result); // Closing the client gracefully closes the connection and cleans up resources client.Close(); } catch (EndpointNotFoundException) { Console.WriteLine("Unable to connect to the calculator service because a valid endpoint was not found."); } Console.WriteLine("Press <ENTER> to exit."); Console.ReadLine(); }
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() { // 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(); }
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(); } }
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(); } }
static void Main(string[] args) { Console.ReadLine(); CalculatorServiceClient client = new CalculatorServiceClient(); Console.WriteLine(client.Add(3, 5)); Console.ReadLine(); }
// GET: Home public string Index() { using (var client = new CalculatorServiceClient()) { return(client.Add(3, 5).ToString()); } return("Error"); }
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(); } }
static void Main(string[] args) { using (CalculatorServiceClient proxy = new CalculatorServiceClient()) { Console.WriteLine("x + y = {0}", proxy.Add(1, 2)); } Console.Read(); }
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.ReadKey(); }
static void Main(string[] args) { using (CalculatorServiceClient client = new CalculatorServiceClient()) { Console.WriteLine("8 + 9 = " + client.Add(8, 9)); Console.WriteLine("4723 - 12 = " + client.Subtract(4723, 12)); } Console.ReadKey(); }
public static void Main(string[] args) { EchoServiceClient echoClient = new EchoServiceClient(); Console.WriteLine("Echo(\"Is anyone there?\") returned: " + echoClient.Echo("Is anyone there?")); echoClient.Close(); CalculatorServiceClient calculatorClient = new CalculatorServiceClient(); Console.WriteLine("Add(5) returned: " + calculatorClient.Add(5)); Console.WriteLine("Add(-3) returned: " + calculatorClient.Add(-3)); calculatorClient.Close(); Console.WriteLine(); Console.WriteLine("Press Enter to exit..."); 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) { CalculatorServiceClient calculator = new CalculatorServiceClient(); int result = calculator.Add(5, 7); Console.WriteLine(result); Console.WriteLine("\n\nDone"); Console.ReadLine(); }
static void Main(string[] args) { using (CalculatorServiceClient client = new CalculatorServiceClient()) { // In later versions(>= 7.1) of C# you could just make the Main method async // and convert the following line to use async flavor of the Add method var result = client.Add(1, 2); Console.WriteLine("Add result : {0}", result); Console.ReadLine(); } }
public static void Main() { try { DynamicEndpoint dynamicEndpoint = new DynamicEndpoint(ContractDescription.GetContract(typeof(ICalculatorService)), new NetTcpBinding()); Uri redmondScope = new Uri("net.tcp://Microsoft.Samples.Discovery/RedmondLocation"); Uri seattleScope = new Uri("net.tcp://Microsoft.Samples.Discovery/SeattleLocation"); Uri portlandScope = new Uri("net.tcp://Microsoft.Samples.Discovery/PortlandLocation"); dynamicEndpoint.FindCriteria.Scopes.Add(redmondScope); dynamicEndpoint.FindCriteria.Scopes.Add(seattleScope); dynamicEndpoint.FindCriteria.Scopes.Add(portlandScope); // Specify the custom ScopeMatchBy dynamicEndpoint.FindCriteria.ScopeMatchBy = new Uri("net.tcp://Microsoft.Samples.Discovery/ORExactMatch"); CalculatorServiceClient client = new CalculatorServiceClient(dynamicEndpoint); Console.WriteLine("Discovering CalculatorService."); Console.WriteLine("Looking for a Calculator Service that matches either of the scopes:"); Console.WriteLine(" " + redmondScope); Console.WriteLine(" " + seattleScope); Console.WriteLine(" " + portlandScope); Console.WriteLine(); double value1 = 1023; double value2 = 1534; double value3 = 2342; // Call the Add service operation. double result = client.Add(value1, value2); Console.WriteLine("Adding({0}, {1}) = {2}", value1, value2, result); // Call the Subtract service operation. result = client.Subtract(value3, value2); Console.WriteLine("Subtracting ({0}, {1}) = {2}", value3, value2, result); //Closing the client gracefully closes the connection and cleans up resources client.Close(); } catch (EndpointNotFoundException) { Console.WriteLine("Unable to connect to the calculator service because a valid endpoint was not found."); } Console.WriteLine("Press <ENTER> to exit."); 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 InvokeCalculatorService(EndpointAddress endpointAddress) { // Create a client CalculatorServiceClient client = new CalculatorServiceClient(new WSHttpBinding(), 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); //Closing the client gracefully closes the connection and cleans up resources client.Close(); }
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(); } }
public void AuthenticatedUserWithUserRole_ShouldSuccess() { // Arrange using (var client = new CalculatorServiceClient()) { var factory = client.ChannelFactory; factory.Endpoint.Behaviors.Remove <ClientCredentials>(); var loginCredentials = new ClientCredentials(); loginCredentials.UserName.UserName = "******"; loginCredentials.UserName.Password = "******"; factory.Endpoint.Behaviors.Add(loginCredentials); // Act var value = client.Add(1, 2); // Assert Assert.AreEqual(value, 3); } }
protected void Button1_Click(object sender, EventArgs e) { try { using (var client = new CalculatorServiceClient()) { int a = int.Parse(TextBoxA.Text); int b = int.Parse(TextBoxB.Text); var c = client.Add(a, b); TextBoxC.Text = c.ToString(); Response.Write(string.Format("{0}+{1}={2}", a, b, c)); } } catch (Exception ex) { Response.Write(ex.Message); } }
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(); } }
static void Main(string[] args) { RoutingConfiguration #region 使用客户端自己生成的引用 调用服务 using (CalculatorServiceClient proxy = new CalculatorServiceClient()) { Console.WriteLine("x + y = {2} when x ={0} and y = {1}", 1, 2, proxy.Add(1, 2)); } #endregion #region 使用ChannelFactory 调用服务 using (ChannelFactory <ICalculator> channelFactory = new ChannelFactory <ICalculator>(new WSHttpBinding(), "http://127.0.0.1:9999/calculatorservice")) { ICalculator proxy = channelFactory.CreateChannel(); Console.WriteLine("x + y = {2} when x ={0} and y = {1}", 1, 2, proxy.Add(1, 2)); } #endregion #region 使用channelfactory 并通过配置 指定终结点名称 "calculatorservice" using (ChannelFactory <ICalculator> channelFactory = new ChannelFactory <ICalculator>("calculatorservice")) { ICalculator proxy = channelFactory.CreateChannel(); Console.WriteLine("x + y = {2} when x ={0} and y = {1}", 1, 2, proxy.Add(1, 2)); } #endregion }