How to use WCF in Windows 7 Phone Applications?

When you are building a data driven Windows 7 Phone application, use a web service or WCF service comes as natural choice. If you have been using WCF for your Silverlight and ASP.Net or any other applications, you will find that use of WCF for Windows 7 Phone is similar to what you have been doing so far. It is easy to the extent that you can simply cut and paste the code from your existing Silverlight to Phone application and it will work like a charm. This holds true if you are not using any namespaces or APIs that are not supported in Windows 7 Phone SDK. Otherwise you are golden. In this post, I will show you how to use WCF in Windows 7 Phone applications.

I will be using project from my previous post Using Bing Map Silverlight Control. In that project, I used a WCF service to pull data from server and associated it with bing map silverlight control. I am going to develop the same application but for Windows 7 Phone. In this post I will just focus on using WCF in Windows 7 Phone.

Create Windows 7 Phone Project

I am assuming that you have installed all development tools needed for Windows 7 Phone application development. Create a new Windows 7 Phone project in Visual Studio 2010. Since I want to display list of users on main page of application, so I picked Windows Phone List Application project. This provides boiler plate code to quickly set up your pages to display list on main page and then a details page to display details when user clicks on an item in the list.

Add Service Reference

Yes, it is the same Add Service Reference menu items in your project that you have seen in Silverlight, ASP.Net or any other .Net application projects. You will follow the same procedure of adding reference to WCF service in Windows 7 Phone application.

Call WCF Service To Fetch Data

It is exactly the same asynchronous model that gets used in Silverlight application. Well that makes sense because Windows 7 Phone development uses Silverlight as well. I copied the code from Silverlight application and used it in Windows Phone application without any change and it worked. The following code snippet shows async call to WCF service to which I added reference to my project.


 public void GetUserConnectionData(int sinceLastId, int maxResults, 
    Action<IEnumerable<UserConnectionDataViewModel>, Exception> callBack)
 {
   _wsActivityMonitor.GetUserConnectionsCompleted += (s, e) =>
   {
    var userCallBack = e.UserState as 
    Action<IEnumerable<UserConnectionDataViewModel>, Exception>;
    if (null == userCallBack)
    {// Wrong data type.return;}
     // Check if we got an error from request.
     if (null != e.Error)
     {
       userCallBack(null, e.Error);
       return;
     }
     // Process results.
     var connectDataList = new List<UserConnectionDataViewModel>();

     foreach (var connectData in e.Result)
     {
       var ucViewModel = new UserConnectionDataViewModel(connectData);
       connectDataList.Add(ucViewModel);
     }

     userCallBack(connectDataList.AsEnumerable<UserConnectionDataViewModel>(), null);
    };

    _wsActivityMonitor.GetUserConnectionsAsync(sinceLastId, maxResults, callBack);
 }

If you will download code from my earlier post, you will find the code to be same. You can play with sample project to see how this all works and how Windows 7 Phone application is binding to data from WCF service.

comments powered by Disqus

Search

Social

Weather

7.4 °C / 45.3 °F

weather conditions Clear

Monthly Posts

Blog Tags