2017-04-12 116 views
0

我正在使用Xamarin Forms与Web服务一起玩。我制作了一个简单的Web服务,它托管在IIS Express上(与VS 2015一起提供)。 Web服务处理员工信息。无法连接到UWP应用程序托管在本地主机上的webservice

这最初工作正常与Windows模拟器。但我有问题要得到它与Android emulator.Upon寻求帮助我从一个人下面的说明工作,

Instructions to make webservice work on android emulator

下面的说明后,它不仅不能在Android模拟器工作,但UWP应用程序也停止工作。

以下是它失败

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; 
     } 

而且下面是错误抛出,

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() 

我根本不知道出了什么错在这里。

更新:

这仍然工作正常的Windows和Windows Phone应用程序 的UWP应用程序失败。

+0

从您发布的内容看,您似乎在每一步都有不同的网址和路径。因此,如果没有准确的信息,很难说出什么时候发生了错误。看起来您可能根本没有在您的客户端中拥有正确的WebServiceUrl。你最终解决了你的问题吗? – monty

回答

0

在您的RestClient类中,URL http://localhost:52792/api/Employees/指向设备的本地主机,而不是托管IIS Express的机器。

相关问题