How to Create WCF Service



First create class Library project and name it as SERVICE_DRAGON. Then   add WCF service to this class library project and name it as Service_Winows.When add the wcf file it will create clas file and interface file and also added System.serviceModel assembly..in this file create Service_Winows and IService_Winows.

          Now we will create simple method to print name with hello wold in interface class and implement it in class file.

Ø  IService_Winows

namespace SERVICE_DRAGON
{
    [ServiceContract(Name ="DoService")]
    public interface IService_Winows
    {
        [OperationContract(Name ="GetName")]
        string  DoWork(string Name);
    }
}

Ø  Service_Winows

namespace SERVICE_DRAGON
{
    public class Service_Winows : IService_Winows
    {
        public string DoWork(string Name)
        {
            return Name;
        }
    }
}

Now we have to host this service, for doing this we can use following method.
ü  Using windows form
ü  Using windows service
ü  Using console application
ü  Using IIS
  
In this tutorial we will use console application to host service.

Create console application and name it as ServiceHostSer and add this code snippet to it

namespace ServiceHostSer
{
    class Program
    {
        static void Main(string[] args)
        {
            using (ServiceHost host = new ServiceHost(typeof(SERVICE_DRAGON.Service_Winows)))
            {
               
                host.Open();
                Console.WriteLine(System.DateTime.Now);
                Console.ReadLine();
            }
        }
    }
}


Then we have to configure endpoint to relevant communication media to send data. We can do this in console application app.conifg file.


<system.serviceModel>
    <services>
      <service name="SERVICE_DRAGON.Service_Winows" behaviorConfiguration="mexBehavior">
        <endpoint address="Create_WCF_Service" binding="basicHttpBinding" contract="SERVICE_DRAGON.IService_Winows">
        </endpoint>
     
        <endpoint address="Create_WCF_Service" binding="netTcpBinding" contract="SERVICE_DRAGON.IService_Winows">
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange">
        </endpoint>
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8083/"/>
            <add baseAddress="net.tcp://localhost:8080/"/>
          </baseAddresses>
        </host>
      </service>


    <behaviors>
      <serviceBehaviors>
        <behavior name="mexBehavior">
          <serviceMetadata httpGetEnabled="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

    

Comments

Popular Posts