2017-01-23 41 views
0

我用下面的代码创建WCF:Xamarin.Forms不能得到WCF结果的方法,通过网址

[ServiceContract] 
    public interface IRestServiceImpl 
    {   
    [OperationContract] 
    [WebGet(ResponseFormat = WebMessageFormat.Json, 
     BodyStyle =WebMessageBodyStyle.Wrapped, 
     UriTemplate = "json")] 
    string JSONData(); 
} 



    public class RestServiceImpl : IRestServiceImpl 
     { 
      public string JSONData() 
      { 
       return "You Requested Prodcut"; 
      } 
     } 

和我说WCF到本地IIS,它是通过以下工作正常: enter image description here

当我试图从xamarin.forms应用程序,我得到一个异常值

System.Net.WebExceptionStatus.ConnectFailure

,结果应该是什么?

private async Task DownloadInfo() 
     { 
      var Uri = "http://localhost:8020/wcfrest/RestServiceImpl.svc/json"; 
      var httpClient = new HttpClient(); 
      var json= await httpClient.GetStringAsync(Uri);//I get exception here System.Net.WebExceptionStatus.ConnectFailure 
     } 

回答

1

它可以通过颇有几件事情引起的:

  1. 你加上网权限,您的应用程序?

  2. 你确定你的设备是相同的网络API吗?

  3. 尝试使用IP地址而不是本地主机

  4. 主机添加到您的HttpClient对象:

    VAR的HttpClient =新的HttpClient {主机= “localhost” 的};

+0

感谢您的提示:) –

+1

确实有帮助您吗? –