Friday, April 4, 2014

Windows Phone 8 Emulator connecting to localhost...

Problem:

Well this getting the Windows Phone emulator to work with a web service running on localhost turned out to be more work then I thought.  There are a multitude of blogs and postings to walk you through the process. Each implementation is slightly different and my issues may be unique to my box.  That said I did want to document what caused me so many issues. 

Solution:

1 – Bind your application to your public IP address

Normally when you run an application in IIS Express, it’s only accessible on http://localhost:<port>.
In order to access it from another machine, it needs to be bound to your public IP address as well.
Open D:\Users\<YourName>\Documents\IISExpress\config\applicationhost.config and find your site.

<site name="Test.Web" id="2">   
<application path="/">       
<virtualDirectory path="/" physicalPath="C:\Users\Jeff\Test\Test.Web" />   
</application>   
<bindings>       
<binding protocol="http" bindingInformation="*:39142:localhost" />   
</bindings>
</site>

In <bindings>, add another row:
<binding protocol="http" bindingInformation="*:39142:169.254.80.80" /> (But with your IP, and port number, of course)

2 - Allow incoming connections

If you’re running Windows 7/8, pretty much all incoming connections are locked down, so you need to specifically allow incoming connections to your application. First, start an administrative command prompt. Second, run these commands, replacing 169.254.80.80:39142 with whatever IP and port you are using:

> netsh http add urlacl url=http://169.254.80.80:39142/ user=everyone
This just tells http.sys that it’s ok to talk to this url.

> netsh advfirewall firewall add rule name="IISExpressWeb" dir=in protocol=tcp localport=39142 profile=private remoteip=localsubnet action=allow
This adds a rule in the Windows Firewall, allowing incoming connections to port 39142 for computers on your local subnet.


Key learnings:

1.) Command prompt
2.) ipconfig
Results may vary:

Ethernet adapter vEthernet (Internal Ethernet Port Windows Phone Emulator Internal Switch):

   Connection-specific DNS Suffix  . :
   Link-local IPv6 Address . . . . . : fe80::c0fc:ebf2:755d:4a03%14
   IPv4 Address. . . . . . . . . . . : 169.254.80.80
   Subnet Mask . . . . . . . . . . . : 255.255.0.0
   Default Gateway . . . . . . . . . :

3.) Emulator IP is 169.254.80.80

4.)Cut and paste line to add a second binding row.
binding ip needs to be exactly what the IP for the emulator is:

bingingInformation="*:<port>:<ip address>"

Sample:
bindingInformation="*:39142:169.254.80.80"

5.)Allow incoming connections
Open cmd as admin
>netsh http add urlacl url=http://169.254.80.80:39142/ user=everyone

6.)Adds rule in the Windows Firewall, allowing incoming connections to port 39142 for computers on your local subnet.
 Open cmd as admin
>netsh advfirewall firewall add rule name="IISExpressWeb" dir=in protocol=tcp localport=39142 profile=private remoteip=localsubnet action=allow

Addendum:
 Debugging was failing to connect on my VS 2013 build.  I realized that I was having firewall issues.
I started by turning off all the firewalls to determine if this was the issue.  Once the firewall was down I was able to connect from the emulator to localhost.  I then modified the netsh command.

Change the profile to public on the firewall connection.

> netsh advfirewall firewall add rule name="IISExpressWeb" dir=in protocol=tcp localport=58938 profile=public remoteip=localsubnet action=allow



Research How tos:

MVC Sample

http://blog.anthonybaker.me/2013/06/how-to-connect-to-local-web-services.html

Microsoft has drafted a how to guide using a wcf web service
http://msdn.microsoft.com/en-us/library/windowsphone/develop/jj684580.aspx

Robert McMurray has a great guide on connecting the emulator to a Web API project in Visual Studio.
http://blogs.iis.net/robert_mcmurray/archive/2013/08/20/connecting-the-windows-phone-8-emulator-to-web-api-applications-on-a-local-computer.aspx

Johan's succint post gave the key steps in the process
http://johan.driessen.se/posts/Accessing-an-IIS-Express-site-from-a-remote-computer

Tip using fiddler
You can install fiddler and go through the instruction on how to configure fiddler for windows emulator.
This article worked just fine for me:http://blogs.msdn.com/b/wsdevsol/archive/2013/06/05/configure-the-windows-phone-8-emulator-to-work-with-fiddler.aspx
One the configuration is done, emulator will automatically use fiddler as a proxy server and you localhost should work just fine from emulator too.
Remember to start fiddler before starting the emulator to route the traffic, otherwise it wont work. You will also be able to monitor http/https requests made by windows emulator.

Source:
http://stackoverflow.com/questions/13149304/windows-phone-8-emulator-access-localhost

No comments:

Post a Comment