26

I am not able to use localhost with xamarin.android application. I have executed Web project & Service project(not mobile app) on Chrome Browser.

This is emulators executed project

The displayed port number I tried browsing in Android Studio emulators & Genymotion emulators by appending 10.0.2.2: & 10.0.3.2: respectively, I got result Bad Request-Invalid Hostname. Even I have tried Service project port number, both not working.

Web Project: ASP.NET project using Visual Studio 2017 & Having service project in the same solution.

R15
  • 13,982
  • 14
  • 97
  • 173
  • 2
    What have you done? You deleted your other post where i tried to help you for hours and just posted your badly worded post again. You are not supposed to behave in that way as you throw away my work too. And then we were not ready yet. Realy terrible. – greenapps Mar 15 '18 at 10:19
  • Preceding comments gone to chat section automatically. – R15 Mar 15 '18 at 10:26
  • Then sorry for accusing you. Sorry. Try to undo that as this makes no fun too. – greenapps Mar 15 '18 at 10:29
  • 1
    What do your bindings look like in IIS? – em_ Apr 23 '18 at 19:31
  • What do you mean by IIS binding, I am not getting. Are you asking about my service/site hosted on IIS or not? – R15 May 16 '18 at 10:02
  • 1
    looks like the answer is here: https://stackoverflow.com/a/15809698/7149454 – Nick Kovalsky Jun 22 '18 at 14:57

4 Answers4

53

Open your solution folder. You might have several projects inside 1 folder, you don't need project's folder but the root one. Inside you'd have .vs/config/applicationhost.config file, open it.

find <sites> inside you'll have sites that you are running, might have several, if you switch startup projects in one solution. The name will be corresponding to one displayed by IIS in taskbar when you right-click it's icon. The tag could be, for example, <site name="ServerApp" id="2">. Now you need to add a line to bind external connections to your machine ip, for example

      <site name="ServerApp" id="2">
...
        <bindings>
...
          <binding protocol="http" bindingInformation="*:60424:127.0.0.1" />
        </bindings>
      </site>

Set appropriate port number and protocol, what matters here is just map them to 127.0.0.1, not "localhost" then they will be available for android emulator via 10.0.2.2:60424 (the port number 60424 is just from the example above, use yours)

Nick Kovalsky
  • 5,378
  • 2
  • 23
  • 50
  • 3
    It took 5 hours of my life to lead me to this answer. Thank you! – mr5 Jan 24 '21 at 08:50
  • You, my friend, are a lifesaver. I agree with @mr5. – M. Azyoksul Apr 05 '21 at 15:39
  • 2
    one further suggestion will be just add another `binding` not replace the existing one – Moumit Jan 13 '22 at 07:31
  • You sir - are a complete legend! I have had to walk away from this issue multiple times for fear of trashing my workspace. I think this happend on the update to 2019 because it used to be fine without having to jiggle the config. I am off work tommorow and will enjoy the long weekend far more now thanks to you. Thank you! – AntDC Jul 21 '22 at 16:08
2

You can use our free extension Conveyor https://marketplace.visualstudio.com/items?itemName=vs-publisher-1448185.ConveyorbyKeyoti

If the IP that Conveyor gives you doesn't work, just substitute it with 10.0.2.2. Using Conveyor solves the problem of needing to make IIS Express bind to other hostnames. It also has some other features too.

Jim W
  • 4,866
  • 1
  • 27
  • 43
1

A shoutout to the Top answer for putting me on the right track. In my configuration I certainly needed to change the binding to 127.0.0.1 instead of localhost in the solution config file.

Since I'm using Rider, the config file was in a different location. Check it out:

enter image description here

Under run/debug configurations, click the link open in editor next to (a checked by default) Generate applicationhost.config. This opens up the config file for editing.

In addition to the above, I also had to update the applicationUrl property in that same project's launchsettings.json file to use the loopback IP instead of localhost:

enter image description here

Restarting the service (and afterward the client) should go without saying, but one can never be too careful.

MDMoore313
  • 3,233
  • 1
  • 23
  • 38
0

I have answered this on another stackoverflow question, using third party software ngrock

Check this Android emulator not connecting to localhost api

R15
  • 13,982
  • 14
  • 97
  • 173