Xamarin Forms App Calling Web API Pin
-
'm learning Xamaring Forms. I want to test connecting to a Web API. I have a an Asp.Net Framework Web API running and I can connect to to it. However, in my Xamarin Forms page, when I call it, the call to GetAsync never returns:
public class MainPageViewModel : _ViewModelBase
{
private string url = "https://localhost:44340/api/Test/GetAll";
public List Items { get; set; }
public MainPageViewModel()
{
GetData();
}
private async Task GetData()
{
try
{
HttpClient client = new HttpClient();
HttpResponseMessage response = await client.GetAsync(url); // <====== NEVER RETURNSif (response.IsSuccessStatusCode) { string content = await response.Content.ReadAsStringAsync(); Items = JsonConvert.DeserializeObject\>(content); } } catch (Exception e) { throw; } }
}
I can paste the URL into a browser and see the JSON results. If the WebAPI is NOT running the GetAsync throws. Anyone have any thoughts on this?
If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.
-
'm learning Xamaring Forms. I want to test connecting to a Web API. I have a an Asp.Net Framework Web API running and I can connect to to it. However, in my Xamarin Forms page, when I call it, the call to GetAsync never returns:
public class MainPageViewModel : _ViewModelBase
{
private string url = "https://localhost:44340/api/Test/GetAll";
public List Items { get; set; }
public MainPageViewModel()
{
GetData();
}
private async Task GetData()
{
try
{
HttpClient client = new HttpClient();
HttpResponseMessage response = await client.GetAsync(url); // <====== NEVER RETURNSif (response.IsSuccessStatusCode) { string content = await response.Content.ReadAsStringAsync(); Items = JsonConvert.DeserializeObject\>(content); } } catch (Exception e) { throw; } }
}
I can paste the URL into a browser and see the JSON results. If the WebAPI is NOT running the GetAsync throws. Anyone have any thoughts on this?
If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.
It sounds like you have a deadlock somewhere. Try adding
.ConfigureAwait(false)
to theGetAsync
call.private async Task GetData()
{
try
{
HttpClient client = new HttpClient();
HttpResponseMessage response = await client.GetAsync(url).ConfigureAwait(false);if (response.IsSuccessStatusCode) { string content = await response.Content.ReadAsStringAsync(); Items = JsonConvert.DeserializeObject<List<TestDataEntity>>(content); } } catch (Exception e) { throw; }
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
It sounds like you have a deadlock somewhere. Try adding
.ConfigureAwait(false)
to theGetAsync
call.private async Task GetData()
{
try
{
HttpClient client = new HttpClient();
HttpResponseMessage response = await client.GetAsync(url).ConfigureAwait(false);if (response.IsSuccessStatusCode) { string content = await response.Content.ReadAsStringAsync(); Items = JsonConvert.DeserializeObject<List<TestDataEntity>>(content); } } catch (Exception e) { throw; }
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
Well that helped, a bit. Again, I'm just trying to create a small, simple xamarin forms example that connects to a web api. I've read these: [Consume a RESTful Web Service - Xamarin | Microsoft Docs](https://docs.microsoft.com/en-us/xamarin/xamarin-forms/data-cloud/web-services/rest) [6 Steps to Consume ASP.Net Core Web API in Xamarin | Syncfusion Blogs](https://www.syncfusion.com/blogs/post/consume-asp-net-core-web-api-in-xamarin.aspx) Here's what I have now:
private async Task GetData()
{
//string url = $"https://localhost:44340/api/Test/GetAll";
string url = $"https://127.0.0.1:44340/api/Test/GetAll";
try
{
HttpClient client = new HttpClient();
HttpResponseMessage response = await client.GetAsync(url).ConfigureAwait(false);
if (response.IsSuccessStatusCode)
{
string content = await response.Content.ReadAsStringAsync();
Items = JsonConvert.DeserializeObject>(content);
}
}
catch (Exception e)
{
throw;
}
}My browser opens and shows the commented out url (https:/localhost:44340). Yet the call to GetAsync throws "{System.Net.WebException: Failed to connect to localhost/127.0.0.1:44340". When I try that URL I get the same exception.
If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.
-
Well that helped, a bit. Again, I'm just trying to create a small, simple xamarin forms example that connects to a web api. I've read these: [Consume a RESTful Web Service - Xamarin | Microsoft Docs](https://docs.microsoft.com/en-us/xamarin/xamarin-forms/data-cloud/web-services/rest) [6 Steps to Consume ASP.Net Core Web API in Xamarin | Syncfusion Blogs](https://www.syncfusion.com/blogs/post/consume-asp-net-core-web-api-in-xamarin.aspx) Here's what I have now:
private async Task GetData()
{
//string url = $"https://localhost:44340/api/Test/GetAll";
string url = $"https://127.0.0.1:44340/api/Test/GetAll";
try
{
HttpClient client = new HttpClient();
HttpResponseMessage response = await client.GetAsync(url).ConfigureAwait(false);
if (response.IsSuccessStatusCode)
{
string content = await response.Content.ReadAsStringAsync();
Items = JsonConvert.DeserializeObject>(content);
}
}
catch (Exception e)
{
throw;
}
}My browser opens and shows the commented out url (https:/localhost:44340). Yet the call to GetAsync throws "{System.Net.WebException: Failed to connect to localhost/127.0.0.1:44340". When I try that URL I get the same exception.
If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.
Is the WebAPI project running? That looks like an IIS Express URL, which means you'll need to run the API project from Visual Studio for the URL to work. Also, if you're using a self-signed certificate and you haven't added it to your local PC's trust store, you'll need some extra code to ignore the certificate error: c# - Allowing Untrusted SSL Certificates with HttpClient - Stack Overflow[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
Is the WebAPI project running? That looks like an IIS Express URL, which means you'll need to run the API project from Visual Studio for the URL to work. Also, if you're using a self-signed certificate and you haven't added it to your local PC's trust store, you'll need some extra code to ignore the certificate error: c# - Allowing Untrusted SSL Certificates with HttpClient - Stack Overflow[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
Yes, the API is running. In the browser I can type localhost:44340/api/test/GetAll and it returns json data from my repo. localhost:44340 is what VS gives me when I run the app. So I'm trying that same URL in the Android app. Yet the exception shows the other IP address
If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.
-
Yes, the API is running. In the browser I can type localhost:44340/api/test/GetAll and it returns json data from my repo. localhost:44340 is what VS gives me when I run the app. So I'm trying that same URL in the Android app. Yet the exception shows the other IP address
If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.
localhost
is an alias for127.0.0.1
, so it's resolving to the correct address. Did you add the self-signed certificate to the local computer's trusted certificates store?
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
localhost
is an alias for127.0.0.1
, so it's resolving to the correct address. Did you add the self-signed certificate to the local computer's trusted certificates store?
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
Richard Deeming wrote:
Did you add the self-signed certificate to the local computer's trusted certificates store?
What cert are you referring to?
If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.
-
Richard Deeming wrote:
Did you add the self-signed certificate to the local computer's trusted certificates store?
What cert are you referring to?
If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.
IIS Express issues a self-signed certificate to serve your application over HTTPS instead of HTTP. When you open the site in a browser, you should see a warning that the certificate is untrusted.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
IIS Express issues a self-signed certificate to serve your application over HTTPS instead of HTTP. When you open the site in a browser, you should see a warning that the certificate is untrusted.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
I'm running from VS. When I start the app Chrome opens and I see JSON returned from my DAL. But no cert warnings.
If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.
-
I'm running from VS. When I start the app Chrome opens and I see JSON returned from my DAL. But no cert warnings.
If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.
It sounds like you've added the certificate to Chrome's certificate store, or enabled
chrome://flags/#allow-insecure-localhost
to bypass the certificate warning.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer