Tuesday, November 25, 2008

Getting Started WCF (3)[Creating your first WCF service in visual studio 2008]

In this Article I will explain how to create a simple WCF service (Employee Service) using visual studio 2008 using new WCF tools founded in visual studio 2208

a) We will start from scratch in this example and define a:

1- Data contract.

2- Service contact.

3- Implement the service.

b) Use new visual studio to host and test the service

please review first my posts Getting Started WCF (1) and  Getting Started WCF (2) [WCF Terms].
or Download these files :

Let’s start our Visual studio 2008 and create this service…

1- Click file menu and chose create new project

a. Chose c# project and WCF project Type from project types list.

b. Chose WCF service Library from templates list.

c. Enter your project name and location and press ok, it gives me a WCF simple implementation.

image

a. Form solution explorer delete IService1.cs and Service1.cs Files to create it from scratch.

image 

2- Go ahead in add a new class to this project called it Employee Class.

a. this will represent the employee data that will represent using this service and contain a few filed [ID – Name – Age ]

b. To make it a efficient WCF Data contract we need to add DataContract attribute.
image

c. To decide which of these fields will include in your data contact in this example I will include all of them.
image

3- Now back to our solution and create new class Called IEmployeeServices.

a. Actually will be an interface not a class within it create a few operation one for create new employee and get a list of employee and to delete a specific employee.

b. In order to add this as a WCF Service we need to add ServiceContract attribute and also to decide which of these operations will be in our service contract we need to add OperationContact attribute also in this example I will include all of them.
image

4- Now we need to implement our service.

a. Back to project explorer and add new class called EmployeeService (the actual implementation of our service) drive this class from IEmplyeeService.
image

b. Ok now I will implement our methods but before that I need to add ServicesBehavior attribute , this attribute allow me to configure different things in my service around how will be managed by WCF runtime behind the scene , one of the things can I configure here is the InstanceContectMode , here can specify instanceContectMode to be Single , which mean that WCF runtime only create single instance of our service in runtime ,it will be a singleton
image

c. Now adding our method implementation .it will be

[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]

public class EmployeeService : IEmployeeService

{

// To Save List of Employee just in memory .

List<Employee> EmployeeList = new List<Employee>();

/// <summary>

/// Add New Employee to the Employee List

/// </summary>

/// <param name="employee"></param>

public void AddEmployee(Employee employee)

{

// just for testing count emploee list to add new id for the added employee

employee.ID = EmployeeList.Count + 1;

EmployeeList.Add(employee);

}

/// <summary>

/// return List of Employees

/// </summary>

/// <returns></returns>

public List<Employee> GetEmplyees()

{

return EmployeeList;

}

/// <summary>

/// Delete a specific Employee From the List.

/// </summary>

/// <param name="employeeID"></param>

public void DeleteEmployee(int employeeID)

{

// use pradicate expression that defines the condition of the element to search for.

EmployeeList.Remove(EmployeeList.Find(e => e.ID.Equals(employeeID)));

}

}

5- Now we ready to host and test but before that.

a. We need to define how I will communicate with the service- then we need to define the configuration for service endpoint in the application configuration file.

b. Visual studio gives us a great way to configure it by r click on the app.config and chose Edit WCF Configuration.

image

c. It will open Microsoft Configuration Editor that show that we have services called service1.and endpoint need to configure.

image

d. Now change the service1 to be our service EmployeeService.

image image

e. For the endpoints also we need to configure the contract I will use in this end point IEmployeeService Contract.

 image

f. The other endpoint is the metadata exchange endpoint that useful for retrieve metadata from our service in runtime.

image

g. With these few changes in endpoint we have enough information about our service to host and test.

6- Now when we simply press f5 we will launch our service with WCF host application and will open WCF test client communicate with WCF service host application and expose the operation into a UI based on our service contract

image
image

Try to click on AddEmployee in IEmployeeService and add test data for the first employee and click invoke. Also go to GetEmployee and list your employees and Delete also.

For downloading the Article file click here :

Now we implement our first WCF Service hope this article be useful for u

2 comments:

  1. Thank You for Help.
    ReplyDelete
  2. This is a very good sample! Thank you, Walled.
    ReplyDelete