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

namespace HostService
{
    /// <summary>
    /// STEP 2 : Self -  Hosting the Service.
    /// 
    /// (HostService)
    /// 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) Create a service host by binding the service type and base address to it.
    ///  4) Add the Service End points and Metadata End points to the Service host.
    ///  5) Open the service host, to listen for client connections.
    /// 
    /// </summary>
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                //Create a base address.
                Uri baseaddress = new Uri("http://localhost:8080");

                //create a service host by binding the service type and base address.
                ServiceHost srvHost = new ServiceHost(typeof(WCFService.FrtWCFService), baseaddress);
                
                //Add the behaviour type to the service host
                ServiceMetadataBehavior behaviour = new ServiceMetadataBehavior();
                behaviour.HttpGetEnabled = true;
                srvHost.Description.Behaviors.Add(behaviour);


                //Add the Service End points & Meta data End points to the service host.
                BasicHttpBinding httpBinding = new BasicHttpBinding();
                srvHost.AddServiceEndpoint(typeof(WCFService.IFirstWCFService), httpBinding, "MYFirstWCFService");
                srvHost.AddServiceEndpoint(typeof(IMetadataExchange), httpBinding, "MEX");

                
                //Open the service host to listen for client connections...
                srvHost.Open();

                // Disply the service address...on console...
                Console.WriteLine(srvHost.Description.Endpoints[0].Address);
                Console.ReadKey();
             
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception : "+  ex.Message);
               
            }
      
        }
    }
}
