C#async不';不要等待
我正在使用的应用程序应该通过http客户端检索json字符串,然后反序列化并在应用程序中使用 除了等待功能外,所有功能都正常工作。我做错了什么,我似乎不知道是什么。我如何确保我的DataService类一直等到我有了json并且它已经被反序列化 数据服务类:C#async不';不要等待,c#,json,asynchronous,async-await,task,C#,Json,Asynchronous,Async Await,Task,我正在使用的应用程序应该通过http客户端检索json字符串,然后反序列化并在应用程序中使用 除了等待功能外,所有功能都正常工作。我做错了什么,我似乎不知道是什么。我如何确保我的DataService类一直等到我有了json并且它已经被反序列化 数据服务类: class DataService : IDataService { private IEnumerable<Concert> _concerts; public DataService() {
class DataService : IDataService
{
private IEnumerable<Concert> _concerts;
public DataService()
{
_concerts = new DataFromAPI()._concerts;
Debug.WriteLine("____Deserialization should be done before continuing____");
**other tasks that need the json**
}
}
class DataService : IDataService
{
private IEnumerable<Concert> _concerts;
public DataService()
{
_concerts = new DataFromAPI()._concerts;
}
}
class DataService : IDataService
{
private IEnumerable<Concert> _concerts;
public DataService()
{
_concerts = new DataFromAPI()._concerts;
}
}
您正在调用
DataFromAPI
构造函数中的Retrieve()
而不调用await
,这就是为什么不等待您的方法
您最好在构造函数之外调用此方法,使用wait关键字如下:
await Retrieve();
你必须稍微重构一下你的代码。下面是一个例子:
public class DataService : IDataService
{
private IEnumerable<Concert> _concerts;
public async Task LoadData()
{
_concerts = await DataFromAPI.Retrieve();
**other tasks that need the json**
}
}
public static class DataFromAPI
{
public static async Task<IEnumerable<Concert>> Retrieve()
{
try
{
HttpClient client = new HttpClient();
HttpRequestMessage request = new HttpRequestMessage();
var result = await client.GetAsync(new Uri("http://url-of-my-api"), HttpCompletionOption.ResponseContentRead);
string jsonstring = await result.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<IEnumerable<Concert>>(response.ToString());
}
catch(Exception)
{
}
return Enumerable.Empty<Concert>();
}
}
当然,这两行代码也必须从异步方法调用。(异步/一路等待)经过大量的尝试和错误,我意识到对于这个特定的东西,它不必是异步的,所以我只是在主线程上重新创建了,并取得了成功: 数据服务类:
class DataService : IDataService
{
private IEnumerable<Concert> _concerts;
public DataService()
{
_concerts = new DataFromAPI()._concerts;
Debug.WriteLine("____Deserialization should be done before continuing____");
**other tasks that need the json**
}
}
class DataService : IDataService
{
private IEnumerable<Concert> _concerts;
public DataService()
{
_concerts = new DataFromAPI()._concerts;
}
}
class DataService : IDataService
{
private IEnumerable<Concert> _concerts;
public DataService()
{
_concerts = new DataFromAPI()._concerts;
}
}
如果我不能把Retrieve()放在构造函数中,我会想办法把它放在哪里,但是我真的不知道该把它放在哪里。就在
DataFromAPI
构造函数之后:var temp=new DataFromAPI()
然后就在wait temp.Retrieve()之后代码>你的意思是把它放在方法之外?这是另一个问题:)至少你解决了异步问题:)如果你想更深入地讨论异步/等待问题,我可以向你推荐这个优秀的博客:
public static class DataFromAPI
{
public void Retrieve()
{
try
{
HttpClient client = new HttpClient();
HttpRequestMessage request = new HttpRequestMessage();
var result = client.GetAsync("http://url-of-my-api").Result;
if (result.IsSuccessStatusCode)
{
var responseContent = result.Content;
}
DownloadCompleted(result.Content.ReadAsStringAsync().Result);
}
catch {}
}
}