private void button1_Click(object sender, RoutedEventArgs e) { using (var proxy = new CalculatorClient()) { int result1 = proxy.Add(1, 2); double result2 = proxy.Add(1.0, 2.0); labelUsualResult.Content = string.Format("int: {0}, double: {1}", result1, result2); } }
public static void CallService(string endpointName) { CalculatorClient client = new CalculatorClient(endpointName); Console.WriteLine("Calling Endpoint: {0}", endpointName); // Call the Add service operation. double value1 = 100.00D; double value2 = 15.99D; double result = client.Add(value1, value2); Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result); // Call the Subtract service operation. value1 = 145.00D; value2 = 76.54D; result = client.Subtract(value1, value2); Console.WriteLine("Subtract({0},{1}) = {2}", value1, value2, result); // Call the Multiply service operation. value1 = 9.00D; value2 = 81.25D; result = client.Multiply(value1, value2); Console.WriteLine("Multiply({0},{1}) = {2}", value1, value2, result); // Call the Divide service operation. value1 = 22.00D; value2 = 7.00D; result = client.Divide(value1, value2); Console.WriteLine("Divide({0},{1}) = {2}", value1, value2, result); Console.WriteLine(); client.Close(); }
static void Main(string[] args) { var client = new CalculatorClient(); double value1 = 100.00D; double value2 = 15.99D; double result = client.Add(value1, value2); Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result); // Call the Subtract service operation. value1 = 145.00D; value2 = 76.54D; result = client.Subtract(value1, value2); Console.WriteLine("Subtract({0},{1}) = {2}", value1, value2, result); // Call the Multiply service operation. value1 = 9.00D; value2 = 81.25D; result = client.Multiply(value1, value2); Console.WriteLine("Multiply({0},{1}) = {2}", value1, value2, result); // Call the Divide service operation. value1 = 22.00D; value2 = 7.00D; result = client.Divide(value1, value2); Console.WriteLine("Divide({0},{1}) = {2}", value1, value2, result); client.Close(); Console.WriteLine(); Console.WriteLine("Press <ENTER> to terminate client."); Console.ReadLine(); }
static void Main() { // Create a proxy with given client endpoint configuration CalculatorClient client = new CalculatorClient(); // Call the Add service operation. double value1 = 100.00D; double value2 = 15.99D; double result = client.Add(value1, value2); Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result); // Call the Subtract service operation. value1 = 145.00D; value2 = 76.54D; result = client.Subtract(value1, value2); Console.WriteLine("Subtract({0},{1}) = {2}", value1, value2, result); // Call the Multiply service operation. value1 = 9.00D; value2 = 81.25D; result = client.Multiply(value1, value2); Console.WriteLine("Multiply({0},{1}) = {2}", value1, value2, result); // Call the Divide service operation. value1 = 22.00D; value2 = 7.00D; result = client.Divide(value1, value2); Console.WriteLine("Divide({0},{1}) = {2}", value1, value2, result); client.Close(); Console.WriteLine(); Console.WriteLine("Press <ENTER> to terminate client."); Console.ReadLine(); }
static void Main(string[] args) { // Create a proxy with default client endpoint configuration. CalculatorClient client = new CalculatorClient(); // Add double value1 = 100.00D; double value2 = 15.99D; double result = client.Add(value1, value2); Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result); // Subtract value1 = 145.00D; value2 = 76.54D; result = client.Subtract(value1, value2); Console.WriteLine("Subtract({0},{1}) = {2}", value1, value2, result); // Multiply value1 = 9.00D; value2 = 81.25D; result = client.Multiply(value1, value2); Console.WriteLine("Multiply({0},{1}) = {2}", value1, value2, result); // Divide value1 = 22.00D; value2 = 7.00D; result = client.Divide(value1, value2); Console.WriteLine("Divide({0},{1}) = {2}", value1, value2, 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(); }
static void Main(string[] args) { //create an instance of wcf proxy CalculatorClient client = new CalculatorClient(); //call the service operations double value1 = 100.00d; double value2 = 15.99d; double result= client.Add(value1,value2); Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result); // Call the Subtract service operation. value1 = 145.00D; value2 = 76.54D; result = client.Subtract(value1, value2); Console.WriteLine("Subtract({0},{1}) = {2}", value1, value2, result); // Call the Multiply service operation. value1 = 9.00D; value2 = 81.25D; result = client.Multiply(value1, value2); Console.WriteLine("Multiply({0},{1}) = {2}", value1, value2, result); // Call the Divide service operation. value1 = 22.00D; value2 = 7.00D; result = client.Divide(value1, value2); Console.WriteLine("Divide({0},{1}) = {2}", value1, value2, result); //Step 3: Closing the client gracefully closes the connection and cleans up resources. client.Close(); }
static void InvokeCalculatorService(EndpointAddress endpointAddress) { //create a client CalculatorClient client = new CalculatorClient(); 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(); //Closing the client gracefully closes the connection and cleans up resources client.Close(); }
static void Main() { // Create a client CalculatorClient client = new CalculatorClient("NetTcpBinding_ICalculator"); // Call the Add service operation. double value1 = 100.00D; double value2 = 15.99D; double result = client.Add(value1, value2); Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result); // Call the Subtract service operation. value1 = 145.00D; value2 = 76.54D; result = client.Subtract(value1, value2); Console.WriteLine("Subtract({0},{1}) = {2}", value1, value2, result); // Call the Multiply service operation. value1 = 9.00D; value2 = 81.25D; result = client.Multiply(value1, value2); Console.WriteLine("Multiply({0},{1}) = {2}", value1, value2, 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(); }
static void DoCalculations(CalculatorClient client) { // Call the Add service operation. double value1 = 100.00D; double value2 = 15.99D; double result = client.Add(value1, value2); Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result); // Call the Subtract service operation. value1 = 145.00D; value2 = 76.54D; result = client.Subtract(value1, value2); Console.WriteLine("Subtract({0},{1}) = {2}", value1, value2, result); // Call the Multiply service operation. value1 = 9.00D; value2 = 81.25D; result = client.Multiply(value1, value2); Console.WriteLine("Multiply({0},{1}) = {2}", value1, value2, result); // Call the Divide service operation. value1 = 22.00D; value2 = 7.00D; result = client.Divide(value1, value2); Console.WriteLine("Divide({0},{1}) = {2}", value1, value2, result); }
static void Main(string[] args) { ServiceReference1.CalculatorClient client = new CalculatorClient(); double value1 = 100.00D; double value2 = 15.99D; double result = client.Add(value1, value2); Console.WriteLine("Add({0},{1}) = {2}", value1,value2, result); value1 = 145.00D; value2 = 76.54D; result = client.Substract(value1, value2); Console.WriteLine("Substract({0},{1}) = {2}",value1,value2,result); value1 = 9.00D; value2 = 81.25D; result = client.Multiply(value1, value2); Console.WriteLine("Mutiply({0},{1}) = {2}", value1, value2, result); value1 = 22.00D; value2 = 7.00D; result = client.Divide(value1, value2); Console.WriteLine("Divide({0},{1}) = {2}", value1, value2, result); client.Close(); Console.ReadLine(); }
static void Main(string[] args) { CalculatorClient proxy = new CalculatorClient(); Console.WriteLine("Client is running at " + DateTime.Now.ToString()); Console.WriteLine("Sum of two numbers... 5+5 =" + proxy.Add(5, 5)); Console.ReadLine(); }
public double Add(double n1, double n2) { CalculatorClient client = new CalculatorClient(); client.ClientCredentials.UserName.UserName = ServiceSecurityContext.Current.PrimaryIdentity.Name; double result = client.Add(n1, n2); client.Close(); return result; }
static void Main(string[] args) { CalculatorClient svc = new CalculatorClient(); Console.WriteLine(svc.Add(99.3, 29.8)); svc.Close(); }
static void Main(string[] args) { CalculatorClient client = new CalculatorClient(); // 使用 "client" 变量在服务上调用操作。 var result = client.Add(1, 2); // 始终关闭客户端。 client.Close(); }
static void Main(string[] args) { CalculatorClient client = new CalculatorClient(); Console.WriteLine(client.Add(10, 20)); Console.ReadKey(); client.Close(); }
static void Main() { // Create a client with Certificate endpoint configuration CalculatorClient client = new CalculatorClient("Certificate"); try { client.ClientCredentials.ClientCertificate.SetCertificate(StoreLocation.CurrentUser, StoreName.My, X509FindType.FindBySubjectName, "alice"); // Call the Add service operation. double value1 = 100.00D; double value2 = 15.99D; double result = client.Add(value1, value2); Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result); // Call the Subtract service operation. value1 = 145.00D; value2 = 76.54D; result = client.Subtract(value1, value2); Console.WriteLine("Subtract({0},{1}) = {2}", value1, value2, result); // Call the Multiply service operation. value1 = 9.00D; value2 = 81.25D; result = client.Multiply(value1, value2); Console.WriteLine("Multiply({0},{1}) = {2}", value1, value2, result); // Call the Divide service operation. value1 = 22.00D; value2 = 7.00D; result = client.Divide(value1, value2); Console.WriteLine("Divide({0},{1}) = {2}", value1, value2, result); client.Close(); } catch (TimeoutException e) { Console.WriteLine("Call timed out : {0}", e.Message); client.Abort(); } catch (CommunicationException e) { Console.WriteLine("Call failed : {0}", e.Message); client.Abort(); } catch (Exception e) { Console.WriteLine("Call failed : {0}", e.Message); client.Abort(); } Console.WriteLine(); Console.WriteLine("Press <ENTER> to terminate client."); Console.ReadLine(); }
static void Main() { // Create a client with given client endpoint configuration CalculatorClient client = new CalculatorClient(); try { // Call the Add service operation. int value1 = 15; int value2 = 3; int result = client.Add(value1, value2); Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result); // Call the Subtract service operation. value1 = 145; value2 = 76; result = client.Subtract(value1, value2); Console.WriteLine("Subtract({0},{1}) = {2}", value1, value2, result); // Call the Multiply service operation. value1 = 9; value2 = 81; result = client.Multiply(value1, value2); Console.WriteLine("Multiply({0},{1}) = {2}", value1, value2, result); // Call the Divide service operation - trigger a divide by zero error. value1 = 22; value2 = 0; result = client.Divide(value1, value2); Console.WriteLine("Divide({0},{1}) = {2}", value1, value2, result); //Closing the client gracefully closes the connection and cleans up resources client.Close(); } catch (FaultException<MathFault> e) { Console.WriteLine("FaultException<MathFault>: Math fault while doing " + e.Detail.Operation + ". Problem: " + e.Detail.ProblemType); client.Abort(); } catch (FaultException e) { Console.WriteLine("Unknown FaultException: " + e.GetType().Name + " - " + e.Message); client.Abort(); } catch (Exception e) { Console.WriteLine("EXCEPTION: " + e.GetType().Name + " - " + e.Message); client.Abort(); } Console.WriteLine(); Console.WriteLine("Press <ENTER> to terminate client."); Console.ReadLine(); }
static void Main(string[] args) { var client = new CalculatorClient(); Console.WriteLine("{0} + {1} = {2}", 2, 3, client.Add(2, 3)); Console.WriteLine("{0} - {1} = {2}", 2, 3, client.Subtract(2, 3)); Console.WriteLine("{0} * {1} = {2}", 2, 3, client.Multiply(2, 3)); Console.WriteLine("{0} / {1} = {2}", 2, 3, client.Divide(2, 3)); Console.ReadLine(); client.Close(); }
/// <summary> /// Makes calls to the specified endpoint name in app.config /// </summary> /// <param name="endpointName">The endpoint to use from app.config</param> private static void CallEndpoint(string endpointName) { Console.WriteLine("\nCalling endpoint {0}\n", endpointName); // Create a client with given client endpoint configuration CalculatorClient client = new CalculatorClient(endpointName); // Create new credentials class SamlClientCredentials samlCC = new SamlClientCredentials(); // Set the client certificate. This is the cert that will be used to sign the SAML token in the symmetric proof key case samlCC.ClientCertificate.SetCertificate(StoreLocation.CurrentUser, StoreName.My, X509FindType.FindBySubjectName, "Alice"); // Set the service certificate. This is the cert that will be used to encrypt the proof key in the symmetric proof key case samlCC.ServiceCertificate.SetDefaultCertificate(StoreLocation.CurrentUser, StoreName.TrustedPeople, X509FindType.FindBySubjectName, "localhost"); // Create some claims to put in the SAML assertion IList<Claim> claims = new List<Claim>(); claims.Add(Claim.CreateNameClaim(samlCC.ClientCertificate.Certificate.Subject)); ClaimSet claimset = new DefaultClaimSet(claims); samlCC.Claims = claimset; // set new credentials client.ChannelFactory.Endpoint.Behaviors.Remove(typeof(ClientCredentials)); client.ChannelFactory.Endpoint.Behaviors.Add(samlCC); // Call the Add service operation. double value1 = 100.00D; double value2 = 15.99D; double result = client.Add(value1, value2); Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result); // Call the Subtract service operation. value1 = 145.00D; value2 = 76.54D; result = client.Subtract(value1, value2); Console.WriteLine("Subtract({0},{1}) = {2}", value1, value2, result); // Call the Multiply service operation. value1 = 9.00D; value2 = 81.25D; result = client.Multiply(value1, value2); Console.WriteLine("Multiply({0},{1}) = {2}", value1, value2, result); // Call the Divide service operation. value1 = 22.00D; value2 = 7.00D; result = client.Divide(value1, value2); Console.WriteLine("Divide({0},{1}) = {2}", value1, value2, result); client.Close(); }
static void Main() { // Create a client CalculatorClient client = new CalculatorClient(); // Call the Add service operation. double value1 = 100.00D; double value2 = 15.99D; double result = client.Add(value1, value2); Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result); // Call the Subtract service operation. value1 = 145.00D; value2 = 76.54D; result = client.Subtract(value1, value2); Console.WriteLine("Subtract({0},{1}) = {2}", value1, value2, result); // Call the Multiply service operation. value1 = 9.00D; value2 = 81.25D; result = client.Multiply(value1, value2); Console.WriteLine("Multiply({0},{1}) = {2}", value1, value2, result); // Call the Divide service operation. value1 = 22.00D; value2 = 7.00D; result = client.Divide(value1, value2); Console.WriteLine("Divide({0},{1}) = {2}", value1, value2, result); // Call the Sum service operation. int[] values = { 1, 2, 3, 4, 5 }; using (new OperationContextScope(client.InnerChannel)) { Message request = Message.CreateMessage(OperationContext.Current.OutgoingMessageHeaders.MessageVersion, "http://Microsoft.ServiceModel.Samples/ICalculator/Sum", values); Message reply = client.Sum(request); int sum = reply.GetBody<int>(); Console.WriteLine("Sum(1,2,3,4,5) = {0}", sum); } //Closing the client gracefully closes the connection and cleans up resources client.Close(); Console.WriteLine(); Console.WriteLine("Press <ENTER> to terminate client."); Console.ReadLine(); }
static void Main() { // Create a client with given client endpoint configuration CalculatorClient client = new CalculatorClient(); // set new credentials client.ChannelFactory.Endpoint.Behaviors.Remove(typeof(ClientCredentials)); client.ChannelFactory.Endpoint.Behaviors.Add(new MyUserNameClientCredentials()); /* Setting the CertificateValidationMode to PeerOrChainTrust means that if the certificate is in the Trusted People store, then it will be trusted without performing a validation of the certificate's issuer chain. This setting is used here for convenience so that the sample can be run without having to have certificates issued by a certificate authority (CA). This setting is less secure than the default, ChainTrust. The security implications of this setting should be carefully considered before using PeerOrChainTrust in production code. */ client.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.PeerOrChainTrust; // Call the Add service operation. double value1 = 100.00D; double value2 = 15.99D; double result = client.Add(value1, value2); Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result); // Call the Subtract service operation. value1 = 145.00D; value2 = 76.54D; result = client.Subtract(value1, value2); Console.WriteLine("Subtract({0},{1}) = {2}", value1, value2, result); // Call the Multiply service operation. value1 = 9.00D; value2 = 81.25D; result = client.Multiply(value1, value2); Console.WriteLine("Multiply({0},{1}) = {2}", value1, value2, result); // Call the Divide service operation. value1 = 22.00D; value2 = 7.00D; result = client.Divide(value1, value2); Console.WriteLine("Divide({0},{1}) = {2}", value1, value2, result); client.Close(); Console.WriteLine(); Console.WriteLine("Press <ENTER> to terminate client."); Console.ReadLine(); }
static void Main() { // Create a proxy with given client endpoint configuration CalculatorClient client = new CalculatorClient(); // set the certificate on the client client.ClientCredentials.ClientCertificate.SetCertificate(StoreLocation.CurrentUser, StoreName.My, X509FindType.FindBySubjectName, "client.com"); client.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.PeerOrChainTrust; // Call the GetCallerIdentity service operation Console.WriteLine(client.GetCallerIdentity()); // Call the Add service operation. double value1 = 100.00D; double value2 = 15.99D; double result = client.Add(value1, value2); Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result); // Call the Subtract service operation. value1 = 145.00D; value2 = 76.54D; result = client.Subtract(value1, value2); Console.WriteLine("Subtract({0},{1}) = {2}", value1, value2, result); // Call the Multiply service operation. value1 = 9.00D; value2 = 81.25D; result = client.Multiply(value1, value2); Console.WriteLine("Multiply({0},{1}) = {2}", value1, value2, result); // Call the Divide service operation. value1 = 22.00D; value2 = 7.00D; result = client.Divide(value1, value2); Console.WriteLine("Divide({0},{1}) = {2}", value1, value2, 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(); }
static void CallWcfService() { EndpointAddress address = new EndpointAddress(new Uri("http://localhost:8000/servicemodelsamples/service"), EndpointIdentity.CreateDnsIdentity("localhost")); WseHttpBinding binding = new WseHttpBinding(); binding.SecurityAssertion = WseSecurityAssertion.AnonymousForCertificate; binding.EstablishSecurityContext = true; binding.RequireDerivedKeys = true; binding.MessageProtectionOrder = MessageProtectionOrder.SignBeforeEncrypt; // Use the calculator client in generatedClient.cs CalculatorClient client = new CalculatorClient(binding, address); client.ClientCredentials.ServiceCertificate.SetDefaultCertificate( StoreLocation.CurrentUser, StoreName.TrustedPeople, X509FindType.FindBySubjectName, "localhost"); double value1 = 100.00D; double value2 = 15.99D; double result = client.Add(value1, value2); Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result); // Call the Subtract service operation. value1 = 145.00D; value2 = 76.54D; result = client.Subtract(value1, value2); Console.WriteLine("Subtract({0},{1}) = {2}", value1, value2, result); // Call the Multiply service operation. value1 = 9.00D; value2 = 81.25D; result = client.Multiply(value1, value2); Console.WriteLine("Multiply({0},{1}) = {2}", value1, value2, result); // Call the Divide service operation. value1 = 22.00D; value2 = 7.00D; result = client.Divide(value1, value2); Console.WriteLine("Divide({0},{1}) = {2}", value1, value2, result); //Closing the client gracefully closes the connection and cleans up resources client.Close(); Console.WriteLine("Press <ENTER> to terminate client."); Console.ReadLine(); }
static void DemonstrateCommunicationException() { // Create a client CalculatorClient client = new CalculatorClient(); try { // Call the Add service operation. double value1 = 100.00D; double value2 = 15.99D; double result = client.Add(value1, value2); Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result); // Simulate a network problem by aborting the connection. Console.WriteLine("Simulated network problem occurs..."); client.Abort(); // Call the Divide service operation. Now that the channel has been // abruptly terminated, the next call will fail. value1 = 22.00D; value2 = 7.00D; result = client.Divide(value1, value2); Console.WriteLine("Divide({0},{1}) = {2}", value1, value2, result); // SHOULD NOT GET HERE -- Divide should throw // If we had gotten here, we would want to close the client gracefully so // that the channel closes gracefully and cleans up resources. client.Close(); Console.WriteLine("Service successfully returned all results."); } catch (TimeoutException exception) { Console.WriteLine("Got {0}", exception.GetType()); client.Abort(); } catch (CommunicationException exception) { // Control comes here when client.Divide throws. The actual Exception // type is CommunicationObjectAbortedException, which is a subclass of // CommunicationException. Console.WriteLine("Got {0}", exception.GetType()); client.Abort(); } }
static void Main() { // Create a client CalculatorClient client = new CalculatorClient(); // Call the Add service operation. double value1 = 100.00D; double value2 = 15.99D; double result = client.Add(value1, value2); Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result); // Call the Subtract service operation. value1 = 145.00D; value2 = 76.54D; result = client.Subtract(value1, value2); Console.WriteLine("Subtract({0},{1}) = {2}", value1, value2, result); // Call the Multiply service operation. value1 = 9.00D; value2 = 81.25D; result = client.Multiply(value1, value2); Console.WriteLine("Multiply({0},{1}) = {2}", value1, value2, result); // Call the Divide service operation. value1 = 22.00D; value2 = 7.00D; result = client.Divide(value1, value2); Console.WriteLine("Divide({0},{1}) = {2}", value1, value2, result); Console.WriteLine(); Console.WriteLine("Client - endpoint: " + client.Endpoint.Address); Console.WriteLine("Client - binding: " + client.Endpoint.Binding.Name); Console.WriteLine("Client - contract: " + client.Endpoint.Contract.Name); IClientChannel channel = client.InnerChannel; Console.WriteLine("Client channel - state: " + channel.State); Console.WriteLine("Client channel - session identifier: " + channel.SessionId); //Closing the client gracefully closes the connection and cleans up resources client.Close(); Console.WriteLine(); Console.WriteLine("Press <ENTER> to terminate client."); Console.ReadLine(); }
private void ClientRun() { //<snippet15> // Create the binding. WSHttpBinding myBinding = new WSHttpBinding(); myBinding.Security.Mode = SecurityMode.Message; myBinding.Security.Message.ClientCredentialType = MessageCredentialType.None; // Create the endpoint address. EndpointAddress ea = new EndpointAddress("http://localhost/Calculator"); // Create the client. CalculatorClient cc = new CalculatorClient(myBinding, ea); // Begin using the client. try { cc.Open(); Console.WriteLine(cc.Add(200, 1111)); Console.ReadLine(); // Close the client. cc.Close(); } //</snippet15> catch (TimeoutException tex) { Console.WriteLine(tex.Message); cc.Abort(); } catch (CommunicationException cex) { Console.WriteLine(cex.Message); cc.Abort(); } finally { Console.WriteLine("Closed the client"); Console.ReadLine(); } }
static void Main(string[] args) { ClientBase<OneWayCalculatorClient>.CacheSetting = CacheSetting.Default; WSHttpBinding binding = new WSHttpBinding(); EndpointAddress epAddress = new EndpointAddress("http://localhost:8000/oneway/service"); OneWayCalculatorClient client = new OneWayCalculatorClient(binding,epAddress); // Call the Add service operation. double value1 = 100.00D; double value2 = 15.99D; client.Add(value1, value2); Console.WriteLine("Add({0},{1})", value1, value2); // Call the Subtract service operation. value1 = 145.00D; value2 = 76.54D; client.Subtract(value1, value2); Console.WriteLine("Subtract({0},{1})", value1, value2); // Call the Multiply service operation. value1 = 9.00D; value2 = 81.25D; client.Multiply(value1, value2); Console.WriteLine("Multiply({0},{1})", value1, value2); // Call the Divide service operation. value1 = 22.00D; value2 = 7.00D; client.Divide(value1, value2); Console.WriteLine("Divide({0},{1})", value1, value2); // Call the SayHello service operation string name = "World"; string response = client.SayHello(name); Console.WriteLine("SayHello([0])", name); Console.WriteLine("SayHello() returned: " + response); CalculatorClient client1 = new CalculatorClient(binding, epAddress); client1.AddAsync(1,2); client1.Add(1, 2); //Closing the client gracefully closes the connection and cleans up resources client.Close(); }
static void Main(string[] args) { var calculatorClient = new CalculatorClient(); var operation = args[0]; var number1 = int.Parse(args[1]); var number2 = int.Parse(args[2]); if (string.Equals(operation, "-Add", StringComparison.OrdinalIgnoreCase)) { var resultAdd = calculatorClient.Add(number1, number2); Console.WriteLine("resultAdd: {0}", resultAdd); } else if (string.Equals(operation, "-Subtract", StringComparison.OrdinalIgnoreCase)) { var resultSubtract = calculatorClient.Subtract(number1, number2); Console.WriteLine("resultSubtract: {0}", resultSubtract); } else if (string.Equals(operation, "-Multiply", StringComparison.OrdinalIgnoreCase)) { var resultMultiply = calculatorClient.Multiply(number1, number2); Console.WriteLine("resultMultiply: {0}", resultMultiply); } else if (string.Equals(operation, "-Divide", StringComparison.OrdinalIgnoreCase)) { var resultDivide = calculatorClient.Divide(number1, number2); Console.WriteLine("resultDivide: {0}", resultDivide); } else if (string.Equals(operation, "-PowerOf", StringComparison.OrdinalIgnoreCase)) { var resultPowerOf = calculatorClient.PowerOf(number1, number2); Console.WriteLine("resultPowerOf: {0}", resultPowerOf); } else { Console.WriteLine("Invalid operation entered"); throw new ArgumentException("Invalid operation entered"); } calculatorClient.Close(); // Console.WriteLine("Press <ENTER> to stop client"); // Console.ReadLine(); }
private void ClientRun() { // Create the binding. WSHttpBinding myBinding = new WSHttpBinding(); myBinding.Security.Mode = SecurityMode.Message; myBinding.Security.Message.ClientCredentialType = MessageCredentialType.Certificate; // Disable credential negotiation and the establishment of // a security context. myBinding.Security.Message.NegotiateServiceCredential = false; myBinding.Security.Message.EstablishSecurityContext = false; // Create the endpoint address. EndpointAddress ea = new EndpointAddress("http://localhost:90/Calculator"); // Create the client. CalculatorClient cc = new CalculatorClient(myBinding, ea); // Specify a certificate to use for authenticating the client. cc.ClientCredentials.ClientCertificate.SetCertificate( StoreLocation.CurrentUser, StoreName.My, X509FindType.FindBySubjectName, "Contoso.com"); // Specify a default certificate for the service. cc.ClientCredentials.ServiceCertificate.SetDefaultCertificate( StoreLocation.CurrentUser, StoreName.TrustedPeople, X509FindType.FindBySubjectName, "cohowinery.com"); // Begin using the client. cc.Open(); Console.WriteLine(cc.Add(200, 1111)); Console.ReadLine(); // Close the client. cc.Close(); }
// This method shows the correct way to clean up a client, including catching the // approprate Exceptions. static void DemonstrateCleanupWithExceptions() { // Create a client CalculatorClient client = new CalculatorClient(); try { // Demonstrate a successful client call. Console.WriteLine("Calling client.Add(0.0, 0.0);"); double addValue = client.Add(0.0, 0.0); Console.WriteLine(" client.Add(0.0, 0.0); returned {0}", addValue); // Demonstrate a failed client call. Console.WriteLine("Calling client.Divide(0.0, 0.0);"); double divideValue = client.Divide(0.0, 0.0); Console.WriteLine(" client.Divide(0.0, 0.0); returned {0}", divideValue); // Do a clean shutdown if everything works. In this sample we do not end up // here, but correct code should Close the client if everything was successful. Console.WriteLine("Closing the client"); client.Close(); } catch (CommunicationException e) { // Because the server suffered an internal server error, it rudely terminated // our connection, so we get a CommunicationException. Console.WriteLine("Got {0} from Divide.", e.GetType()); client.Abort(); } catch (TimeoutException e) { // In this sample we do not end up here, but correct code should catch // TimeoutException when calling a client. Console.WriteLine("Got {0} from Divide.", e.GetType()); client.Abort(); } catch (Exception e) { // In this sample we do not end up here. It is best practice to clean up the // client if some unexpected Exception occurs. Console.WriteLine("Got unexpected {0} from Divide, rethrowing.", e.GetType()); client.Abort(); throw; } }
static void DemonstrateTimeoutException() { // Create a client CalculatorClient client = new CalculatorClient(); try { // Call the Add service operation. double value1 = 100.00D; double value2 = 15.99D; double result = client.Add(value1, value2); Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result); // Set a ridiculously small timeout. This will cause the next call to // fail with a TimeoutException because it cannot process in time. Console.WriteLine("Set timeout too short for method to complete..."); client.InnerChannel.OperationTimeout = TimeSpan.FromMilliseconds(0.001); // Call the Divide service operation. value1 = 22.00D; value2 = 7.00D; result = client.Divide(value1, value2); Console.WriteLine("Divide({0},{1}) = {2}", value1, value2, result); // SHOULD NOT GET HERE -- Divide should throw // If we had gotten here, we would want to close the client gracefully so // that the channel closes gracefully and cleans up resources. client.Close(); Console.WriteLine("Service successfully returned all results."); } catch (TimeoutException exception) { // Control comes here when client.Divide throws a TimeoutException. Console.WriteLine("Got {0}", exception.GetType()); client.Abort(); } catch (CommunicationException exception) { Console.WriteLine("Got {0}", exception.GetType()); client.Abort(); } }
/// <summary> /// 测试调用 WCF 服务. /// </summary> /// <param name="endpointConfigurationName"></param> private static void CallWcfService(string endpointConfigurationName) { using (CalculatorClient service = new CalculatorClient(endpointConfigurationName)) { Console.WriteLine("调用 WCF 服务 ( {0} ) 开始!", endpointConfigurationName); Console.WriteLine("service.Add(1, 2) = {0}", service.Add(1, 2)); Console.WriteLine("service.Sub(2, 3) = {0}", service.Sub(2, 3)); Console.WriteLine("service.Mul(3, 4) = {0}", service.Mul(3, 4)); DivResult divResult = service.Div(5, 2); Console.WriteLine("service.Div(5, 2) = {0} 余 {1} ", divResult.DivData, divResult.ModData); Console.WriteLine("调用 WCF 服务 ( {0} ) 结束!", endpointConfigurationName); Console.WriteLine(); } }
private static void ExecuteOperation(CalculatorClient calculatorClient) { Console.WriteLine("Enter the first operand:"); double operand1 = GetNumberFromConsole(); Console.WriteLine("Enter the operation type:"); Operation operation = GetOperationFromConsole(); if (operation == Operation.InvalidOperation) { Console.WriteLine("Invalid operation"); return; } if (operation == Operation.Square) { FaultTest(() => calculatorClient.Sqr(operand1)); } else { Console.WriteLine("Enter the second operand:"); double operand2 = GetNumberFromConsole(); switch (operation) { case Operation.Add: FaultTest(() => calculatorClient.Add(operand1, operand2)); break; case Operation.Subtract: FaultTest(() => calculatorClient.Subtract(operand1, operand2)); break; case Operation.Multiply: FaultTest(() => calculatorClient.Multiply(operand1, operand2)); break; case Operation.Divide: FaultTest(() => calculatorClient.Divide(operand1, operand2)); break; } } }
private static void Main(string[] args) { Binding binding; Uri baseAddress; #if NETFRAMEWORK if (args.Length > 0 && args[0].Equals("WSHttpBinding", StringComparison.OrdinalIgnoreCase)) { binding = new WSHttpBinding(); baseAddress = new Uri($"http://localhost:{WcfPort}/{WcfNamespace}/CalculatorService"); } else #endif if (args.Length > 0 && args[0].Equals("BasicHttpBinding", StringComparison.OrdinalIgnoreCase)) { binding = new BasicHttpBinding(); baseAddress = new Uri($"http://localhost:{WcfPort}/{WcfNamespace}/CalculatorService"); } else if (args.Length > 0 && args[0].Equals("NetTcpBinding", StringComparison.OrdinalIgnoreCase)) { binding = new NetTcpBinding(); baseAddress = new Uri($"net.tcp://localhost:{WcfPort}/{WcfNamespace}/CalculatorService"); } else { #if NETFRAMEWORK throw new Exception("Binding type required: WSHttpBinding, BasicHttpBinding, or NetTcpBinding"); #else throw new ApplicationException("Binding type required: WSHttpBinding, BasicHttpBinding or NetTcpBinding"); #endif } var address = new EndpointAddress(baseAddress); using (var calculator = new CalculatorClient(binding, address)) { // Add the CustomEndpointBehavior / ClientMessageInspector to add headers on calls to the service calculator.ChannelFactory.Endpoint.EndpointBehaviors.Add(new CustomEndpointBehavior()); double result = calculator.Add(1, 2); Console.WriteLine($"Result: {result}"); } }
private static int RunAdd(AddVerb addVerb) { var client = new CalculatorClient(MainUrl); try { var result = client .Add(addVerb.TrackingId, addVerb.Numbers) .GetAwaiter() .GetResult(); ShowMessage($"Add operation result: {string.Join(" + ", addVerb.Numbers)} = {result}"); } catch (Exception ex) { ShowMessage(ex.Message, ConsoleColor.Red); } return(0); }
static void Main(string[] args) { try { // Create WCF client instance CalculatorClient client = new CalculatorClient(); // Call service operations double value1 = 100.00D; double value2 = 15.99D; double result = client.Add(value1, value2); Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result); value1 = 145.00D; value2 = 76.54D; result = client.Subtract(value1, value2); Console.WriteLine("Subtract({0},{1}) = {2}", value1, value2, result); value1 = 9.00D; value2 = 81.25D; result = client.Multiply(value1, value2); Console.WriteLine("Multiply({0},{1}) = {2}", value1, value2, result); value1 = 22.00D; value2 = 7.00D; result = client.Divide(value1, value2); Console.WriteLine("Divide({0},{1}) = {2}", value1, value2, result); // Close the client client.Close(); Console.WriteLine(); Console.WriteLine("Press <ENTER> to terminate client."); Console.ReadLine(); } catch (EndpointNotFoundException ex) { Console.WriteLine("Service endpoint not found: {0}", ex.Message); Console.ReadLine(); } }
static void Main(string[] args) { //Step 1: Create an instance of the WCF proxy. CalculatorClient client = new CalculatorClient(); // Step 2: Call the service operations. // Call the Add service operation. double value1 = 100.00D; double value2 = 15.99D; double result = client.Add(value1, value2); Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result); // Call the Subtract service operation. value1 = 145.00D; value2 = 76.54D; result = client.Subtract(value1, value2); Console.WriteLine("Subtract({0},{1}) = {2}", value1, value2, result); // Call the Multiply service operation. value1 = 9.00D; value2 = 81.25D; result = client.Multiply(value1, value2); Console.WriteLine("Multiply({0},{1}) = {2}", value1, value2, result); // Call the Divide service operation. value1 = 22.00D; value2 = 7.00D; result = client.Divide(value1, value2); Console.WriteLine("Divide({0},{1}) = {2}", value1, value2, result); //Step 3: Closing the client gracefully closes the connection and cleans up resources. client.Close(); Console.WriteLine("Press <ENTER> to terminate."); Console.WriteLine(); Console.ReadLine(); }
private static void example(CalculatorClient client) { double a = 100; double b = 15.99d; double result = client.Add(a, b); Console.WriteLine(string.Format(opMsg, "Add", a, b, result)); a = 123; b = 45.45d; result = client.Subtract(a, b); Console.WriteLine(string.Format(opMsg, "Subtract", a, b, result)); a = 43; b = 12.4345d; result = client.Multiply(a, b); Console.WriteLine(string.Format(opMsg, "Multiply", a, b, result)); a = 22; b = 7; result = client.Divide(a, b); Console.WriteLine(string.Format(opMsg, "Divide", a, b, result)); }
static void Main() { // Create a client with given client endpoint configuration CalculatorClient client = new CalculatorClient(); // Call the Add service operation. double value1 = 100.00D; double value2 = 15.99D; double result = client.Add(value1, value2); Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result); // Call the Subtract service operation. value1 = 145.00D; value2 = 76.54D; result = client.Subtract(value1, value2); Console.WriteLine("Subtract({0},{1}) = {2}", value1, value2, result); // Call the Multiply service operation. value1 = 9.00D; value2 = 81.25D; result = client.Multiply(value1, value2); Console.WriteLine("Multiply({0},{1}) = {2}", value1, value2, result); // Call the Divide service operation. value1 = 22.00D; value2 = 7.00D; result = client.Divide(value1, value2); Console.WriteLine("Divide({0},{1}) = {2}", value1, value2, 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(); }
static void Main() { // This demonstrates the wrapper client, which matches the generated client // from the WCF service and is intended to make migrating client applications easier // Create a client CalculatorClient client = new CalculatorClient("https://localhost:5001"); // Call the Add service operation. double value1 = 100.00D; double value2 = 15.99D; double result = client.Add(value1, value2); Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result); // Call the Subtract service operation. value1 = 145.00D; value2 = 76.54D; result = client.Subtract(value1, value2); Console.WriteLine("Subtract({0},{1}) = {2}", value1, value2, result); // Call the Multiply service operation. value1 = 9.00D; value2 = 81.25D; result = client.Multiply(value1, value2); Console.WriteLine("Multiply({0},{1}) = {2}", value1, value2, result); // Call the Divide service operation. value1 = 22.00D; value2 = 7.00D; result = client.Divide(value1, value2); Console.WriteLine("Divide({0},{1}) = {2}", value1, value2, result); Console.WriteLine(); Console.WriteLine("Press <ENTER> to terminate client."); Console.ReadLine(); }
private void ClientRun() { // Create the binding. WSHttpBinding myBinding = new WSHttpBinding(); myBinding.Security.Mode = SecurityMode.Transport; myBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Certificate; // Create the endpoint address. Note that the machine name must // must match the subject or DNS field of the X.509 certificate // used to authenticate the service. EndpointAddress ea = new EndpointAddress("https://localhost:8006/Calculator"); // Create the client. The code for the calculator // client is not shown here. See the sample applications // for examples of the calculator code. CalculatorClient cc = new CalculatorClient(myBinding, ea); // The client must specify a certificate trusted by the server. //<snippet1> cc.ClientCredentials.ClientCertificate.SetCertificate( StoreLocation.CurrentUser, StoreName.My, X509FindType.FindBySubjectName, "contoso.com"); //</snippet1> // Begin using the client. Console.WriteLine(cc.Add(100, 1111)); cc.Open(); // Close the client. cc.Close(); }
public static void ClientRun() { // Create the binding. WSHttpBinding myBinding = new WSHttpBinding(); myBinding.Security.Mode = SecurityMode.Transport; myBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic; // Create the endpoint address. Note that the machine name must // must match the subject or DNS field of the X.509 certificate // used to authenticate the service. EndpointAddress ea = new EndpointAddress("https://machineName:8006/Calculator"); // Create the client. The code for the calculator // client is not shown here. See the sample applications // for examples of the calculator code. CalculatorClient cc = new CalculatorClient(myBinding, ea); // The client must provide a username and password. The code // to return the username and password is not shown here. Use // a database to store the username and passwords, or use the // ASP.NET Membership provider database. cc.ClientCredentials.UserName.UserName = ReturnUsername(); cc.ClientCredentials.UserName.Password = ReturnPassword(); // Begin using the client. cc.Open(); Console.WriteLine(cc.Add(100, 11)); Console.ReadLine(); // Close the client. cc.Close(); }
static void Main(string[] args) { // Generated by wvf??? // An instance of the WCF proxy CalculatorClient calculator = new CalculatorClient(); while (true) { string[] input = Console.ReadLine().ToLower().Split(' '); double a, b; // Check if more than one // check first value for operation // check for subsequent values on operations switch (input[0]) { case "quit": case "exit": return; case "add": Result <bool> valid = validateEquasion(input, out a, out b); if (valid.Value) { Console.WriteLine(calculator.Add(a, b)); } else { switch (valid.Error) { case InputError.None: Console.WriteLine(woops); break; case InputError.InsufficientArugments: Console.WriteLine(string.Format(insufArgs, "Add", "2", "double")); break; case InputError.InvalidArguments: Console.WriteLine(string.Format(invalidArgs, "Add", "2", "double")); break; } } //Console.WriteLine(" = ", calculator.Add(, input[2])); break; case "addrest": Console.WriteLine(RestAdd(input[1], input[2])); break; default: break; } calculator.Close(); } }
public void Add_With_CommonDelimiter_Exception() { CalculatorClient calculatorClient = new CalculatorClient(); Assert.ThrowsException <FaultException>(() => calculatorClient.Add("1,\n2,3")); }
public void Add_With_CustomDelimiter_Exception() { CalculatorClient calculatorClient = new CalculatorClient(); Assert.ThrowsException <FaultException>(() => calculatorClient.Add("//;\n1;2;3$4")); }
protected void btnAdd_OnClick(object sender, EventArgs e) { ServiceReference3.ICalculator s = new CalculatorClient(); AddResult.Text = s.Add(int.Parse(Num1.Text), int.Parse(Num2.Text)).ToString(); }
private static void Main() { using (CalculatorClient client = new CalculatorClient()) { try { while (true) { Console.Write("> "); string command = Console.ReadLine(); string[] args = command ?.Split(' ') .ToArray(); if (args?.Any() != true) { Console.WriteLine("Unsupported arguments. Please try again"); continue; } switch (args[0].ToLowerInvariant()) { case "exit": return; case "": break; case "add": if (args.Length != 3 || !Double.TryParse(args[1], out double number1) || !Double.TryParse(args[2], out double number2)) { PrintUnsupportedArguments(); break; } Console.WriteLine(client.Add(number1, number2)); break; case "sub": if (args.Length != 3 || !Double.TryParse(args[1], out number1) || !Double.TryParse(args[2], out number2)) { PrintUnsupportedArguments(); break; } Console.WriteLine(client.Substract(number1, number2)); break; case "mul": if (args.Length != 3 || !Double.TryParse(args[1], out number1) || !Double.TryParse(args[2], out number2)) { PrintUnsupportedArguments(); break; } Console.WriteLine(client.Multiply(number1, number2)); break; case "div": if (args.Length != 3 || !Double.TryParse(args[1], out number1) || !Double.TryParse(args[2], out number2)) { PrintUnsupportedArguments(); break; } Console.WriteLine(client.Divide(number1, number2)); break; case "pow": if (args.Length != 2 || !Double.TryParse(args[1], out number1)) { PrintUnsupportedArguments(); break; } Console.WriteLine(client.Power(number1)); break; default: PrintUnsupportedArguments(); break; } } } catch (Exception e) { Console.WriteLine(e.InnerException?.Message ?? e.Message); } } }