C# HttpClient上的UWP等待不工作
我正在尝试从web API获取JSON响应 我能够用类似的代码在控制台应用程序中检索响应,但是在UWPC# HttpClient上的UWP等待不工作,c#,uwp,dotnet-httpclient,C#,Uwp,Dotnet Httpclient,我正在尝试从web API获取JSON响应 我能够用类似的代码在控制台应用程序中检索响应,但是在UWP中等待httpClient.GetAsync(uri)未按预期工作 public static async Task<double> GetJson() { using (var httpClient = new HttpClient()) { Uri uri= new Uri("https://api.cryptonator.com/api/tick
中等待httpClient.GetAsync(uri)代码>未按预期工作
public static async Task<double> GetJson()
{
using (var httpClient = new HttpClient())
{
Uri uri= new Uri("https://api.cryptonator.com/api/ticker/btc-usd");
HttpResponseMessage response = await httpClient.GetAsync(uri);
//Below code is not relevent since code is failing on await
var result = response.Content.ReadAsStringAsync();
var jsonResponse = Json.ToObjectAsync<ExchangeRate>(result);//
jsonResponse.Wait();//
ExchangeRate exchangeRateObj = jsonResponse.Result;//
return 1.2;//
}
}
这里什么是不起作用的意思?
它应该连接到URL并获取响应,并且应该为响应分配一些值。相反,在调试时,无论是单步执行还是继续,控件都会退出当前行,并跳过后续行。
(我在接下来的几行中也加入了调试),应用程序只是冻结了
我在Stackoverflow和其他博客上用HTTPClient
查阅了类似的JSON解析代码,建议使用System.Net.Http
或Windows.Web.Http
相关问题:
我认为任务是运行的,它一直处于等待模式,这似乎很奇怪,因为调试模式不显示正在运行的代码,它只显示ready
。也不例外
我是做错了什么还是遗漏了一些Nuget参考
请建议
PS:这与httpClient.GetStringAsync
方法的情况相同。
在控制台应用程序上,该行工作,但在UWP上不工作
var json = new WebClient().DownloadString("https://api.cryptonator.com/api/ticker/btc-usd");
不重复
- 它不是Xamarin,尽管它是基于C#的,但我的代码不同,它不是我关心的WebApiClient或GetInformationAsync方法
指定的代码中有几个错误需要纠正。首先,将事件处理程序标记为异步:
private async void Button_Click(object sender, RoutedEventArgs e)
currency_.ROC = await MyClass.GetJsonAsync();
其次,等待GetJson
,因为这是一种异步方法,所以在名称中添加后缀“Async”是一种更好的约定;因此,等待GetJsonAsync
:
private async void Button_Click(object sender, RoutedEventArgs e)
currency_.ROC = await MyClass.GetJsonAsync();
现在,回到GetJsonAsync
本身,ReadAsStringAsync
和ToObjectAsync
也应该等待:
private static async Task<double> GetJsonAsync()
{
using (var httpClient = new HttpClient())
{
Uri uri = new Uri("https://api.cryptonator.com/api/ticker/btc-usd");
HttpResponseMessage response = await httpClient.GetAsync(uri);
string result = await response.Content.ReadAsStringAsync();
//Below code is not relevent since code is failing on await
ExchangeRate exchangeRateObj = await Json.ToObjectAsync<ExchangeRate>(result);
return 1.2;//
}
}
private静态异步任务GetJsonAsync()
{
使用(var httpClient=new httpClient())
{
Uri=新的Uri(“https://api.cryptonator.com/api/ticker/btc-usd");
HttpResponseMessage response=等待httpClient.GetAsync(uri);
字符串结果=wait response.Content.ReadAsStringAsync();
//以下代码不相关,因为代码在等待时失败
ExchangeRate exchangeRateObj=等待Json.ToObject异步(结果);
回报率1.2//
}
}
它以前不工作的原因是,Wait
调用和.Wait()
的同步块之间的上下文切换导致死锁。了解更多信息。指定的代码中有几个错误需要纠正。首先,将事件处理程序标记为异步:
private async void Button_Click(object sender, RoutedEventArgs e)
currency_.ROC = await MyClass.GetJsonAsync();
其次,等待GetJson
,因为这是一种异步方法,所以在名称中添加后缀“Async”是一种更好的约定;因此,等待GetJsonAsync
:
private async void Button_Click(object sender, RoutedEventArgs e)
currency_.ROC = await MyClass.GetJsonAsync();
现在,回到GetJsonAsync
本身,ReadAsStringAsync
和ToObjectAsync
也应该等待:
private static async Task<double> GetJsonAsync()
{
using (var httpClient = new HttpClient())
{
Uri uri = new Uri("https://api.cryptonator.com/api/ticker/btc-usd");
HttpResponseMessage response = await httpClient.GetAsync(uri);
string result = await response.Content.ReadAsStringAsync();
//Below code is not relevent since code is failing on await
ExchangeRate exchangeRateObj = await Json.ToObjectAsync<ExchangeRate>(result);
return 1.2;//
}
}
private静态异步任务GetJsonAsync()
{
使用(var httpClient=new httpClient())
{
Uri=新的Uri(“https://api.cryptonator.com/api/ticker/btc-usd");
HttpResponseMessage response=等待httpClient.GetAsync(uri);
字符串结果=wait response.Content.ReadAsStringAsync();
//以下代码不相关,因为代码在等待时失败
ExchangeRate exchangeRateObj=等待Json.ToObject异步(结果);
回报率1.2//
}
}
它以前不工作的原因是,Wait
调用和.Wait()
的同步块之间的上下文切换导致死锁。了解更多信息。您的代码运行良好。我在下面重新生成了它。把你正在做的那个古怪的等待电话处理掉
这样做。创建一个新的uwp应用程序,粘贴下面的代码,并在返回上放置一个断点,然后查看它是否得到执行
无论是否使按钮处理程序异步,它都会工作。如果不将其设为asnyc,则不会异步执行请求
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
SomeClass.GetJson();
}
}
public class SomeClass
{
public static async Task<double> GetJson()
{
using (var httpClient = new HttpClient())
{
Uri uri = new Uri("https://api.cryptonator.com/api/ticker/btc-usd");
HttpResponseMessage response = await httpClient.GetAsync(uri);
return 1.2;
}
}
}
公共密封部分类主页面:第页
{
公共主页()
{
this.InitializeComponent();
}
私有无效按钮\u单击(对象发送者,路由目标e)
{
GetJson();
}
}
公共类
{
公共静态异步任务GetJson()
{
使用(var httpClient=new httpClient())
{
Uri=新的Uri(“https://api.cryptonator.com/api/ticker/btc-usd");
HttpResponseMessage response=等待httpClient.GetAsync(uri);
回报率1.2;
}
}
}
我可以借此机会无耻地插入我的UWP库吗。它为你做这件事
在该文件中有一个BeginRequest函数,它可以异步为您执行此操作。lib还有许多其他特性 您的代码运行良好。我在下面重新生成了它。把你正在做的那个古怪的等待电话处理掉
这样做。创建一个新的uwp应用程序,粘贴下面的代码,并在返回上放置一个断点,然后查看它是否得到执行
无论是否使按钮处理程序异步,它都会工作。如果不将其设为asnyc,则不会异步执行请求
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
SomeClass.GetJson();
}
}
public class SomeClass
{
public static async Task<double> GetJson()
{
using (var httpClient = new HttpClient())
{
Uri uri = new Uri("https://api.cryptonator.com/api/ticker/btc-usd");
HttpResponseMessage response = await httpClient.GetAsync(uri);
return 1.2;
}
}
}
公共密封部分类主页面:第页
{
公共主页()
{
this.InitializeComponent();
}
私有无效按钮\u单击(对象发送者,路由目标e)
{
GetJson();
}
}
公共类
{
公共静态异步任务GetJson()
{
使用(var httpClient=new httpClient())
{
Uri=新的Uri(“https://api.cryptonator.com/api/ticker/btc-usd");
HttpResponseMessage response=等待httpClient.GetAsync(uri);
回报率1.2;
}
}
}
我可以占用这个时间吗