3

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,

Instructions to make webservice work on android emulator

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.

la theos
  • 45
  • 3
  • From what you've posted it looks like you had a different url and path at every step. As such it would be hard to tell what when wrong without accurate information. It looks like you may have simply not had the right WebServiceUrl in your client. Did you solve your problem in the end Ia? – monty Feb 14 '18 at 20:15

2 Answers2

2

In your RestClient class, the URL http://localhost:52792/api/Employees/ is pointing to the device's localhost, not the machine that's hosting the IIS Express.

Frankie
  • 128
  • 1
  • 7
1

If your UWP app is trying to access a service via localhost, confirm it has a loopback exemption.

See UWP Enable local network loopback for more detail. Note in particular that some people have found the loopback exemption table can become corrupted, which clearing all exemptions and re-applying any require seems to resolve.

https://learn.microsoft.com/en-us/previous-versions/windows/apps/dn640582(v=win.10)

Kyle
  • 1,366
  • 2
  • 16
  • 28