Step By Step To Create WCF Service, Consume WCF service

 

In this article we discuss about how to create simple WCF service with step by step explanation. 

Create ServiceContract:

Create test WCF service from Visual Studio name it as EmployeeService, Add ServiceContract to the Service to make it available to the client(Add ServiceContract to the class which you want to expose it to client) as shown below.

namespace EmployeeWcfService

{

    [ServiceContract]

    public interface IEmpoyeeService

    {

       

    }

}

 

namespace EmployeeWcfService

{

    public class EmpoyeeService : IEmpoyeeService

    {

       

    }

}

 

Create OperationContract:

Add method (name it as GetEmployees()) to that class which has declared with the ServiceContract. This method returns Employee details by connecting to DB. Declare OperationContract to the method to expose it to client as shown below.

namespace EmployeeWcfService

{

    [ServiceContract]

    public interface IEmpoyeeService

    {

        [OperationContract]

        DataTable GetEmployeeDetails(int iEmpId);

    }

}

 

namespace EmployeeWcfService

{

    public class EmpoyeeService : IEmpoyeeService

    {

        public DataTable GetEmployeeDetails(int value)

        {

            DataTable dtEmpoyee = new DataTable();

           

            //Call database to get the details

 

            return dtEmpoyee;

        }     

    }

}

 

Create DataContract (if any):

If you want to expose any user-defined data types to client declare those types with DataContract as shown below.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Runtime.Serialization;

using System.ServiceModel;

using System.ServiceModel.Web;

using System.Text;

using System.Data;

 

namespace EmployeeWcfService

{

    [ServiceContract]

    public interface IEmpoyeeService

    {

        [OperationContract]

        DataTable GetEmployeeDetails(int iEmpId);

 

        [OperationContract]

        EmpName GetEmpName(EmpName composite);

    }

 

    [DataContract]

    public class EmpName

    {

        string sFirstName = "Smith";

        string sSecondName = "John";

 

        [DataMember]

        public string FirstName

        {

            get { return sFirstName; }

            set { sFirstName = value; }

        }

 

        [DataMember]

        public string SecondName

        {

            get { return sSecondName; }

            set { sSecondName = value; }

        }

    }

}

 

 

Create endpoint:

Create endpoint in the web.config file to make the service available for the client with proper Address, Banding and Contract as shown below.

<services>     

      <servicename="EmployeeWcfService.EmpoyeeService">

        <endpointaddress=""contract="EmployeeWcfService.IEmpoyeeService"binding="basicHttpBinding"/>       

      </service>

    </services>

 

 Test the Service:

Now runs the service by pressing F5 and check whether your method which has OperationContract is displaying in Browser or not. If it is displaying the browser window, the service is ready to consumed by the client if not check whether contract is defined properly or not for Class and Method.

Invoke WCF service from Client:

Once the WCF service is ready to consume by the client, call the service from the client application by right-click on Solution explorer and select Add Service Reference.

In client application you can call the method which has OperationContract by creating the object for class which has ServiceContract as shown below,

ServiceReference1.EmpoyeeServiceClient objEmployee = new ServiceReference1.EmpoyeeServiceClient();

            DataTable objEmployee = obj.GetEmployeeDetails(1);