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.
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.
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.
MyTaskService.svc
. 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); } }
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); } } }
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); } } }
Recipe2_MyTaskClient
. Add the service reference similar to the last recipe and type the namespace MyTaskServiceReference
.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.
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.