how to use WCF Service in Xamarin.Forms Portable class library

北城以北 提交于 2019-12-02 16:43:28

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/41680217/how-to-use-wcf-service-in-xamarin-forms-portable-class-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!