2017-01-23 39 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

確實有幫助您嗎? –