Beispiel #1
0
        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();
        }
Beispiel #2
0
        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();
        }
Beispiel #3
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();
            }
        }
Beispiel #4
0
        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();
        }
Beispiel #5
0
        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();
        }
Beispiel #6
0
        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();
        }
Beispiel #7
0
        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();
        }
Beispiel #8
0
        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);
            }
        }
Beispiel #9
0
        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();
        }
Beispiel #10
0
        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();
        }
Beispiel #11
0
        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();
        }
Beispiel #12
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>
        }
Beispiel #13
0
        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();
        }
Beispiel #14
0
        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();
        }
Beispiel #15
0
        static void InvokeClient(string configurationName)
        {
            Console.WriteLine("Using {0} endpoint.", configurationName);

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

            // 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();
        }
Beispiel #16
0
        static void Main()
        {
            // 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();
        }
Beispiel #17
0
        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();
        }
Beispiel #18
0
        static void Main()
        {
            // Create a client with Username endpoint configuration
            CalculatorClient client1 = new CalculatorClient("Username");

            client1.ClientCredentials.UserName.UserName = "******";
            client1.ClientCredentials.UserName.Password = "******";

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

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

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

                // Call the Divide service operation.
                value1 = 22.00D;
                value2 = 7.00D;
                result = client1.Divide(value1, value2);
                Console.WriteLine("Divide({0},{1}) = {2}", value1, value2, result);
            }
            catch (Exception e)
            {
                Console.WriteLine("Call failed : {0}", e.Message);
            }

            client1.Close();

            // Create a client with Certificate endpoint configuration
            CalculatorClient client2 = new CalculatorClient("Certificate");

            client2.ClientCredentials.ClientCertificate.SetCertificate(StoreLocation.CurrentUser, StoreName.My, X509FindType.FindBySubjectName, "test1");

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

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

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

                // Call the Divide service operation.
                value1 = 22.00D;
                value2 = 7.00D;
                result = client2.Divide(value1, value2);
                Console.WriteLine("Divide({0},{1}) = {2}", value1, value2, result);
            }
            catch (Exception e)
            {
                Console.WriteLine("Call failed : {0}", e.Message);
            }

            client2.Close();

            Console.WriteLine();
            Console.WriteLine("Press <ENTER> to terminate client.");
            Console.ReadLine();
        }
Beispiel #19
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();
        }
Beispiel #20
0
        static void Main()
        {
            Console.WriteLine("Username authentication required.");
            Console.WriteLine("Provide a valid machine or domain account. [domain\\user]");
            Console.WriteLine("   Enter username:"******"   Enter password:"******"*");
            }

            Console.WriteLine();

            // 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();

            // Setup the UserName credential
            client.ClientCredentials.UserName.UserName = username;
            client.ClientCredentials.UserName.Password = password.ToString();

            // 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);

            client.Close();

            Console.WriteLine();
            Console.WriteLine("Press <ENTER> to terminate client.");
            Console.ReadLine();
        }
Beispiel #21
0
        static void Main()
        {
            Console.WriteLine("Username authentication required.");
            Console.WriteLine(" Please enter a valid domain email address:");
            string username = Console.ReadLine();

            Console.WriteLine("   Enter password:"******"";
            ConsoleKeyInfo info     = Console.ReadKey(true);

            while (info.Key != ConsoleKey.Enter)
            {
                if (info.Key != ConsoleKey.Backspace)
                {
                    password += info.KeyChar;
                    info      = Console.ReadKey(true);
                }
                else if (info.Key == ConsoleKey.Backspace)
                {
                    if (password != "")
                    {
                        password = password.Substring(0, password.Length - 1);
                    }
                    info = Console.ReadKey(true);
                }
            }

            for (int i = 0; i < password.Length; i++)
            {
                Console.Write("*");
            }

            Console.WriteLine();

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

            client.ClientCredentials.UserName.UserName = username;
            client.ClientCredentials.UserName.Password = password;

            /*
             * 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 = System.ServiceModel.Security.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();
        }
Beispiel #22
0
        static void Main()
        {
            // Create a client with given client endpoint configuration. Make calls as Alice.
            CalculatorClient client = new CalculatorClient();

            // Set credentials to Alice
            client.ClientCredentials.UserName.UserName = "******";
            client.ClientCredentials.UserName.Password = "******";

            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);

                // 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 (Exception e)
            {
                Console.WriteLine("Exception: {0}", e.Message);

                if (e.InnerException != null)
                {
                    Console.WriteLine("Inner Exception: {0}", e.InnerException.Message);
                }
            }

            // Create a client with given client endpoint configuration. Make calls as Bob.
            client = new CalculatorClient();

            try
            {
                // Set credentials to Bob
                client.ClientCredentials.UserName.UserName = "******";
                client.ClientCredentials.UserName.Password = "******";
                // 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 (Exception e)
            {
                Console.WriteLine("Exception: {0}", e.Message);

                if (e.InnerException != null)
                {
                    Console.WriteLine("Inner Exception: {0}", e.InnerException.Message);
                }
            }

            // Create a client with given client endpoint configuration. Make calls as Charlie.
            client = new CalculatorClient();

            try
            {
                // Set credentials to Charlie
                client.ClientCredentials.UserName.UserName = "******";
                client.ClientCredentials.UserName.Password = "******";
                // 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 (Exception e)
            {
                Console.WriteLine("Exception: {0}", e.Message);

                if (e.InnerException != null)
                {
                    Console.WriteLine("Inner Exception: {0}", e.InnerException.Message);
                }
            }


            Console.WriteLine();
            Console.WriteLine("Press <ENTER> to terminate client.");
            Console.ReadLine();
        }
Beispiel #23
0
        static void Main()
        {
            Console.WriteLine("Username authentication required.");
            Console.WriteLine("Provide a valid machine or domain account. [domain\\user]");
            Console.WriteLine("   Enter username:"******"   Enter password:"******"*");
            }

            Console.WriteLine();

            // Create a client
            CalculatorClient client = new CalculatorClient();

            // Configure client with valid machine or domain account (username,password)
            client.ClientCredentials.UserName.UserName = username;
            client.ClientCredentials.UserName.Password = password.ToString();

            // Call 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();
        }
Beispiel #24
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 = 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();
            }

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

            try
            {
                // <Snippet0>
                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;
                // </Snippet0>

                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 = eptwcfClient.Add(value1, value2);
                Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result);

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

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

                // Call the Divide service operation.
                value1 = 22.00D;
                value2 = 7.00D;
                result = eptwcfClient.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();
        }
Beispiel #25
0
        static void Main()
        {
            // Create a client using either wsat or oletx endpoint configurations
            CalculatorClient client = new CalculatorClient("WSAtomicTransaction_endpoint");

            // CalculatorClient client = new CalculatorClient("OleTransactions_endpoint");

            // Start a transaction scope
            using (TransactionScope tx =
                       new TransactionScope(TransactionScopeOption.RequiresNew))
            {
                Console.WriteLine("Starting transaction");

                // Call the Add service operation
                //  - generatedClient will flow the required active transaction
                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
                //  - generatedClient will flow the allowed active transaction
                value1 = 145.00D;
                value2 = 76.54D;
                result = client.Subtract(value1, value2);
                Console.WriteLine("  Subtract({0},{1}) = {2}", value1, value2, result);

                // Start a transaction scope that suppresses the current transaction
                using (TransactionScope txSuppress =
                           new TransactionScope(TransactionScopeOption.Suppress))
                {
                    // Call the Subtract service operation
                    //  - the active transaction is suppressed from the generatedClient
                    //    and no transaction will flow
                    value1 = 21.05D;
                    value2 = 42.16D;
                    result = client.Subtract(value1, value2);
                    Console.WriteLine("  Subtract({0},{1}) = {2}", value1, value2, result);

                    // Complete the suppressed scope
                    txSuppress.Complete();
                }

                // Call the Multiply service operation
                // - generatedClient will not flow the active transaction
                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.
                // - generatedClient will not flow the active transaction
                value1 = 22.00D;
                value2 = 7.00D;
                result = client.Divide(value1, value2);
                Console.WriteLine("  Divide({0},{1}) = {2}", value1, value2, result);

                // Complete the transaction scope
                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();
        }