Articles → WCF → Hosting WCF using console application

Hosting WCF using console application




















Software requirement





Prerequisite knowledge













Steps of execution













Create a WCF service





Click to Enlarge



using System.ServiceModel;

namespace WCFService
{
    [ServiceContract(Name = "MyService",Namespace="MyNamespace")]
    public interface IHello
    {
        [OperationContract(Name = "HelloMethod")]
        string SayHello(string name);
    }
}






namespace WCFService
{
    public class Hello :IHello
    {
        public string SayHello(string name)
        {
            return string.Format("Hello {0}", name);
        }
    }
}





Create a console application for hosting service







Click to Enlarge



using System;
using System.ServiceModel;

namespace HostingServer
{
    class Program
    {
        static void Main(string[] args)
        {
            ServiceHost host = new ServiceHost(typeof(WCFService.Hello));
            host.Open();
            Console.ReadLine();
            host.Close();
        }
    }
}




<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <services>
      <service name="WCFService.Hello" behaviorConfiguration="MyBehavior">
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:9876/serviceURL"/>
          </baseAddresses>
        </host>
        <endpoint address="" binding="basicHttpBinding" contract="WCFService.IHello"></endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"></endpoint>
      </service>
    </services>

    <behaviors>
      <serviceBehaviors>
        <behavior name="MyBehavior">
          <serviceMetadata/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
</system.serviceModel>
</configuration>





Create a console client application













Click to Enlarge








Click to Enlarge





using System;

namespace ClientApp
{
    class Program
    {
        static void Main(string[] args)
        {
            ServiceReference1.MyServiceClient proxy = new ServiceReference1.MyServiceClient();
            Console.WriteLine(proxy.HelloMethod("Karan"));
            Console.ReadLine();
        }
    }
}





Output





Click to Enlarge












Click to Enlarge


Posted By  -  Karan Gupta
 
Posted On  -  Thursday, July 18, 2013

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250