How To Build a WCF Service | WCF

, ,

1. Start visual studio and select a WCF service application template, Name the project as WcfService1, choose a location to save this project.



2. Delete the default service and contract files, Service1.svc and IService1.cs by default. Delete both the files.

3. Define a data contract for Employee as below

[DataContract]
public class Employee
{
   [DataMember]
   public int EmployeeId { get; set; }
   [DataMember]
   public string EmployeeName { get; set; }   
   [DataMember]
   public string Designation { get; set; }
}

4. Define a service contract for EmployeeService as below

[ServiceContract]
public interface IEmployeeService
{
   [OperationContract]
   Employee GetEmployees(int id);
}

5. Define actual service EmployeeService by inheriting IEmployeeService

[ServiceContract]
public class EmployeeService : IEmployeeService
{
   [OperationContract]
   Employee GetEmployees(int id)
   {
       return new Employee{ 
                  EmployeeId = id, 
                  EmployeeName = "my name", 
                  Designation = "Software Engineer" };
   }
}

6. Configure endpoint in web.config

  1. <div class="dropdown">
  2. <a class="dropdown-toggle" data-toggle="dropdown" href="#">Dropdown trigger</a>
  3. <ul class="dropdown-menu" role="menu" aria-labelledby="dLabel">
  4. ...
  5. </ul>
  6. </div>