Connecting Local Web Service (WCF) From Windows Phone 8 Emulator

I'm glad you came here to resolve web services issues in the Windows Phone 8 emulator. Let us discuss it. Previously in Windows Phone 7 in OS 7.1 a local web service was connected and consumed using http://localhost:port. But in Windows Phone 8 things have changed. The Windows Phone 8 emulator works as a separate device like another PC. We are well aware of the fact that localhost does not work on two separate machines. That is the basic reason why the Windows Phone 8 emulator generates an exception.

emulator generates exception

Now, the real part of my article starts here.

Prerequisites

  1. Create a firewall exception to allow World Wide Web Services (HTTP) using the firewall.

  2. From the Windows Start Screen, search for Turn Windows features on or off. Click to run the program.

  3. In the Windows Features dialog box, expand .NET Framework 4.5 Advanced Services, then expand WCF Services.

  4. Under WCF Services, check the box next to HTTP Activation. Click OK to install this feature.

    http activation

The following describes how to remove this exception and how to consume local web services.

Solution:

  • Move to the Start Screen and search for Windows Firewall. Click to run it.

  • When you have opened the Windows Firewall screen, click Advanced Settings.

  • On the Windows Firewall with Advanced Security screen, select Inbound Rules. Then click New Rule.

  • When you move to the Rule Type page of the New Inbound Rule Wizard, select Port. Then click Next.

  • When you navigate to the Protocols and Ports page, enter the port number that IIS Express is using in the Specific local ports field. Then click Next.

    You can find the port number in the Web page of the project properties in Visual Studio.

  • On the Action page, select Allow the connection. Then click Next.

  • On the Profile page, select Private, and if applicable, Domain. Do not select Public. Then click Next.

  • On the Name page, type a name for the rule, for example, Local web service for testing. Then click Finish.
Note: It is better to allow inbound rules for private and domain.

Everything goes fine here but the problem in Windows Phone 8 is this that consuming WCF is not so much of a charm in WP8.

Even if you do the entire procedure, you will still fail. Here is the solution to the problem.

Whenever you use your IP address you consume WCF. First register it.

CMD provides a native command that can be used for registering the URL for caching.
  • Run CMD as administrator and enter the following command:

  • Netsh http add urlacl url = http://<your ip> : <port>/ user = everyone.

  • This will result in “Reservation successfully added”.

If you want to read more on the Netsh command, here you go!

The next step is to deal with the bindings for “applicationhost.config”.

  • Open the “applicationhost.config” file in Notepad after searching for it from Start.

  • In the configuration file, find the site element for the web service, WebServiceForTesting. The XML elements in the configuration file have the following hierarchy:
    1. <configuration>  
    2.    <application Host>  
    3.       <sites>  
    4.          <site> … </site>  
    5.       </sites>  
    6.    </application Host>  
    7. </configuration>  
    8. ? Change binding here as follow  
    9. <bindings>  
    10.    <binding protocol="http" bindingInformation="*:60661:localhost" />  
    11.    <binding protocol="http" bindingInformation="*:60661:<ip address>" />  
    12. </bindings>  

Note: Use the respective port number instead of 60661.

  • Just replace the bindings with this code.

  • All done! Save the file.

  • Now run the web services from the web services WCF project. It will run on the localhost but in IIS.

    Express you'll see two bindings of web services.

    wcf service

  • If you will run http://<yourip>: 52335<your port>/ in a web explorer it will run as follow.

    web explorer

  • Now the next step is to add the service to a Windows Phone application.

  • Search it by http://<IP address>/Service1.svc.

  • You'll discover the web service and now your are ready to go.

    address

  • Add a button to the XAML of your Windows Phone app and add the following code to its handler.
    1. private void Button_Click_1(object sender, RoutedEventArgs e)  
    2. {  
    3.    int testValue = 7;  
    4.    ServiceReference1.Service1Client clientForTesting = new ServiceReference1.Service1Client();  
    5.    clientForTesting.GetDataCompleted += new EventHandler<ServiceReference1.GetDataCompletedEventArgs>(TestCallback);  
    6.    clientForTesting.GetDataAsync(testValue);  
    7. }  
  • Complete the TestCallBack method as follows:
    1. private void TestCallback(object sender, ServiceReference1.GetDataCompletedEventArgs e)  
    2. {  
    3.    MessageBox.Show( e.Result);  
    4. }  
  • You are good to go. Now run the WCF project and then run the Windows Phone 8 application.

    WCF project

  • The service will hit (breakpoint added).

  • By continuing you will get results as in the follow.

    get result

I hope you enjoyed this simple tutorial to run local web services on your Windows Phone.

Happy coding!

Helping Note:

  • Set inbounds of your private and domain to “allow”.

  • Change the IP if the service does not hit. (In case of more than one accesses to the network, if you see more than one IPs when configuring your IP address by the ipconfig command).
Inbound Error:

If you are getting an Inbound error then that is basically due to firewall settings. To resolve this error use the following procedure.
  1. Go to Windows Firewall and select Advanced Settings -> Windows Firewall properties.

  2. Domain Profile -> Inbound Connections -> Allow.

  3. Private Profile -> Inbound Connections -> Allow
Press OK

References:

Up Next
    Ebook Download
    View all
    Learn
    View all