Click here to Skip to main content
Click here to Skip to main content
Technical Blog

Tagged as

New Features in WCF 4.5 - Part 4

, 23 Apr 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
In this part of WCF Tutorial series, we are going to implement "Task based programming model" feature of Windows Communication Foundation v4.5.Asynchronous programming has undoubtedly overcome application performance issues and also make application more responsive but as a developer for many, this
In this part of WCF Tutorial series, we are going to implement "Task based programming model" feature of Windows Communication Foundation v4.5.

Asynchronous programming has undoubtedly overcome application performance issues and also make application more responsive but as a developer for many, this approach has always been a little complex.
In WCF 4.5, Microsoft introduced support for Task-based programming model based on TAP (Task-based Asynchronous Pattern) that is comparatively easy to implement, because most of the difficult work of developer is now moved to compiler. For a detailed reference to Asynchronous Programming in WCF 4.5, please follow here.

In this WCF Tutorial, we are going to see task-based asynchronous operations in action. Two important keywords "async" and "await" are used for task-based asynchronous programming.

Consider we have a WCF Service as follows:
 namespace SimpleTaskBasedWCFService
 {
    [ServiceContract]
    public interface IStudentService
    {
        [OperationContract]
        List<Student> GetStudents();
    }

    [DataContract]
    public class Student
    {

        [DataMember]
        public int StudnetId { get; set; }

        [DataMember]
        public string StudentName { get; set; }
    }
 }
 

Our WCF Service implementation is as below:

    public class StudentService : IStudentService
    {
        List<Student> students = null;
        public List<Student> GetStudents()
        {
            students = new List<Student> {
                new Student(){StudnetId=1, StudentName="Imran"},
                new Student(){StudnetId=2, StudentName="Nauman"},
                new Student(){StudnetId=3, StudentName="Salman"}
            };
            return students;
        }
    }
Now, let's compile and publish the WCF Service. So far things are same as we do normally for creating a WCF Service.

But if we have a client application and add a reference to this service using Visual Studio 2012 or higher with .NET 4.5, we will see the difference.
If we click on "Advanced..." button above, following screen will be displayed and we can find the option for "Generate task-based operations" as marked below.
WCF Service Reference

If we generate proxy with above selected option, we will see asynchronous method "GetStudentsAsync" as shown below:
WCF Service Proxy

The calling method from client will be using "async" and "await" keywords as displayed in below code:
protected async void btnGetStudents_Click(object sender, EventArgs e)
{
   lblMessage.Text = "Fetching students data....";
   MyStudentService.StudentServiceClient client = new MyStudentService.StudentServiceClient();

   var Result = await client.GetStudentsAsync();
   gvStudents.DataSource = Result;
   gvStudents.DataBind();

   lblMessage.Text = "Students data fetched successfully.";
}

As compared to traditional asynchronous approach, task-based asynchronous operation is much easier in WCF 4.5 especially due to following factors:
  • Proxy generated with Async automatically
  • client code is straight forward using async and await keywords instead of callback methods.
Hopefully, we will continue exploring more features of WCF 4.5 in later posts.

Previous <<< New Features in WCF 4.5 - Part 3

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Imran Abdul Ghani
Software Developer (Senior) Emaratech
United Arab Emirates United Arab Emirates
Imran Abdul Ghani has more than 10 years of experience in designing/developing enterprise level applications. He is Microsoft Certified Solution Developer for .NET(MCSD.NET) since 2005. You can reach his blogging at WCF Tutorials, Web Development, SharePoint for Dummies.
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150326.1 | Last Updated 23 Apr 2014
Article Copyright 2014 by Imran Abdul Ghani
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid