C#, .Net and Azure

Connecting to localhost from a Xamarin app

2020/01/18

Debugging a .Net Core web api by itself is super simple thanks to the overall great debugging capabilities of Visual Studio.

Debugging a Xamarin application by itself is easy as well: Either connect your phone and run the app from there or use an emulator. Both are natively supported in Visual Studio.

However once you need to test both an app and web api in conjunction it becomes trickier:

Either you deploy every change of the web app to a server and then connect to it that way (with remote debugging in Azure this is even allows debugging the app and web app at once).

Or you setup up a webserver (e.g. IIS) in the local network and connect to that from your android.

Both ways are slow & tedious but as it turns out, it is even possible to connect to localhost from a Xamarin app (although not that easy and obvious)!

I personally prefer to debug the UWP version whenever possible as the debug experience is far greater than the android equivalent (for both device and simulator). IMO it's even worth it to keep a UWP version around just for the debugging experience.

Connectiong UWP to localhost for debugging

In debug mode, UWP apps are automatically able to connect to http://localhost:<port>.

For https with self signed certificates you will get an error along the lines of "The certificate authority is invalid or incorrect" until you check "Shared User Certificates" capability in the AppX manifest.

You should then be able to connect to https://localhost:<port> without an error (assuming you have the self signed certificate stored in your OS certificate store).

Interestingly this validation seems to be cached for quite some time (perhaps even for the lifecycle of the self signed certificate) as I was able to uncheck the capability "Shared User Certificates" after a first successful connection and my app continued to successfully connect to localhost even days later.

Connecting Android emulator to localhost

The android emulator (running on HyperV) behaves the same way as your android device on your network: as a seperate networked device with its own IP.

As such "localhost" doesn't make sense from the emulator perspective and you instead need to use the IP of your local PC.

All you need to do in your .Net Core web api is to change:

https://localhost:<port>

to

192.168.0.50:\<port>

(or whatever your local computer IP is).

For .Net Core web apps this can be configured in Properties -> Debug -> Web Server Settings.

The web api will then listen for requests on the specific IP.

In your Xamarin app code you then also use that IP instead of the webserver/localhost address.

Because HyperV runs on the same PC you don't even need to open any ports. Debugging both a web api and Xamarin application should now work locally!

Connection android to localhost

You can use similar steps in case you prefer to debug on a device and not with the emulator.

Your android device needs to be connected to the same network as your PC for this to work.

You can follow the same steps as with the Android emulator above.

As a last step you then need to open the port on your local PC so the android device can connect to it.

tagged as Xamarin.Forms, Android and .Net Core