Пример #1
0
    public static void Main()
    {
        // Picks up configuration from the config file.
        SampleServiceClient wcfClient = new SampleServiceClient();

        try
        {
            // Making calls.
            Console.WriteLine("Enter the greeting to send: ");
            string greeting = Console.ReadLine();
            wcfClient.Open();
            Console.WriteLine("The service responded: " + wcfClient.SampleMethod(greeting));
            Console.WriteLine("The service responded: " + wcfClient.SampleMethod(greeting));
            Console.WriteLine("The service responded: " + wcfClient.SampleMethod(greeting));
            // Done with service.
            wcfClient.Close();
            Console.WriteLine("Done!");

            SampleServiceClient newclient = new SampleServiceClient();
            newclient.Open();
            Console.WriteLine("The service responded: " + newclient.SampleMethod(greeting));
            Console.WriteLine("The service responded: " + newclient.SampleMethod(greeting));
            Console.WriteLine("Press ENTER to exit:");
            Console.ReadLine();

            // Done with service.
            newclient.Close();

            ChannelFactory <ISampleServiceChannel> chFactory = new ChannelFactory <ISampleServiceChannel>("WSHttpBinding_ISampleService");
            ISampleServiceChannel clientChannel = chFactory.CreateChannel();
            clientChannel.Open();
            Console.Read();
            clientChannel.SampleMethod(greeting);
            clientChannel.SampleMethod(greeting);
            clientChannel.SampleMethod(greeting);
            clientChannel.Close();

            Console.WriteLine("Done!");
        }
        catch (TimeoutException timeProblem)
        {
            Console.WriteLine("The service operation timed out. " + timeProblem.Message);
            Console.Read();
        }
        catch (FaultException <SampleFault> fault)
        {
            Console.WriteLine("SampleFault fault occurred: {0}", fault.Detail.FaultMessage);
            Console.Read();
        }
        catch (CommunicationException commProblem)
        {
            Console.WriteLine("There was a communication problem. " + commProblem.Message);
            Console.Read();
        }
    }
Пример #2
0
    public static void Main()
    {
        // Picks up configuration from the config file.
        SampleServiceClient wcfClient = new SampleServiceClient();

        try
        {
            // Making calls.
            Console.WriteLine("Enter the greeting to send: ");
            string greeting = Console.ReadLine();
            Console.WriteLine("The service responded: " + wcfClient.SampleMethod(greeting));

            Console.WriteLine("Press ENTER to exit:");
            Console.ReadLine();

            // Done with service.
            wcfClient.Close();
            Console.WriteLine("Done!");
        }
        catch (TimeoutException timeProblem)
        {
            Console.WriteLine("The service operation timed out. " + timeProblem.Message);
            wcfClient.Abort();
            Console.Read();
        }
        catch (CommunicationException commProblem)
        {
            Console.WriteLine("There was a communication problem. " + commProblem.Message);
            wcfClient.Abort();
            Console.Read();
        }
    }
Пример #3
0
        static void Main(string[] args)
        {
            using (SampleServiceClient client = new SampleServiceClient())
            {
                var retorno = client.SampleMethod("Ricardo");
                Console.WriteLine(retorno); // Hell World
            };

            Console.ReadKey();
        }
Пример #4
0
    void Run()
    {
        // Picks up configuration from the config file.
        using (SampleServiceClient wcfClient = new SampleServiceClient())
        {
            try
            {
                // Make asynchronous call.
                Console.WriteLine("Enter the greeting to send asynchronously: ");
                string       greeting   = Console.ReadLine();
                IAsyncResult waitResult = wcfClient.BeginSampleMethod(greeting, new AsyncCallback(ProcessResponse), wcfClient);
                Console.ForegroundColor = ConsoleColor.Blue;
                Console.WriteLine("Sent asynchronous method. Waiting on the response.");
                waitResult.AsyncWaitHandle.WaitOne();
                Console.ResetColor();

                // Make synchronous call.
                Console.WriteLine("Enter the greeting to send synchronously: ");
                greeting = Console.ReadLine();
                Console.ForegroundColor = ConsoleColor.Blue;
                Console.Write("Response: ");
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(wcfClient.SampleMethod(greeting));
                Console.ResetColor();

                // Make synchronous call on asynchronous method.
                Console.WriteLine("Enter the greeting to send synchronously to async service operation: ");
                greeting = Console.ReadLine();
                Console.ForegroundColor = ConsoleColor.Blue;
                Console.Write("Response: ");
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(wcfClient.ServiceAsyncMethod(greeting));
                Console.ResetColor();

                Console.WriteLine("Press ENTER to exit:");
                Console.ReadLine();

                // Done with service.
                wcfClient.Close();
                Console.WriteLine("Done!");
            }
            catch (TimeoutException timeProblem)
            {
                Console.WriteLine("The service operation timed out. " + timeProblem.Message);
                Console.Read();
                wcfClient.Abort();
            }
            catch (CommunicationException commProblem)
            {
                Console.WriteLine("There was a communication problem. " + commProblem.Message);
                Console.Read();
                wcfClient.Abort();
            }
        }
    }
