我使用以下代码创建了一个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";
}
}
当我试图从xamarin.forms app获取价值时,我得到一个异常
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
}
答案 0 :(得分:1)
这可能是由很多事情造成的:
您是否为自己的应用添加了互联网权限?
您确定自己的设备与API位于同一网络吗?
尝试使用IP地址而不是localhost
将主机添加到您的HttpClient对象:
var httpClient = new HttpClient {Host =" localhost" };