Building a service layer using WCF

In the last recipe, we learned how to call a simple WCF service from the client application. In this recipe let's create a MyTasks Service.

Getting ready

Create a new project using the WCF service template and follow the same steps as in the recipe Writing and consuming a simple web service to create the service.

How to do it...

In this recipe, we will first create the WCF service and then build the Data Contract and Service Contract. In the Data Contract, we will add all the Data members; in the Service Contract we will return a collection class. Once the service is built successfully, we create the Phone Client application to call the service when searching for a task name.

  1. Right-click on the project and click on Add new item. Select the WCF service template and name it MyTaskService.svc.
  2. Open the IMyTaskService.cs file and add the following code for class MyTask and IMyTaskService:
    namespace Recipe2_MyTasksService
    {
    [DataContract]
    public class MyTask
    {
    [DataMember]
    public string Name { get; set; }
    [DataMember]
    public string Notes { get; set; }
    [DataMember]
    public string Priority { get; set; }
    [DataMember]
    public DateTime DateDue { get; set; }
    [DataMember]
    public DateTime DateCreated { get; set; }
    }
    [ServiceContract]
    public interface IMyTaskService
    {
    [OperationContract]
    List<string> GetMyTaskList();
    [OperationContract]
    MyTask GetMyTask(string Name);
    }
    }
    
  3. Now let's add a collection class for MyTask. Right-click on the project and add a new class. Name this class MyTasks.cs. Open the file and add the following code:
    namespace Recipe2_MyTasksService
    {
    public class MyTasks : List<MyTask>
    {
    public MyTasks()
    {
    LoadSampleTasks();
    }
    public List<string> GetMyTaskList()
    {
    var lst = this.Select(t => new { t.Name });
    return lst as List<string>;
    }
    public MyTask GetMyTask(string name)
    {
    return this.Where(t => t.Name == name).First();
    }
    public void LoadSampleTasks()
    {
    MyTask myTask = new MyTask()
    {
    Name = "Task Name 1",
    Notes = "Task Note 1",
    Priority = "Low",
    DateDue = new DateTime(2011, 9, 1),
    DateCreated = DateTime.Now
    };
    this.Add(myTask);
    myTask = new MyTask()
    {
    Name = "Task Name 2",
    Notes = "Task Note 2",
    Priority = "Medium",
    DateDue = new DateTime(2011, 10, 1),
    DateCreated = DateTime.Now };
    this.Add(myTask);
    myTask = new MyTask()
    {
    Name = "Task Name 3",
    Notes = "Task Note 3",
    Priority = "High",
    DateDue = new DateTime(2011, 11, 1),
    DateCreated = DateTime.Now
    };
    this.Add(myTask);
    }
    }
    }
    
  4. Open the MyTaskService.svc.cs file and add two public methods GetMyTaskList and GetMyTask. GetMyTaskList returns the list of tasks and GetMyTask returns the name of the task, as follows:
    namespace Recipe2_MyTasksService
    {
    public class MyTaskService : IMyTaskService
    {
    public List<string> GetMyTaskList()
    {
    MyTasks mytaskList = new MyTasks();
    return mytaskList.GetMyTaskList();
    }
    public MyTask GetMyTask(string name)
    {
    MyTasks mytaskList = new MyTasks();
    return mytaskList.GetMyTask(name);
    }
    }
    }
    
  5. Press F5 and you should see the WCF Test Client as shown in the following screenshot. Click on the IMyTaskService item to see the two methods. You can easily test the service here by passing a task name to get the priority.
    How to do it...
  6. Now let's create a new project to consume the service we created. Name the project Recipe2_MyTaskClient. Add the service reference similar to the last recipe and type the namespace MyTaskServiceReference.
    How to do it...
  7. Now after adding the code similar to last recipe for the context object, run the application. You should see the following results when you type in the name under Task Name and click on Get the Priority.
    How to do it...

How it works...

In this recipe, we created a class to store all the information for MyTask.

Using the service model, we built service classes to expose the members. We then added the collection class to return MyTask as a list object to the Phone Client Application.

There's more...

In this recipe, we didn't use SQL Server for storing our data to keep it simple to understand. In the next recipe, we will use SQL Server as the backend database to store our tasks.

See also

Check the next recipe, which deals with the ADO.NET Entity Framework. Also, check the recipe Using LINQ to SQL for creating the service.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset