如何在Windows Phone 8.1中使用WCF服务

时间:2014-11-04 08:29:27

标签: c# wcf windows-phone-8.1 windows-phone

我正在尝试从Windows Phone 8.1应用程序调用WCF服务,添加服务引用的选项不再存在。

我试过了:

HttpClient httpClient = new System.Net.Http.HttpClient();
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://serverURl/serviceName.svc/methodName?variableName=value");
HttpResponseMessage response = await httpClient.SendAsync(request);
string data = await response.Content.ReadAsStringAsync();

但它没有用,字符串总是空的。

N.B 服务器也不是本地主机,因此我不会面临从Windows手机模拟器连接到本地主机的问题。

3 个答案:

答案 0 :(得分:2)

试试这个。 如果它不起作用,请告诉我。

HttpClient httpClient = new System.Net.Http.HttpClient();
 HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get,"http://localhost:18362/Service1.svc/GetData");
 HttpResponseMessage response = await httpClient.SendAsync(request);
 string data = await response.Content.ReadAsStringAsync();
 var dialog = new MessageDialog(data);
 await dialog.ShowAsync();

答案 1 :(得分:1)

Windows Phone 8.1中的Windows Phone应用商店不支持System.ServiceModel命名空间。

您可以

找到解决方法

阅读here

答案 2 :(得分:0)

您可以使用System.Net.Http:

public async Task<string> GetMethod(string url)
    {
        HttpClient client = new HttpClient();
        var response = await client.GetAsync(url); // The Get Process to get result from WCF service
        string result = await response.Content.ReadAsStringAsync(); // Get Json string result
        return result;
        }
    }