如何在Xamarin.Forms可移植类库中使用WCF服务 [英] how to use WCF Service in Xamarin.Forms Portable class library

查看:61
本文介绍了如何在Xamarin.Forms可移植类库中使用WCF服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图调用使用WCF正确创建的方法,我开始为WCF调试项目,结果如下:

在我的 xamarin.forms 代码上,我使用了HttpClient库,如下所示:

 private async Task DownloadInfo()
        {
            var Uri = "http://localhost:10300/RestServiceImpl.svc/json";
            var httpClient = new HttpClient();
            var json= await  httpClient.GetStringAsync(Uri);
        } 

当我尝试从 Xamarin.Forms 获得json结果时,我得到以下信息:

我该怎么办?

解决方案

似乎您正在那里检查任务,但这并没有提供太多信息.您可以尝试这种更有条理的方法.

using (var httpClient = new HttpClient())
{
            httpClient.BaseAddress = new Uri("http://localhost:10300");
            var request = "/RestServiceImpl.svc/json";

            var result = await httpClient.GetAsync(request);

            if (!result.IsSuccessStatusCode)
                throw new HttpRequestException($"{result.StatusCode} \n {result.Content.ReadAsStringAsync().Result} \n\n {httpClient.BaseAddress}{request}");

            var json = await result.Content.ReadAsStringAsync();

            Debug.WriteLine(json);
}

I am trying to call method created correctly using WCF, I start debugging the project for WCF and the result as the following:

on my xamarin.forms code i used HttpClient Library as the following:

 private async Task DownloadInfo()
        {
            var Uri = "http://localhost:10300/RestServiceImpl.svc/json";
            var httpClient = new HttpClient();
            var json= await  httpClient.GetStringAsync(Uri);
        } 

when I am trying to get json result from Xamarin.Forms I get the following:

what I should do?

解决方案

It seems like you are inspecting the task there, this doesnt give that much information. You can try this little more structured approach.

using (var httpClient = new HttpClient())
{
            httpClient.BaseAddress = new Uri("http://localhost:10300");
            var request = "/RestServiceImpl.svc/json";

            var result = await httpClient.GetAsync(request);

            if (!result.IsSuccessStatusCode)
                throw new HttpRequestException($"{result.StatusCode} \n {result.Content.ReadAsStringAsync().Result} \n\n {httpClient.BaseAddress}{request}");

            var json = await result.Content.ReadAsStringAsync();

            Debug.WriteLine(json);
}

这篇关于如何在Xamarin.Forms可移植类库中使用WCF服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