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 WCF client object. CalculatorClient wcfClient = new CalculatorClient(); try { // Call the Add service operation. double value1 = 100.00D; double value2 = 15.99D; double result = wcfClient.Add(value1, value2); Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result); // Call the Subtract service operation. value1 = 145.00D; value2 = 76.54D; result = wcfClient.Subtract(value1, value2); Console.WriteLine("Subtract({0},{1}) = {2}", value1, value2, result); // Call the Multiply service operation. value1 = 9.00D; value2 = 81.25D; result = wcfClient.Multiply(value1, value2); Console.WriteLine("Multiply({0},{1}) = {2}", value1, value2, result); // Call the Divide service operation. value1 = 22.00D; value2 = 7.00D; result = wcfClient.Divide(value1, value2); Console.WriteLine("Divide({0},{1}) = {2}", value1, value2, result); // Closing the client gracefully closes the connection and cleans up resources. wcfClient.Close(); } catch (TimeoutException timeout) { Console.WriteLine(timeout.Message); Console.ReadLine(); wcfClient.Abort(); } catch (CommunicationException commProblem) { Console.WriteLine(commProblem.Message); Console.ReadLine(); wcfClient.Abort(); } Console.WriteLine(); Console.WriteLine("Press <ENTER> to terminate client."); Console.ReadLine(); }
static void Main() { // Create a wcfClient with the given client endpoint configuration. CalculatorClient wcfClient = new CalculatorClient(); try { // Call the Add service operation. double value1 = 100.00D; double value2 = 15.99D; double result = wcfClient.Add(value1, value2); Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result); // Call the Subtract service operation. value1 = 145.00D; value2 = 76.54D; result = wcfClient.Subtract(value1, value2); Console.WriteLine("Subtract({0},{1}) = {2}", value1, value2, result); // Call the Multiply service operation. value1 = 9.00D; value2 = 81.25D; result = wcfClient.Multiply(value1, value2); Console.WriteLine("Multiply({0},{1}) = {2}", value1, value2, result); // Call the Divide service operation. value1 = 22.00D; value2 = 7.00D; result = wcfClient.Divide(value1, value2); Console.WriteLine("Divide({0},{1}) = {2}", value1, value2, result); wcfClient.Close(); } catch (TimeoutException timeProblem) { Console.WriteLine("The service operation timed out. " + timeProblem.Message); Console.ReadLine(); wcfClient.Abort(); } catch (CommunicationException commProblem) { Console.WriteLine("There was a communication problem. " + commProblem.Message + commProblem.StackTrace); Console.ReadLine(); wcfClient.Abort(); } 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 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 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(); }
// This method shows the correct way to clean up a client, including catching the // approprate Exceptions. static void DemonstrateCleanupWithExceptions() { Console.WriteLine("="); Console.WriteLine("= Demonstrating cleanup with Exceptions."); Console.WriteLine("="); // 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 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(); }
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 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() { // Note that the ListenUri must be communicated out-of-band. // That is, the metadata exposed by the service does not publish // the ListenUri, and thus the svcutil-generated config doesn't // know about it. // On the client, use ClientViaBehavior to specify // the Uri where the server is listening. Uri via = new Uri("http://localhost/ServiceModelSamples/service.svc"); // Create a client to talk to the Calculator contract CalculatorClient calcClient = new CalculatorClient(); calcClient.ChannelFactory.Endpoint.Behaviors.Add( new ClientViaBehavior(via)); double value1 = 100.00D; double value2 = 15.99D; double result = calcClient.Add(value1, value2); Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result); calcClient.Close(); // Create a client to talk to the Echo contract that is located // at the same EndpointAddress and ListenUri as the calculator contract EchoClient echoClient = new EchoClient("WSHttpBinding_IEcho"); echoClient.ChannelFactory.Endpoint.Behaviors.Add( new ClientViaBehavior(via)); Console.WriteLine(echoClient.Echo("Hello!")); echoClient.Close(); // Create a client to talk to the Echo contract that is located // at a different EndpointAddress, but the same ListenUri EchoClient echoClient1 = new EchoClient("WSHttpBinding_IEcho1"); echoClient1.ChannelFactory.Endpoint.Behaviors.Add( new ClientViaBehavior(via)); Console.WriteLine(echoClient1.Echo("Hello!")); echoClient.Close(); Console.WriteLine(); Console.WriteLine("Press <ENTER> to terminate client application."); Console.ReadLine(); }
static void Main() { //Specify the binding to be used for the client. BasicHttpBinding binding = new BasicHttpBinding(); //Specify the address to be used for the client. EndpointAddress address = new EndpointAddress("http://localhost/servicemodelsamples/service.svc"); // Create a client that is configured with this address and binding. CalculatorClient client = new CalculatorClient(binding, address); // 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 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(); } }
static void DoWork() { // Create a client with given client endpoint configuration CalculatorClient client = new CalculatorClient(); Random rand = new Random(); while (!flag) { // Call the Add service operation. double value1 = (double)rand.Next(0, 5); double value2 = (double)rand.Next(0, 5); double result; switch (rand.Next(0, 4)) { case (0): IncrementCounter(); result = client.Add(value1, value2); Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result); break; case (1): result = client.Subtract(value1, value2); Console.WriteLine("Subtract({0},{1}) = {2}", value1, value2, result); break; case (2): result = client.Multiply(value1, value2); Console.WriteLine("Multiply({0},{1}) = {2}", value1, value2, result); break; case (3): result = client.Divide(value1, value2); Console.WriteLine("Divide({0},{1}) = {2}", value1, value2, result); break; } Thread.Sleep(500); } //Closing the client gracefully closes the connection and cleans up resources 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 = 0.00D; try { 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 (Exception ex) { Console.WriteLine(ex.ToString()); } Console.WriteLine(); Console.WriteLine("Press <ENTER> to terminate client."); Console.ReadLine(); }
static void Main() { Console.WriteLine("Press <ENTER> to terminate client once the output is displayed."); Console.WriteLine(); // Create a client CalculatorClient client = new CalculatorClient(); // <snippet5> // AddAsync double value1 = 100.00D; double value2 = 15.99D; client.AddCompleted += new EventHandler <AddCompletedEventArgs>(AddCallback); client.AddAsync(value1, value2); Console.WriteLine("Add({0},{1})", value1, value2); // </snippet5> // SubtractAsync value1 = 145.00D; value2 = 76.54D; client.SubtractCompleted += new EventHandler <SubtractCompletedEventArgs>(SubtractCallback); client.SubtractAsync(value1, value2); Console.WriteLine("Subtract({0},{1})", value1, value2); // Multiply value1 = 9.00D; value2 = 81.25D; double 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); Console.ReadLine(); //Closing the client gracefully closes the connection and cleans up resources client.Close(); }
static void Main() { // Create a client CalculatorClient client = new CalculatorClient(); // Create a transaction scope with the default isolation level of Serializable using (TransactionScope tx = new TransactionScope()) { Console.WriteLine("Starting transaction"); // Call the Add service operation. double value = 100.00D; Console.WriteLine(" Adding {0}, running total={1}", value, client.Add(value)); // Call the Subtract service operation. value = 45.00D; Console.WriteLine(" Subtracting {0}, running total={1}", value, client.Subtract(value)); // Call the Multiply service operation. value = 9.00D; Console.WriteLine(" Multiplying by {0}, running total={1}", value, client.Multiply(value)); // Call the Divide service operation. value = 15.00D; Console.WriteLine(" Dividing by {0}, running total={1}", value, client.Divide(value)); Console.WriteLine(" Completing transaction"); tx.Complete(); } Console.WriteLine("Transaction committed"); // Closing the client gracefully closes the connection and cleans up resources client.Close(); Console.WriteLine("Press <ENTER> to terminate client."); Console.ReadLine(); }
static void Main() { // WARNING: This code is only needed for test certificates such as those created by makecert. It is // not recommended for production code. PermissiveCertificatePolicy.Enact("CN=ServiceModelSamples-HTTPS-Server"); // 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() { // Create a client CalculatorClient client = new CalculatorClient(); Console.WriteLine("Press <ENTER> to terminate client once the output is displayed."); Console.WriteLine(); // BeginAdd double value1 = 100.00D; double value2 = 15.99D; IAsyncResult arAdd = client.BeginAdd(value1, value2, AddCallback, client); Console.WriteLine("Add({0},{1})", value1, value2); // BeginSubstract value1 = 145.00D; value2 = 76.54D; IAsyncResult arSubtract = client.BeginSubtract(value1, value2, SubtractCallback, client); Console.WriteLine("Subtract({0},{1})", value1, value2); // BeginMultiply value1 = 9.00D; value2 = 81.25D; IAsyncResult arMultiply = client.BeginMultiply(value1, value2, MultiplyCallback, client); Console.WriteLine("Multiply({0},{1})", value1, value2); // BeginDivide value1 = 22.00D; value2 = 7.00D; IAsyncResult arDivide = client.BeginDivide(value1, value2, DivideCallback, client); Console.WriteLine("Divide({0},{1})", value1, value2); Console.ReadLine(); //Closing the client gracefully closes the connection and cleans up resources client.Close(); }
static void Main(string[] args) { InstanceContext instance = new InstanceContext(new CalculatorCallbackHandler()); CalculatorClient client = new CalculatorClient(instance); Console.WriteLine("Calling AddTo"); client.AddTo(2); Console.WriteLine("Calling SubtractFrom"); client.SubtractFrom(1); Console.WriteLine("Calling MultiplyBy"); client.MultiplyBy(22); Console.WriteLine("Calling DivideBy"); client.DivideBy(7); Console.WriteLine("Calling GetResult"); client.GetResult(); Console.ReadLine(); //Closing the client gracefully closes the connection and cleans up resources client.Close(); }
static void Main(string[] args) { CalculatorClient client = new CalculatorClient(); //X509Certificate2 cert = new X509Certificate2("c:\\MyClientCert.pfx", "password", X509KeyStorageFlags.DefaultKeySet); //client.ClientCredentials.ClientCertificate.Certificate = cert; //client.ClientCredentials.UserName.UserName = "******"; //client.ClientCredentials.UserName.Password = "******"; 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() { // Create a client with given client endpoint configuration CalculatorClient client = new CalculatorClient(); // Call the GetCallerIdentity operation Console.WriteLine("IsCallerAnonymous returned: {0}", client.IsCallerAnonymous()); // 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(); }
public static void CallServiceCustomClientIdentity(string endpointName) { // Create a custom binding that sets a custom IdentityVerifier. Binding customSecurityBinding = CreateCustomSecurityBinding(); // Call the service with DNS identity, setting a custom EndpointIdentity that checks that the certificate // returned from the service contains an organization name of Contoso in the subject name; that is, O=Contoso. EndpointAddress serviceAddress = new EndpointAddress(new Uri("http://localhost:8003/servicemodelsamples/service/dnsidentity"), new OrgEndpointIdentity("O=Contoso")); //<snippet4> using (CalculatorClient client = new CalculatorClient(customSecurityBinding, serviceAddress)) { //</snippet4> client.ClientCredentials.ServiceCertificate.SetDefaultCertificate(StoreLocation.CurrentUser, StoreName.TrustedPeople, X509FindType.FindBySubjectDistinguishedName, "CN=identity.com, O=Contoso"); // Setting the certificateValidationMode to PeerOrChainTrust means that if the certificate // is in the user's 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; 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); Console.WriteLine(); // 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); } }
static void Main(string[] args) { // Create a client // Since multiple endpoints exist, an endpoint is chosen as the // constructor parameter. CalculatorClient client = new CalculatorClient("CalcServiceSEP1"); // 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(); }
// This method shows another problem with the "using" statement. // // The service Aborts the channel to indicate that the session failed. When that // happens, the client gets an Exception from Close. Because Dispose is the same as // Close, the client gets an Exception from Dispose as well. The close of the "using" // block results in a call to client.Dispose. Because the closing brace executes // regardless of whether an Exception occurred, Exceptions from Close can mask more // important Exceptions from inside the "using" block. static void DemonstrateProblemUsingCanThrowAndMask() { Console.WriteLine("="); Console.WriteLine("= Demonstrating problem: closing brace of using statement can mask other Exceptions."); Console.WriteLine("="); try { // Create a new client. using (CalculatorClient client = new CalculatorClient()) { // Call Divide and catch the associated Exception. This throws because the // server aborts the channel before returning a reply. try { client.Divide(0.0, 0.0); } catch (CommunicationException e) { Console.WriteLine("Got {0} from Divide.", e.GetType()); } throw new ObjectDisposedException("Hope this Exception wasn't important, because " + "it might be masked by the Close Exception."); // The following line calls Dispose on the client. Dispose and Close are the // same thing, and the Close is not successful because the server Aborted the // channel. This masks the ObjectDisposedException above so nobody outside the // "using" block sees it. } } catch (ObjectDisposedException) { Console.WriteLine("We do not come here because the ObjectDisposedException is masked."); } catch (CommunicationException e) { Console.WriteLine("Got {0}", e.GetType()); } }
public static void Main() { //<snippet20> CalculatorClient wcfClient = new CalculatorClient(); try { Console.WriteLine(wcfClient.Add(4, 6)); wcfClient.Close(); } catch (TimeoutException timeout) { // Handle the timeout exception. wcfClient.Abort(); } catch (CommunicationException commException) { // Handle the communication exception. wcfClient.Abort(); } //</snippet20> }
static void Main() { // Get all serviceEndpoints from the MEX endpoint of the service. ServiceEndpointCollection serviceEndpoints = MetadataResolver.Resolve(typeof(ICalculator), new EndpointAddress("http://localhost:8000/ServiceModelSamples/service/mex")); foreach (ServiceEndpoint endpoint in serviceEndpoints) { try { Console.WriteLine("Creating a client with connecting to endpoint address {0}.", endpoint.Address.ToString()); CalculatorClient client = new CalculatorClient(endpoint.Binding, endpoint.Address); // Add the InternetClientValidatorBehavior to the endpoint. // The behaviors are evaluated when the client creates a ChannelFactory. client.Endpoint.Behaviors.Add(new InternetClientValidatorBehavior()); client.ClientCredentials.ClientCertificate.SetCertificate(StoreLocation.CurrentUser, StoreName.My, X509FindType.FindBySubjectName, "client.com"); client.ClientCredentials.ServiceCertificate.SetDefaultCertificate(StoreLocation.CurrentUser, StoreName.TrustedPeople, X509FindType.FindBySubjectName, "localhost"); client.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = System.ServiceModel.Security.X509CertificateValidationMode.PeerOrChainTrust; // Call operations // All invalid enpoints will fail when this is called. DoCalculations(client); client.Close(); } catch (InvalidOperationException ex) { // This exception is thrown in InternetClientValidatorBehavior. Console.WriteLine(ex.Message); } Console.WriteLine(); } Console.WriteLine(); Console.WriteLine("Press <ENTER> to terminate client."); Console.ReadLine(); }
static void Main() { // Create a MetadataExchangeClient for retrieving metadata. EndpointAddress mexAddress = new EndpointAddress(ConfigurationManager.AppSettings["mexAddress"]); MetadataExchangeClient mexClient = new MetadataExchangeClient(mexAddress); // Retrieve the metadata for all endpoints using metadata exchange protocol (mex). MetadataSet metadataSet = mexClient.GetMetadata(); //Convert the metadata into endpoints WsdlImporter importer = new WsdlImporter(metadataSet); ServiceEndpointCollection endpoints = importer.ImportAllEndpoints(); CalculatorClient client = null; ContractDescription contract = ContractDescription.GetContract(typeof(ICalculator)); // Communicate with each endpoint that supports the ICalculator contract. foreach (ServiceEndpoint ep in endpoints) { if (ep.Contract.Namespace.Equals(contract.Namespace) && ep.Contract.Name.Equals(contract.Name)) { // Create a client using the endpoint address and binding. client = new CalculatorClient(ep.Binding, new EndpointAddress(ep.Address.Uri)); Console.WriteLine("Communicate with endpoint: "); Console.WriteLine(" AddressPath={0}", ep.Address.Uri.PathAndQuery); Console.WriteLine(" Binding={0}", ep.Binding.Name); // call operations DoCalculations(client); //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(); client.ClientCredentials.Windows.AllowedImpersonationLevel = TokenImpersonationLevel.Impersonation; // 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() { // Create a client // Specify CalculatorEndpoint1 through CalculatorEndpoint4 to try various endpoints. CalculatorClient client = new CalculatorClient("CalculatorEndpoint1"); // 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(); }