Sunday, July 17, 2016

Accessing a VS2015 Express website on a Parallels VM from a Mac

I’ve got a Mac which is running a Windows 10 Parallels VM that I use for Microsoft Visual Studio 2015 development. I’m using the Express Edition of VS2015. If I code up a web application (maybe a web service or an ASP.NET Web API) and run it in the IDE, I can hit it from the browser on that machine at http://localhost:57292/api/test.

How can I consume this Windows 10 web service from my Mac?

First of all I need to know the IP address of my Win 10 machine. Fire up a “cmd" prompt and run “ipconfig”. In my case the "IPv4 Address” is 10.211.55.4

To avoid having to remember and type this address, let’s add it to the “hosts” file on the Mac. Do this from the Terminal on the Mac using “sudo vi /private/etc/hosts” and add in a new line such as:

10.211.55.4     win10

Sadly, if I fire up Safari on my Mac and try to browse to http://win10:57292/api/test or (http://10.211.55.4:57292/api/test), it won’t find the page. There are two reasons for this:

  1. By default, IIS Express only allows access to your Visual Studio websites via localhost, and
  2. The Windows 10 firewall will block incoming connections to this website.

Fortunately, both of these can be overcome thanks to the handy tips on this webpage. Read it for the details, but for my quick reference I need to do the following on the Windows 10 machine:

  1. Edit the {project}\.vs\config\applicationhost.config file and search for my website (using the port is easiest). Add the following binding (the localhost line will already be there - add the second line with the IP address): 

    <binding protocol="http" bindingInformation="*:57292:localhost" />

    <binding protocol="http" bindingInformation="*:57292:10.211.55.4" />

    NB.This step needs to be done for each new website.  

  2. Create a hole in the Windows 10 firewall using these two commands from a “Run as Administrator” cmd prompt:  

    >netsh http add urlacl url=http://10.211.55.4:57292/ user=everyone 

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

Now, firing up Safari on the Mac and browsing to http://win10:57292/api/test should work!

If you are getting a 503 error, it could be because IIS Express hasn’t restarted and picked up the new website location. Check in the Windows System Tray for the IIS Express icon and click on it to see when there IP address is listed under the application:

IIS express from system tray

 If it isn’t there, stop VS2015, exit IIS Express if still running (using the ‘Exit' option above), and restart your VS2015 application.

No comments: