I was playing around with web services with Xamarin Forms. I made a simple web service that's hosted on IIS Express (that comes with VS 2015). The Web service deals with employee information.
This worked fine initially with the Windows Emulator. But I had problems to get it to work with the Android emulator.Upon seeking help I got the following instructions from a person,
After following the instructions, not only did it not work in the android emulator, but the UWP app stopped working as well.
following is where it fails
public class RestClient<T>
{
private const string WebServiceUrl = "http://localhost:52792/api/Employees/";
//private const string WebServiceUrl = "http://services.groupkt.com/country/get/all/";
public async Task<List<T>> GetAsync()
{
var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.ExpectContinue = false;
var json = "";
try
{
**json = await httpClient.GetStringAsync(WebServiceUrl);
}
catch (HttpRequestException e)
{
var error = "";
error=e.StackTrace;
error = e.Source;
error = e.HelpLink;
error = e.ToString();
}
var taskModels = JsonConvert.DeserializeObject<List<T>>(json);
return taskModels;
}
And following is the error thrown,
System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> System.Runtime.InteropServices.COMException: The text associated with this error code could not be found.
A connection with the server could not be established
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Net.Http.HttpHandlerToFilter.<SendAsync>d__4.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Net.Http.HttpClientHandler.<SendAsync>d__86.MoveNext()
--- End of inner exception stack trace ---
at System.Net.Http.HttpClientHandler.<SendAsync>d__86.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Net.Http.HttpClient.<FinishSendAsync>d__58.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Net.Http.HttpClient.<GetContentAsync>d__32`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at Plugin.RestClient.RestClient`1.<GetAsync>d__1.MoveNext()
I have little idea what went wrong here.
Update:
This still works fine in the Windows and Windows Phone applications The UWP application fails.