Create WCF Service without Wizard

Basic Programming

This article gives you the clear picture of WCF implementation

Introduction

Windows Communication Foundation (WCF) is an SDK for developing and deploying services on Windows.

WCF provides a runtime environment for services, enabling you to expose CLR types as services, and to consume other services as CLR types.

Service: the functionality exposed to the world

Client: consuming the functionality from the service.

Main Components

  1. Endpoint
  2. Hosting environment
  3. Service class

The Endpoint in WCF Is ABC

  • A- Address (where)
  • B-Binding (How)
  • C-Contract

WCF supports following transport schemas

  • HTTP
  • TCP
  • Peer network
  • IPC (Inter-Process Communication over named pipes)
  • MSMQ

Contracts in WCF

  • Service Contract

o Service Contract (at class level)
o Operation Contract(atmethod level)

  • Data Contract

o Data contract (at class level)
o Data Member (at property level)

  • Fault Contract
  • Message Contract

Hosting of WCF

  1. IIS
  2. Self Hosting
  3. WAS (Windows Activation Service)
Implementation

STEPS FOR WCF IMPLEMENTATION
  • Console application
  • Add reference system.serviceModel.dll
  • Include the namespace System.ServiceModel;
    Server Side
  • create a service contract
    1. [ServiceContract (Namespace=http://.......)] (this is for an interface)  
  • Create an Interface
    1. Public interface Icalc  
    2. {  
    3. …….  
    4. ….  
    5. }  
  • Declare an method of each operation with OperationContract attribute
    1. [ServiceContract (Namespace=http://.......)]  
    2. Public interface Icalc  
    3. {  
    4. [OperationContract]  
    5. Int add(int n1,int n2);  
    6. [OperationContract]  
    7. Int Sub(int n1,int n2);  
    8. }  
  • Create an another class which implements the interface
    1. Public class cal_op:ICalc  
    2. {  
    3. Public int add(int a,int b)  
    4. {  
    5. Return(a+b);  
    6. }  
    7. Public int sub(int a,int b)  
    8. {  
    9. Return(a-b);  
    10. }  
    In main program:

  • Config. The address
    1. Uri baseaddress=new uri(http://…..”);  
  • Create a service host
    1. ServiceHost selfhost=new ServiceHost(typeof(class_name),<object of uri >);  
    2. ServiceHost selfhost=new ServiceHost(typeof(cal_op),baseAddress);  
  • Add service end point
    1. Selfhost.AddServiceEndPoint(typeof(<interface name>),new WSHttpBinding(),”<service name>”);  
    2. Selfhost.AddServiceEndPoint(typeof(Icalc),new WSHttpBinding(),”calc_server”);  
  • Hosting with Metadata
    1. ServiceMetadataBehavior smb=new ServiceMetadataBehavior();  
    2. Smb.HttpGetEnabled=true;  
    3. Selfhost.Description.Behaviors.Add(smb);  
  • Then open the host
    1. <host_name>.open();  
    2. Selfhost.Open();  
    3.   
    4. …  
    5. …  
    6. Selfhost.Close();  
    7.   
    8.   
    9. In VS2008 CMD:  
    10. Svcutill.exe/language:cs/out:<filename.cs>/config http://……  
    11. Svcutill.exe/language:cs/out:client.cs>/config http://……  
  • Add that file to client solution
    Client side:

    Create an end point by using tat obj
    1. <servicename> obj=new <servicename>();  
    2. Console.WriteLine(“{0}”,obj.Add(10,5));