1

I have a Xamarin Forms app (Android and iOS, not UWP) that hits a DotNetCore 2.1 API I have created (runs under http://localhost:5000). All this is being developed using Visual Studio 2017.

I am able to debug the Android side with the address http://10.0.2.2:5000 which translates to 'localhost' on the Android side of things. So I am able to hit my API just fine from the Android simulator.

The problem is on the iOS side.

The iOS simulator is really running on the Mac, and thusly is not able to see my Windows machine running the API.

How do I configure my Windows/Mac machines so I am able to hit my local API?

I am able to ping my Windows machine from the Mac, but that's about it at this point.

3
  • 1
    Assuming both machines are the same local network/subnet, in your code, use the local network address of your DotNetCore server (ie. your Windows Visual Studio host ip address (ipconfig it, or open the network properties to find the current ip address of the NIC that is on the same subnet as your macOS device, etc...) Then use that IP address to also bind your DotNetCore server during its startup so it is listening on that subset for inbound traffic requests Commented Mar 5, 2019 at 23:17
  • Open your Xamarin Form project in Visual Studio 2017 on Windows PC and pair to mac. See if you can pair to mac successfully. Commented Mar 5, 2019 at 23:39
  • Yes, I am pairing to the Mac just fine (I can build and debug in the emulator). I fixed it by using my machine's local IP and for incoming requests unblocking the port I was using (in this case 5000). Commented Mar 5, 2019 at 23:43

1 Answer 1

6

I have this solved and am posting what I did to help others. I am now able to hit my .net core 2.1 web api from both the Android emulator, iOS emulator, and actual devices.

1) Run ipconfig to get your local IP address.

enter image description here

2) I didn't do anything special with the Program class. You can see this is the default File/New Project templated code. I saw many suggestions to use a more explicit Kestral configuration. I am sure in some scenarios it would help, but in my case this was not necessary.

enter image description here

3) Configure Kestral to listen on that IP and port (vs. localhost).

enter image description here

4) Open the firewall to pass traffic on that port.

netsh advfirewall firewall add rule name="Http Port 5000" dir=in action=allow protocol=TCP localport=5000
Sign up to request clarification or add additional context in comments.

4 Comments

You can mark your answer so that more people can see it,
Not until tomorrow! But, yes, I will do that.
thanks a lot for this simple and yet very clear tip. Just got it working right now. Although I've defined the kestrel url by appsettings.json file. Worked like a charm.
Hi @JohnLivermore does this instructions work on a macbook ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.