Пример #5
0
    public static void Main()
    {
        // Picks up configuration from the configuration file.
        SampleServiceClient wcfClient = new SampleServiceClient();

        try
        {
            // Making calls.
            Console.WriteLine("Enter the greeting to send: ");
            string greeting = Console.ReadLine();
            Console.WriteLine("The service responded: " + wcfClient.SampleMethod(greeting));
            Console.WriteLine("Press ENTER to exit:");
            Console.ReadLine();
        }
        catch (TimeoutException timeProblem)
        {
            Console.WriteLine("The service operation timed out. " + timeProblem.Message);
            wcfClient.Abort();
            Console.ReadLine();
        }
        // Catch the contractually specified SOAP fault raised here as an exception.
        catch (FaultException <GreetingFault> greetingFault)
        {
            Console.WriteLine(greetingFault.Detail.Message);
            Console.Read();
            wcfClient.Abort();
        }
        // Catch unrecognized faults. This handler receives exceptions thrown by WCF
        // services when ServiceDebugBehavior.IncludeExceptionDetailInFaults
        // is set to true.
        catch (FaultException faultEx)
        {
            Console.WriteLine("An unknown exception was received. "
                              + faultEx.Message
                              + faultEx.StackTrace
                              );
            Console.Read();
            wcfClient.Abort();
        }
        // Standard communication fault handler.
        catch (CommunicationException commProblem)
        {
            Console.WriteLine("There was a communication problem. " + commProblem.Message + commProblem.StackTrace);
            Console.Read();
            wcfClient.Abort();
        }
    }
Пример #6
0
        static void Main(string[] args)
        {
            SampleServiceClient client = new SampleServiceClient();

            try
            {
                for (int i = 0; i < 10; i++)
                {
                    // This will post (In the service):

                    // "Hey! Test #x" until an exception happens or it goes 10 times without an exception.
                    string test = client.SampleMethod("Test #" + i);

                    Console.WriteLine(test);
                }
            }
            catch (TimeoutException timeProblem)
            {
                Console.WriteLine("The service operation timed out. " + timeProblem.Message);
                Console.ReadLine();
                client.Abort();
            }
            catch (FaultException<GreetingFault> greetingFault)
            {
                Console.WriteLine(greetingFault.Detail.Message);
                Console.ReadLine();
                client.Abort();
            }
            catch (FaultException unknownFault)
            {
                Console.WriteLine("An unknown exception was received. " + unknownFault.Message);
                Console.ReadLine();
                client.Abort();
            }
            catch (CommunicationException commProblem)
            {
                Console.WriteLine("There was a communication problem. " + commProblem.Message + commProblem.StackTrace);
                Console.ReadLine();
                client.Abort();
            }
            finally
            {
                client.Close();
            }
        }
Пример #7
0
    public static void Main()
    {
        // Picks up configuration from the config file.
        SampleServiceClient wcfClient = new SampleServiceClient();

        try
        {
            // Add the client side behavior programmatically.
            wcfClient.Endpoint.Behaviors.Add(new EndpointBehaviorMessageInspector());

            // Making calls.
            Console.WriteLine("Enter the greeting to send: ");
            string greeting = Console.ReadLine();
            Console.WriteLine("The service responded: " + wcfClient.SampleMethod(greeting));

            Console.WriteLine("Press ENTER to exit:");
            Console.ReadLine();

            // Done with service.
            wcfClient.Close();
            Console.WriteLine("Done!");
        }
        catch (TimeoutException timeProblem)
        {
            Console.WriteLine("The service operation timed out. " + timeProblem.Message);
            Console.Read();
        }
        catch (FaultException <SampleFault> fault)
        {
            Console.WriteLine("SampleFault fault occurred: {0}", fault.Detail.FaultMessage);
            Console.Read();
        }
        catch (CommunicationException commProblem)
        {
            Console.WriteLine("There was a communication problem. " + commProblem.Message);
            Console.Read();
        }
    }
