Build a basic WCF Service and Call it with different endpoints

Problem Definition

Build a basic WCF Service, consume it in a client and demonstrate the WCF calls using different Http bindings:

  1. BasicHttpBinding
  2. WsHttpBinding
  3. WsDualHttpBinding

Service Contract


 [ServiceContract(ProtectionLevel = ProtectionLevel.None)]
 public interface IService1
 {
        [OperationContract]
        string GetData(int value);
    
 }

Service Contract Implementation


 public class Service1 : IService1
 {
        public string GetData(int value)
        {
            return string.Format("You entered: {0}", value);
        }
}

Service Model Configuration

Client Side Implementation


class Program
{
        static void Main(string[] args)
        {

            try
            {

                //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:8083/Service1.svc");
                // Create a client that is configured with this address and binding.
                Service1Client client = new Service1Client(binding, address);
                Console.WriteLine("************Basic****************");
                Console.WriteLine("Address: " + client.Endpoint.Address);
                Console.WriteLine("Binding: " + client.Endpoint.Binding);
                Console.WriteLine("Contract: " + client.Endpoint.Contract);
                Console.WriteLine(client.GetData(12345));
                Console.WriteLine("*********************************\n");


                //Specify the binding to be used for the client.
                WSHttpBinding wsBinding = new WSHttpBinding();
                //Specify the address to be used for the client.
                EndpointAddress secureAddress = new EndpointAddress("http://localhost:8083/Service1.svc/secure");

                // Create a client that is configured with this address and binding.
                Service1Client secureClient = new Service1Client(wsBinding, secureAddress);
                Console.WriteLine("************WS****************");
                Console.WriteLine("Address: " + secureClient.Endpoint.Address);
                Console.WriteLine("Binding: " + secureClient.Endpoint.Binding);
                Console.WriteLine("Contract: " + secureClient.Endpoint.Contract);

                Console.WriteLine(secureClient.GetData(891));
                Console.WriteLine("*********************************\n");

                //Specify the binding to be used for the client.
                WSDualHttpBinding wsDualBinding = new WSDualHttpBinding();
                //Specify the address to be used for the client.
                EndpointAddress secureDualAddress = new EndpointAddress("http://localhost:8083/Service1.svc/secureAndDual");

                // Create a client that is configured with this address and binding.
                Service1Client secureAndDualClient = new Service1Client(wsDualBinding, secureDualAddress);
                Console.WriteLine("************Dual****************");
                Console.WriteLine("Address: " + secureAndDualClient.Endpoint.Address);
                Console.WriteLine("Binding: " + secureAndDualClient.Endpoint.Binding);
                Console.WriteLine("Contract: " + secureAndDualClient.Endpoint.Contract);

                Console.WriteLine(secureAndDualClient.GetData(82899327));
                Console.WriteLine("*********************************\n");
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex);
            }

            Console.Read();
        }
}

Results


Comments

Popular posts from this blog

Azure - Manage Blob Storage - Part #7 - Add Metadata to an Existing Container using C#

Azure - Manage Blob Storage - Part #5 - Create Folder Structure and upload a file to folder using an Existing Container using C#

Algorithm - Breadth First Search(BFS) - Python(3)