using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;


namespace UsingWCFService
{
    /// <summary>
    /// STEP 3: Consuming the WCF service.
    /// 
    /// (Client_ConsumingWCFService)
    /// Open a new console project. Do the following steps:
    /// 
    /// 1) Add a reference "System.ServiceModel" to the project.
    /// 2) Add the namespace "System.ServiceModel" to the class file.
    /// 3) Copy the service interface that we are going to consume. (IFirstWCFService). 
    ///     This is used in creating the channel.  
    /// 4) Create the channel by using the channel factory. Make sure that new channel 
    ///    should be associated with the same binding type and end point address as the service host contains.
    /// 5) Consume the methods exposed in services by using the channel object.   
    /// 
    /// 
    /// Running the Application :
    /// 1) Need to run the service host.
    ///    Run the "WCFService.exe" file.
    ///    
    /// 2) Now run the client applictaion to consume the WCF Service.
    ///    Run the "Client_ConsumingWCFService.exe"
    /// 
    /// 
    /// 
    /// </summary>
    class Program
    {
        static void Main(string[] args)
        {
            // Create the channel.
            IFirstWCFService chnl = new ChannelFactory<IFirstWCFService>(new BasicHttpBinding(), new EndpointAddress("http://localhost:8080/MYFirstWCFService")).CreateChannel();
            
            // Consume the services by using the channel factory.
            int result;
            result=chnl.Add(2, 3);
            Console.WriteLine("Result of Add() : " + result.ToString());

            // Consume the services by using the channel factory.
            Console.WriteLine(chnl.Hello("First Program"));

            // Close the channel.
            ((System.ServiceModel.Channels.IChannel)chnl).Close();

            
            Console.ReadKey();
        }
    }



    /// <summary>
    /// This is used to create the channels.
    /// This interface is used while consuming the WCF services manually by using the channels.
    /// 
    /// </summary>
    [ServiceContract]
    public interface IFirstWCFService
    {
        [OperationContract]
        int Add(int x, int y);

        [OperationContract]
        string Hello(string strName);
    }


}
