maui
maui copied to clipboard
Android emulator unable to call local web api via 10.0.2.2 via http or https - stuck after hours of trying
Description
Section: Connect to local web services from Android emulators and iOS simulators Looks and reads as well-written. However unfortunately after failing to gain success implementing the advice in the first portion of the document entitled "Local web services running over HTTP", I decided to instead deploy "Local web services running over HTTPS". The document is not clear that one should choose one or the other. I decided that was the case; this could be made more clear in the intro. I could not achieve success because on my web api, when I used Swagger to directly reach the http endpoint (via the http port), I received a "CORS" error with a lot of detail that was unhelpful. Since I have seen no other mention of CORS related to MAUI / Android / iOS reaching a local development web api, I chose to not go down the path of implementing "CORS". So I decided to implement: "Local web services running over HTTPS". I trusted the local certificate (it was already trusted - I'm not sure when that ever happened in the past.) I located the file in my Maui app that references "10.0.2.2" via a text search since your document doesn't give much direction on where to find that, and being a newbie to Maui, I didnt' know. in "Bypass the certificate security check", the description made sense. The sample code following: "The following example shows a class that configures the AndroidMessageHandler class on Android and the NSUrlSessionHandler class on iOS to trust localhost communication over HTTPS:" ... well.. I didn't know where I should put that, so I created a class file within the same namespace as where the httpClient is initialized.
namespace MTRMauiClient.DataServices
{
public class RestDataService : IRestDataService
{
private readonly HttpClient _httpClient;
private readonly string _baseAddress;
private readonly string _url;
private readonly JsonSerializerOptions _jsonSerializerOptions;
public RestDataService(HttpClient httpClient)
{
//HERE IS THE Sample code under paragraph:
//"The resulting HttpClientHandler object can then be passed as an argument to the HttpClient constructor for debug builds:"
#if DEBUG
HttpMessageHandler handler = new HttpsClientHandlerService();
HTTPClient client = new HttpClient(handler.GetPlatformMessageHandler());
#else
client = new HttpClient();
#endif
There is an error for line
HttpMessageHandler handler = new HttpsClientHandlerService();
indicating:
CS0029: Cannot implicitly convert type 'MTRMauiClient.DataServices.HttpsClientHandlerService' to 'System.Net.Http.HttpMessageHandler'
I clearly didn't copy/paste the sample code into the correct place.
Also, if the DI process supplies an httpClient parameter, why are we "NEWING UP" a new HttpClient ? This makes me question if I am putting this code in the correct place, because the info in the document wasn't sufficient for me to be confident in knowing where to place it.
I am completely stuck. Neither route has allowed me to successfully call my web api from the Maui app. After spending a few hours last night, and my entire day today fighting with this, I'd like to suggest that this is an exceedingly weak and wasteful section of this otherwise amazing platform. I will greatly appreciate any assistance. I don't know where to turn after this because I'm pretty much I've reviewed virtually every forum post on this topic.
Steps to Reproduce
Setup api project. Setup maui client project. Point maui client project to api project. Verify it works when running the Windows platform. Try to run it via Android; receive error.
Link to public reproduction project repository
x
Version with bug
6.0.400
Last version that worked well
6.0.400
Affected platforms
Android
Affected platform versions
android
Did you find any workaround?
no
Relevant log output
no
Hi @tradegear , can you have a look through https://github.com/dotnet/maui/discussions/8131, which has some info on how to connect from Android emulators to the host?
Hi @tradegear. We have added the "s/needs-info" label to this issue, which indicates that we have an open question for you before we can take further action. This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.
This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment. If it is closed, feel free to comment when you are able to provide the additional information and we will re-investigate.
Hi, I can no longer run my app in Android emulator for a different reason. Please see this issue post: https://github.com/dotnet/maui/issues/10249
Is there still an issue here?
Hi @tradegear. We have added the "s/needs-info" label to this issue, which indicates that we have an open question for you before we can take further action. This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.
Yes. I have intermittent issues with this. Sometimes "it just stops working". Today my normal wifi & internet connection is down due to power outage. First I did not try to connect to any network and I get the error: [monodroid-debug] Trying to initialize the debugger with options: --debugger-agent=transport=dt_socket,loglevel=0,address=10.0.2.2:51263,embedding=1 [mono] debugger-agent: Unable to connect to 10.0.2.2:51263 I am unable to debug. Hoping to workaround this issue, I connected to my iPhone wifi hotspot and now have internet connection. But I am receiving the same error. The emulator seems to care about the structure of the network provided by whatever device is the internet gateway, and its ability to reach a 10.0.2.x subnet (?), depending on how my dev PC connects to the internet (?). Am I dependent upon always having my standard internet connection where my standard wifi has me on a 10.x network, instead of the 172.x network that my phone wifi hotspot puts me on? Also, I specifically placed all web api's and sql db's on my dev PC just so that I can work without an internet connection. Should I expect to be disabled from developing unless I have an internet connection?
Hi @tradegear. We have added the "s/needs-repro" label to this issue, which indicates that we require steps and sample code to reproduce the issue before we can take further action. Please try to create a minimal sample project/solution or code samples which reproduce the issue, ideally as a GitHub repo that we can clone. See more details about creating repros here: https://github.com/dotnet/maui/blob/main/.github/repro.md
This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.
Hi.
I searched on "failed to connect to 10.0.2.2 from 172.20.20.20" and wow I found my own post.
I receive this error on an actual "local device", an Android 11 Moto G5.
part of "RestDataServices.cs"
public RestDataService(HttpClient httpClient)
{
#if WINDOWS
_httpClient = httpClient; //accept the one created by builder
#elif DEBUG
HttpsClientHandlerServiceForDebug handler = new(); // for debugging, must use a different handler; handler cannot be updated within an httpClient, so must re-instantiate new client every time ugly
_httpClient = new HttpClient(handler.GetPlatformMessageHandler());
#else
_httpClient = httpClient; //accept the one created by builder
#endif
#if DEBUG
_baseAddress = DeviceInfo.Platform == DevicePlatform.Android ? "https://10.0.2.2:7145" : "https://localhost:7145"; //do not use cleartext (5145), instead setup handler to accept cert
#else
_baseAddress = $"https://a-real-endpoint/";
#endif
...and the code that creates "GetPlatformMessageHandler", for android.
public class HttpsClientHandlerServiceForDebug
{
public HttpMessageHandler GetPlatformMessageHandler()
{
#if ANDROID
var handler = new CustomAndroidMessageHandler();
handler.ServerCertificateCustomValidationCallback = (message, cert, chain, errors) =>
{
if (cert != null && cert.Issuer.Equals("CN=localhost"))
return true;
return errors == System.Net.Security.SslPolicyErrors.None;
};
return handler;
I should say this works for "emulator android" debugging, but it does not work for USB-connected, developer mode "local device" debugging.
I will probably, as usual, post this problem, then go on to work on other items on this very large project, and then this will get triaged and I'll forget about it because I'll get lost in other concerns. Perhaps I'll just continue debugging android with local emulators, then debug an actual android phone when I push the web api up to the Azure endpoint; there doesn't seem to be a problem with hitting actual endpoints with real certificates.
This is a REAL SORE POINT in the whole experience. I've wasted quite a bit of time on this, as you can see from the lifespan of this posting.
I think I saw a guy (Versluis?) on Youtube get this working in about 2 minutes.
Hi @tradegear. We have added the "s/needs-repro" label to this issue, which indicates that we require steps and sample code to reproduce the issue before we can take further action. Please try to create a minimal sample project/solution or code samples which reproduce the issue, ideally as a GitHub repo that we can clone. See more details about creating repros here: https://github.com/dotnet/maui/blob/main/.github/repro.md
This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.
Hi, @tradegear - thanks for opening this issue and sharing your concerns. @jfversluis do you have any ideas here?
@tradegear What Android device and version are you having issues with vs. which Android emulator and version are you having success with? Also, please share a repro project so that we can investigate this further. Thank you
Hi @tradegear. We have added the "s/needs-info" label to this issue, which indicates that we have an open question for you before we can take further action. This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.
This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment. If it is closed, feel free to comment when you are able to provide the additional information and we will re-investigate.