异步,等待和任务仅在调试模式下工作 [英] Async, await and Task works in debug mode only

查看:83
本文介绍了异步,等待和任务仅在调试模式下工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码从.net页的第一行调用"LoadNames"开始.如果我未处于调试模式,则将访问变量设置为null.如果我在其中添加调试点并逐步执行,则将从api中获取一个值.

The code below begins with the first line calling 'LoadNames' from a .net page. If I'm not in debug mode the interviews variable is set to null. If I add a debug point there and step through it gets a value from the api.

它正在接受UAT的采访.我现在将它指向LIVE.

It was getting interviews in UAT. I'm pointing it now at LIVE.

我认为这可能是异步解决的问题.较慢的旧api可能会使它看起来像在工作,而添加debug也会使其变慢,从而使其看起来也正确.

I'm thinking it's likely something unresolved asynchronously. The old api being slower probably made it appear like it was working, and adding debug will slow it down making it appear correct too.

Page.RegisterAsyncTask(new PageAsyncTask(LoadNames));

private async Task LoadNames()
{
   VideoInterviewRepository videoRepository = await Repository.CreateClient();
   IEnumerable<Api> interviews = await Repository.GetEntityList<Api>(EndPoints);

   CODE HERE RUNS BUT FAILS BECAUSE THE ABOVE CODE RETURNS NULL
   var interviewList = interviews.ToDictionary(o => o.id, o => o.name);
}


public static Task<VideoInterviewRepository> CreateClient()
{
    var videoInterviewRepository = new VideoInterviewRepository();
    return videoInterviewRepository.InitializeClient();
}
private async Task<VideoInterviewRepository> InitializeClient()
{
    client = VideoInterviewHttpClient.GetClient(VideoInterviewEndPoints.baseUrl);

    var bearer = await Authenticate.GetBearerTokenAsync(client);

    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", bearer);

    return this;
}
public static async Task<string> GetBearerTokenAsync(HttpClient client)
{
   var bearerResult = await client.SendAsync(requestToken);
   var bearerData = await bearerResult.Content.ReadAsStringAsync();

   bearerToken = JObject.Parse(bearerData)["access_token"].ToString();
}

public async Task<IEnumerable<T>> GetEntityList<T>(string path)
{
    IEnumerable<T> model = await GetAndParseApiResponse<IEnumerable<T>>(path);
    return model;
}

private async Task<T> GetAndParseApiResponse<T>(string path)
{
   HttpResponseMessage response = await client.GetAsync(path);
   if (response.IsSuccessStatusCode)
   {
      string content = await response.Content.ReadAsStringAsync();
      model = JsonConvert.DeserializeAnonymousType<T>(content, model);
   }

   return model;
}

推荐答案

我发现了这个错误!所有异步,等待和任务均正确.第三方没有足够快地在其末端注册令牌,这就是为什么单步执行造成了延迟.我应该已经处理了response.IsSuccessStatusCode.

I found the bug! All the async and awaits and Tasks were correct. The 3rd party didn't register the token on their end fast enough which is why by stepping through I caused a delay. I should have handled response.IsSuccessStatusCode.

这篇关于异步,等待和任务仅在调试模式下工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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