Пример #8
0
    public static void Main()
    {
        // Picks up configuration from the config file.
        using (SampleServiceClient wcfClient = new SampleServiceClient())
        {
            try
            {
                // Making calls.
                Console.Write("Enter the first name to send: ");
                string first = Console.ReadLine();
                Console.Write("Enter the last name to send: ");
                string last = Console.ReadLine();
                Console.Write("Enter a message: ");
                string msg = Console.ReadLine();

                OriginalPerson person = new OriginalPerson();
                person.lastName       = last;
                person.firstName      = first;
                person.Message        = msg;
                person.additionalData = "extra data the service does not have.";

                person.Blob = new ArgumentException();

                OriginalPerson replyPerson = wcfClient.SampleMethod(person);

                Console.WriteLine("The service responded: " + replyPerson.Message);
                Console.WriteLine("From \"{0}, {1}\".", replyPerson.firstName, replyPerson.lastName);

                if (replyPerson.additionalData == null)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("The return additional data is null");
                    Console.ResetColor();
                }
                else
                {
                    Console.WriteLine("Any extra data: {0}.", replyPerson.additionalData.ToString());
                }

                if (replyPerson.Blob != null)
                {
                    Console.WriteLine("And the added object is: " + replyPerson.Blob.ToString());
                }
                else
                {
                    Console.WriteLine("There is no added data in the extra object property.");
                }

                Console.WriteLine("Press ENTER to exit:");
                Console.ReadLine();

                // Done with service.
                wcfClient.Close();
                Console.WriteLine("Done!");
            }
            catch (TimeoutException timeProblem)
            {
                Console.WriteLine("The service operation timed out. " + timeProblem.Message);
            }
            catch (CommunicationException commProblem)
            {
                Console.WriteLine("There was a communication problem. " + commProblem.Message);
                Console.Read();
            }
        }
    }
Пример #9
0
    public static void Main()
    {
        try
        {
            SampleServiceClient wcfClient;
            // Client has an endpoint for metadata
            EndpointAddress metaAddress
                = new EndpointAddress(new Uri("http://localhost:8080/SampleService/mex"));
            EndpointAddress httpGetMetaAddress
                = new EndpointAddress(new Uri("http://localhost:8080/SampleService?wsdl"));

            // <snippet1>
            // Get the endpoints for such a service
            ServiceEndpointCollection endpoints = MetadataResolver.Resolve(typeof(SampleServiceClient), metaAddress);
            Console.WriteLine("Trying all available WS-Transfer metadata endpoints...");

            foreach (ServiceEndpoint point in endpoints)
            {
                if (point != null)
                {
                    // Create a new wcfClient using retrieved endpoints.
                    wcfClient = new SampleServiceClient(point.Binding, point.Address);
                    Console.WriteLine(
                        wcfClient.SampleMethod("Client used the "
                                               + point.Address.ToString()
                                               + " address.")
                        );
                    wcfClient.Close();
                }
            }
            // </snippet1>
            // <snippet2>
            // Get the endpoints for such a service using Http/Get request
            endpoints = MetadataResolver.Resolve(typeof(SampleServiceClient), httpGetMetaAddress.Uri, MetadataExchangeClientMode.HttpGet);
            Client.WriteParameters(endpoints);
            ISampleService serviceChannel;
            Console.WriteLine(
                "\r\nTrying all endpoints from HTTP/Get and with direct service channels...");

            foreach (ServiceEndpoint point in endpoints)
            {
                if (point != null)
                {
                    ChannelFactory <ISampleService> factory = new ChannelFactory <ISampleService>(point.Binding);
                    factory.Endpoint.Address = point.Address;
                    serviceChannel           = factory.CreateChannel();
                    Console.WriteLine("Client used the " + point.Address.ToString() + " address.");
                    Console.WriteLine(
                        serviceChannel.SampleMethod(
                            "Client used the " + point.Address.ToString() + " address."
                            )
                        );
                    factory.Close();
                }
            }
            // </snippet2>

            // <snippet3>
            // Get metadata documents.
            Console.WriteLine("URI of the metadata documents retreived:");
            MetadataExchangeClient metaTransfer
                = new MetadataExchangeClient(httpGetMetaAddress.Uri, MetadataExchangeClientMode.HttpGet);
            metaTransfer.ResolveMetadataReferences = true;
            MetadataSet otherDocs = metaTransfer.GetMetadata();
            foreach (MetadataSection doc in otherDocs.MetadataSections)
            {
                Console.WriteLine(doc.Dialect + " : " + doc.Identifier);
            }
            // </snippet3>

            Console.WriteLine("Press ENTER to exit...");
            Console.ReadLine();
        }
        catch (TimeoutException timeProblem)
        {
            Console.WriteLine("The service operation timed out. " + timeProblem.Message);
        }
        catch (FaultException unkException)
        {
            Console.WriteLine(unkException.Message);
            Console.ReadLine();
        }
        catch (CommunicationException commProblem)
        {
            Console.WriteLine("There was a communication problem. " + commProblem.Message);
        }
    }