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

本文关键字:WCF 服务 Windows Phone | 更新日期: 2023-09-27 18:28:56

我正试图从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 phone模拟器连接到本地主机的问题。

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

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

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

Windows Phone 8.1中的Windows Phone Store应用程序不支持System.ServiceModel命名空间。

有一种变通方法可以

请阅读此处。

您可以使用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;
        }
    }