Using WCF Service in Windows Phone 8.1

Rate this post

In the previous post you know how to write and configure the WCF Service to be used on mobile applications. If you do not know how to use WCF Service let’s review this article:

In this article we will learn the last part about how to use WCF Service in Windows Phone 8.1.

Previously on Windows Phone 7, Windows Phone 8, Windows 8.1 the way to use WCF Service is relatively easy, you only need Add Service Reference then call generated functions that are defined in the WCF Service at code-behind. But Windows Phone 8.1 everything seems a little bit strange because there will be two different ways to build applications for Windows Phone that uses Silverlight core or Windows Runtime core. Now we’ll take a detailed look at how to use WCF Service in Windows Phone 8.1: example source code is reused from the previous post which is a WCF service has two functions GetMessage and PostMessage.

Using WCF Service on Windows Phone 8.1 Silverlight

First we will talk about how to use WCF Service in Windows Phone 8.1 Silverlight app. This approach can be applied on Windows Phone 7, Windows Phone 8, Windows 8.1 App or Desktop App (WinForms), Web App (ASP.NET, ASP.NET MVC …) too

  • Step 1: In the solution (created in previous article) we added blank Windows Phone 8.1 Silverlight app named WCFRESTDemo as shown below:

  • Step 2: Ctrl-F5 to run the WCF Service, the purpose of this step is to run WCF Service and we can find it by Add Service Reference in the next step

• Step 3: Right-click on References and select Add Service Reference. Then click Discover to find the service in the Solution. I’ll see WCFRestDemo service, named it is WCFDemo and click OK


• Step 4: After 3 steps above we have finished preparing WCF Service to use. Next, we need to design the interface of Windows Phone application to call the functions in this service. WCF Service only have two simple functions, so we will design UI have two buttons and rename App name and Page name on MainPage.xaml like below

• Step 5: On Designer double-click two buttons to add click event handler. Then from these handler we call GetMessage and PostMessage functions of WCF Service like this

At this point everything has completed, right-click Windows Phone Project and choose Set as Startup then press F5 to enjoy the fruits.

Click on the button GetMessage we will see a CommunicationException instead of message returned from WCF Service

This exception occur because in the previous post we have only one Endpoint configuration for WCFRestDemo service returns JSON (use webHttpBinding binding) so it’s not valid here. We need add another Endpoint use basicHttpBinding binding to return data as SOAP/XML to use in this case. Open web.config file in the project Tungnt.NET.WCFRestDemo and edit as below:

The last step is right-click WCFDemo Service Reference and update the service.

Finally F5 to run and now everything works like a charm.

Very simple and easy to implement, isn’t it?

Using WCF Service on Windows Phone 8.1 App Store / Universal App

We have just connect and use the WCF Service from Windows Phone 8.1 Silverlight app, everything is easy to implement because Visual Studio support all for you. But with Windows Phone 8.1 App Store/Universal App with core Windows Runtime, things become more complicated because it no longer supports using the Add Service Reference and directly call the WCF function again. We have to build and called WCF REST Service manually by the construction of HTTP GET/POST messages.

This approach is relatively new to those who have long been using the Add Service Reference method above. In my opinion seem that Microsoft has not complete this API yet because Windows 8.1 still supports Add Service Reference method but Windows Phone 8.1 does not. Everything might change when Windows 10 released but now we have only way is to use the method mentioned above. Let’s get started

  • Step 1: Add another Windows Phone 8.1 App Store project to the WCFRESTDemo solution as shown below

  • Step 2: Design the application interface similar to Windows Phone Silverlight apps but change the App name to “App Store Tungnt.net WP8.1″

  • Step 3: Similarly we handle two buttons click event handler to call the WCF Service function. We will use HttpClient, HttpRequestMessage and HttpResponseMessage classes (in System.Net.Http namespace) to send and receive data by REST.I will create a common function to call WCF REST Service named WCFRESTServiceCall. Note: URI is the URL of WCFRestDemo service + function name like PostMessage or GetMessage.

We’ve built Windows Phone 8.1 application to WCF REST Service, please F5 to run the test and everything work fine.

Conclude

In this article we explore about using WCF Service on Windows Phone application. Step by step we know how to use WCF Service on two types of application: Windows Phone 8.1 Silverlight and Windows Phone 8.1 App Store. Each way has pros and cons so you should consider using them in your application for your fit

  • Add Service Reference easy to implement but it cannot use for Windows Phone 8.1 App Store and performance not good as using WCF REST Service returns JSON
  • Create GET/POST message and use HttpClient, HttpRequestMessage, HttpResponseMessage to call WCF Service harder to implement and you need write more code but support on Windows Phone 8.1 App Store and better performance by using JSON

Hopefully article has been help you when building Windows Phone applications to use cloud server to store data.

If you have any questions or experience any please share in comments below the article and if you find it useful, please share with your friends again. If you want to receive notifications of new posts from my blog, please subscribe blog offline.

Happy coding. Stay tuned.

P/s: Example source code you can download here: WCFRESTDemoFinal

Comments

  1. says

    It’s not work for me. Only receive the 404 error, the WCF service works, because i test the url in the browser and i have answer, but by WP 8.1 device not. I try everything but i couldn’t to do it….

  2. Swaroop says

    I Have downloaded your code from code project and i am using VS-15 …….Can you Please tell me step by step that how to run your code…..Thank you in advance..

    • tungnt185 says

      Hi Swaroop,

      The first you need to start browse WCF Rest Service project then start Windows Phone project to connect and use this service.
      If you use emulator change from target device to emulator as well.

      Regards.

      • Swaroop says

        1. I am starting service WCFRestDemo.svc by keeping Tungnt.NET.WCFRestDemo as startup project so it shows your step-2 image.

        2. Now i am transfering my startup project to Tungnt.NET.WP8.1StoreApp and will run directly on device (i.e. is windows phone 8.1) as my system is cannot use other emmulators.

        3. When press GetMessage or PostMessage buttons on my windows phone then in output windows of visual studio i get an error of “Exception thrown: ‘System.Net.WebException’ in System.Net.ni.DLL” and another one is
        “The thread 0x79c has exited with code 0 (0x0).”

        Instead to have ” Welcome message…” pop-up…..i just have an pop-up having empty message.

        Please help to get through this error

Leave a Reply

Your email address will not be published. Required fields are marked *