Exemple #1
0
        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();
        }
Exemple #2
0
        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();
        }
Exemple #3
0
        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();
        }
Exemple #4
0
        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();
        }
Exemple #5
0
        // 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;
            }
        }
Exemple #6
0
        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();
            }
        }
Exemple #7
0
        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();
            }
        }
Exemple #8
0
        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>
        }
Exemple #9
0
        static void Main()
        {
            {
                // <Snippet1>
                // Configure custom certificate validation.
                ClientCredentials creds = new ClientCredentials();
                creds.ServiceCertificate.Authentication.CertificateValidationMode
                    = X509CertificateValidationMode.Custom;
                creds.ServiceCertificate.Authentication.CustomCertificateValidator
                    = new MyCertificateValidator();
                // </Snippet1>
            }
            {
                // <Snippet2>
                ClientCredentials creds = new ClientCredentials();

                // Configure chain trust.

                creds.ServiceCertificate.Authentication.CertificateValidationMode =
                    X509CertificateValidationMode.ChainTrust;

                creds.ServiceCertificate.Authentication.RevocationMode =
                    X509RevocationMode.NoCheck;
                // </Snippet2>
            }
            {
                // <Snippet3>
                ClientCredentials creds = new ClientCredentials();

                // Configure chain trust.
                creds.ServiceCertificate.Authentication.CertificateValidationMode
                    = X509CertificateValidationMode.ChainTrust;
                creds.ServiceCertificate.Authentication.TrustedStoreLocation
                    = StoreLocation.LocalMachine;
                // </Snippet3>
            }

            {
                // <Snippet4>
                ClientCredentials clientCreds =
                    new ClientCredentials();
                Console.WriteLine(
                    clientCreds.ServiceCertificate.Authentication);
                // </Snippet4>
            }

            // Create a client with given client endpoint configuration
            CalculatorClient wcfClient = new CalculatorClient();

            try
            {
                // set new credentials
                wcfClient.ChannelFactory.Endpoint.Behaviors.Remove(typeof(ClientCredentials));
                wcfClient.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.
                 */
                wcfClient.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode =
                    X509CertificateValidationMode.PeerOrChainTrust;
                // <Snippet0>
                ClientCredentials creds = new ClientCredentials();
                // Configure peer trust.
                creds.ServiceCertificate.Authentication.CertificateValidationMode =
                    X509CertificateValidationMode.PeerTrust;

                // Configure chain trust.
                creds.ServiceCertificate.Authentication.CertificateValidationMode =
                    X509CertificateValidationMode.ChainTrust;

                // Configure custom certificate validation.
                creds.ServiceCertificate.Authentication.CertificateValidationMode =
                    X509CertificateValidationMode.Custom;
                creds.ServiceCertificate.Authentication.CustomCertificateValidator =
                    new MyCertificateValidator();
                // </Snippet0>
                // 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();
        }
Exemple #10
0
        static void Main()
        {
            // Create a wcfClient with Username endpoint configuration
            CalculatorClient wcfClient = new CalculatorClient("Username");

            try
            {
                wcfClient.ClientCredentials.UserName.UserName = "******";
                wcfClient.ClientCredentials.UserName.Password = "******";

                // Call the Add service operation.
                double value1 = 100.00D;
                double value2 = 15.99D;
                double result = proxy.Add(value1, value2);
                Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result);

                // Call the Subtract service operation.
                value1 = 145.00D;
                value2 = 76.54D;
                result = proxy.Subtract(value1, value2);
                Console.WriteLine("Subtract({0},{1}) = {2}", value1, value2, result);

                // Call the Multiply service operation.
                value1 = 9.00D;
                value2 = 81.25D;
                result = proxy.Multiply(value1, value2);
                Console.WriteLine("Multiply({0},{1}) = {2}", value1, value2, result);

                // Call the Divide service operation.
                value1 = 22.00D;
                value2 = 7.00D;
                result = proxy.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();
            }

            // Create a wcfClient with Certificate endpoint configuration
            CalculatorClient eptwcfClient = new CalculatorClient("Certificate");

            try
            {
                // <Snippet00>
                ChannelFactory <ISimpleChannel> cf =
                    new ChannelFactory <ISimpleChannel>();
                cf.Credentials.ClientCertificate.SetCertificate(
                    StoreLocation.CurrentUser, StoreName.My,
                    X509FindType.FindByThumbprint,
                    "37 28 05 09 22 81 07 08 a0 cd 2a af dd c3 83 cd c3 3b 8f 9d");
                cf.Credentials.ServiceCertificate.SetDefaultCertificate(
                    StoreLocation.CurrentUser,
                    StoreName.TrustedPeople,
                    X509FindType.FindByThumbprint,
                    "33 93 68 cc 7c 75 80 24 a2 80 9f 45 8c 81 fa 92 ad 5b 04 39");
                cf.Credentials.ServiceCertificate.Authentication.CertificateValidationMode
                    = X509CertificateValidationMode.PeerOrChainTrust;
                // </Snippet00>

                eptwcfClient.ClientCredentials.ClientCertificate.SetCertificate
                    (StoreLocation.CurrentUser,
                    StoreName.TrustedPeople,
                    X509FindType.FindBySubjectName, "test1");

                // Call the Add service operation.
                double value1 = 100.00D;
                double value2 = 15.99D;
                double result = proxy.Add(value1, value2);
                Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result);

                // Call the Subtract service operation.
                value1 = 145.00D;
                value2 = 76.54D;
                result = proxy.Subtract(value1, value2);
                Console.WriteLine("Subtract({0},{1}) = {2}", value1, value2, result);

                // Call the Multiply service operation.
                value1 = 9.00D;
                value2 = 81.25D;
                result = proxy.Multiply(value1, value2);
                Console.WriteLine("Multiply({0},{1}) = {2}", value1, value2, result);

                // Call the Divide service operation.
                value1 = 22.00D;
                value2 = 7.00D;
                result = proxy.Divide(value1, value2);
                Console.WriteLine("Divide({0},{1}) = {2}", value1, value2, result);
                eptwcfClient.Close();
            }
            catch (TimeoutException timeProblem)
            {
                Console.WriteLine("The service operation timed out. " + timeProblem.Message);
                Console.ReadLine();
                eptwcfClient.Abort();
            }
            catch (CommunicationException commProblem)
            {
                Console.WriteLine("There was a communication problem. " + commProblem.Message + commProblem.StackTrace);
                Console.ReadLine();
                eptwcfClient.Abort();
            }

            Console.WriteLine();
            Console.WriteLine("Press <ENTER> to terminate client.");
            Console.ReadLine();
        }