question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add guide how to connect to any service hosted at localhost from real device connected through Visual Studio

See original GitHub issue

Description

For MAUI run on Windows, it is easy to access localhost, it’s simply localhost. For MAUI run on Android emulator, simply replace localhost with 10.0.2.2 This is from this guide: https://docs.microsoft.com/en-us/xamarin/cross-platform/deploy-test/connect-to-local-web-services and it works fine.

For SQL Server Express used from emulator there is a nice tutorial here which works (the most important part is to open TCP/IP connections in the server config) (however the new Microsoft.Sql.Data client does not work on Android or works only for newer Android (depending on the version)): https://www.youtube.com/watch?v=xNmIdFjXzl4

But for Android device which runs MAUI application attached via Visual Studio debugger? 10.0.2.2 doesn’t work.

How to please connect to a service hosted on localhost? Not only HTTP but also TDS or any other protocol.

There is no guide for Android device afaik. But testing must be done also on a real device!

Thank you.

Public API Changes

Add documentation on how to access localhost from Android device when running via Visual Studio (both Debug and Release).

Intended Use-Case

Simulating production environment with MAUI app,

Possible workaround

  • to connect localhost to Android device, if these are on the same network, a temporary private IP can be exposed by localhost and ports with applications open. However this is not a robust solution and router can frequently change the private IP addresses assigned to devices (having a static private IP address in a network is not guaranteed)

VS bug #1745020

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:2
  • Comments:14 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
Alexgooncommented, Feb 9, 2023

Does 10.0.2.2 still not work in an android app? this should work on any android emulator - regardless of framework. Maybe there was an issue with the runtime and general communication.

I think the point is that “10.0.2.2” doesn’t work when an Android device is used through USB debugging, not when an emulator is used. It seems that I have the same issue.

1reaction
Eiloncommented, Jun 28, 2022

@janseris how is the Android device connected to the development machine? Is it via USB, WiFi, or something else? I wonder if this might affect the guidance?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Connect to Local Web Services from iOS Simulators and ...
Applications running in the iOS simulator can connect to local HTTP web services via your machines IP address, or via the localhost hostname ......
Read more >
Connect Any Client, Anywhere to localhost with Visual Studio ...
Visual Studio Dev Tunnels now has a command-line tool that allows you to make your localhost accessible to the outside world.
Read more >
How to connect to localhost from VS Android Emulator
The way I am connecting to the localhost is typing http://169.254.80.80 in the browser of the Emulator. As per http://developer.android.com/ ...
Read more >
How to Run an Android App against a localhost API
Start your server at localhost and attach the debugger. Next, change the API endpoints in your Android code to http://10.0.2.2 . This reroutes ......
Read more >
Accessing localhost from Anywhere
Open that URL on any device with an internet connection and you'll be accessing your localhost from the Web! One of the more...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found