8

I have a Xamarin app and localhost API. I'm trying to connect to the API from the app, but it could not be found. I edited my applicationhost.config file and I tried localhost, 10.0.2.2 and my IP address in the call, but it was not working.

So, how do I configure simple API access? (The API itself is working well)

Dan
  • 59,490
  • 13
  • 101
  • 110
Dönci Fekete
  • 115
  • 1
  • 2
  • 9
  • use the IP address. Test it using the browser on the device/emulator to rule out connectivity issues. Also check your firewall settings. – Jason Oct 03 '18 at 01:24
  • Thanks for your reply. Oke, but I tested it with my IP in emulator and It has not found yet. What do I check my firewall ? I do not know, what I should do. – Dönci Fekete Oct 03 '18 at 07:32

2 Answers2

17

There is simple workaround to connect Emulator & Simulator to localhost API.

Download third party software NgRok for Windows at any specific location of your PC. It just contains one executable file called ngrok.exe (you not need to install it).

Now execute your service on your preferred browser. Than follow below steps.

  • Open your ngrok.exe file path in command prompt

enter image description here

  • Keep your service url (take from your browser) with prefix ngrok http --host-header=rewrite and hit enter.

enter image description here

  • Now go to your browser where you have executed your service open new tab and type localhost:4040 than press enter -->Go to status there you will find your public url which you can use in your mobile application.

enter image description here

Note: Url structure should be like this public const string BaseUri = "https://8c56892f.ngrok.io/"; followed by / in last. Try not to append unnecessary /.

Benifits

  1. You can debug services from one Visual Studio to another.

  2. Your url will be active until you close command prompt.

  3. In browser or command prompt track your requests status like Ok, Not found etc.

For more information visit this https://www.c-sharpcorner.com/article/exposing-local-web-server-to-internet-using-ngrok/

Hope it help you.

R15
  • 13,982
  • 14
  • 97
  • 173
  • 3
    Oh man. Yes, It is working! Thanks a lot! ( I have never seen anything like this before) – Dönci Fekete Oct 03 '18 at 22:13
  • Even this is not helping me in flutter android and dot net server – neena Sep 03 '19 at 12:24
  • @neena I followed all steps and its working in Flutter, using IIS Express (API developed in .NET Core). – Paul Pérez Mar 22 '20 at 23:00
  • Nice tool - just keep in mind the free version only allows sessions of 2 hours each, and the url they generate changes every time, which is pretty annoying – Savage Mar 30 '21 at 18:44
1

That 10.0.2.2 address is used for the Google emulators. For the Microsoft emulator you want to use 169.254.80.80. If that doesn't work then it must mean you have the firewall on and it's blocking the traffic. That can be solved by adding a new incoming rule for the program or port.

  • Yes, It is true! I was debugging it. My android emulator use 10.0.2.2 adress. But, how to add a new incoming rule for the program or port ? – Dönci Fekete Oct 03 '18 at 07:34
  • @DönciFekete Turn on your local server (WAMP, XAMP), Open Emulator and type 169.254.80.80 in the chrome or any browser in emulator and check if the wamp/ xamp page is showing or not. if it is not showing tell the error code what it is showing. – Rizwan Ahmed Shivalli Oct 03 '18 at 09:11
  • if you are using latest wamp server then follow steps at this link https://www.betterhostreview.com/access-wamp-server-local-network.html – Rizwan Ahmed Shivalli Oct 03 '18 at 09:34
  • I use RESTFull web service like in this example https://developer.xamarin.com/samples/xamarin-forms/WebServices/TodoREST/. So, I do not follow your instructions. Do you have any recommendations? – Dönci Fekete Oct 03 '18 at 09:58
  • If you are using local server to perform add, delete, edit etc operations then please follow the above steps if you are using an online server then check if you have given Internet permissions to your app – Rizwan Ahmed Shivalli Oct 03 '18 at 10:07
  • Just what I needed, I wonder why Google doesn't just use 127.0.0.1? – Tyler Petrov Dec 23 '22 at 17:58