异步操作超出页面超时(尝试将 HttpRequestMessage 与 ASP.NET WebForms 页面一起使用)

问题描述 投票:0回答:1

我在 AWS 中创建了一个 Web API,我正在尝试使用 ASP.NET Webforms(最新版本)中内置的网页获取一些 JSON。我无法让异步部分工作。要么 GET 方法似乎永远挂起,要么 - 遵循 Microsoft 文档中的最佳实践方法 - 过了一会儿我收到此错误:

[TimeoutException:异步操作超出页面 超时。] System.Web.UI.d__554.MoveNext() +984

我知道这与代码的等待/异步部分以及 ASP.NET 中的内容有关,原因如下。

  • 如果我在控制台应用程序中使用非常相似的代码,它就可以正常工作。
  • 如果我使用 POSTMAN 调用 Web API,它工作正常。

我在页面指令中设置了 async = true 。这是我的页面加载

protected void Page_Load(object sender, EventArgs e)
{
    try
    {
        RegisterAsyncTask(new PageAsyncTask(GetStuffAsync));
    }
    catch (Exception ex)
    {
        renderStoreCards.Text = ex.Message;
    }
}

这是我的方法

    private async Task GetStuffAsync()
    {
        string testHtml = string.Empty;

        try
        {
            var signer = new AWS4RequestSigner("AccessKey", "SecretKey");
            var request = new HttpRequestMessage
            {
                Method = HttpMethod.Get,
                RequestUri = new Uri("https://some-aws-address-changed-for-stack-overflow.execute-api.ap-southeast-2.amazonaws.com/Prod/tables/InSiteStoreInformation/ServerName")
            };

            request = await signer.Sign(request, "execute-api", "ap-southeast-2");
            var client = new HttpClient();
            var response = await client.SendAsync(request).ConfigureAwait(false);
            string responseString = await response.Content.ReadAsStringAsync();
        }
        catch (Exception ex)
        {
            renderStoreCards.Text = ex.Message; 
        }
    }

上面的例子产生了一个TimeoutException。在上面之前,我正在尝试以下代码。这在控制台应用程序中工作正常,但在 ASP.NET 页面中则不然。

  class Program
    {
        static void Main(string[] args)
        {
            try
            {
                MainAsync().Wait();
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Exception occured {ex.Message}");
                Console.ReadKey();
            }
        }

        static async Task MainAsync()
        {
            try
            {
                var signer = new AWS4RequestSigner("AccessKey", "SecretKey");
                var request = new HttpRequestMessage
                {
                    Method = HttpMethod.Get,
                    RequestUri = new Uri("https://<Hiddenforstackoverflowpost>.execute-api.ap-southeast-2.amazonaws.com/Prod/tables/InSiteStoreInformation/ServerName")
                };

                request = await signer.Sign(request, "execute-api", "ap-southeast-2");

                var client = new HttpClient();
                var response = await client.SendAsync(request);

                var responseStr = await response.Content.ReadAsStringAsync();
                dynamic sales = Newtonsoft.Json.JsonConvert.DeserializeObject(responseStr);
                Console.WriteLine($"Server = {sales[0].ServerName}");
                Console.ReadKey();
                Console.Write(responseStr);
                Console.ReadKey();
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }
    }

我绝不是异步/等待组合方面的专家,但看来我正在使用的 HttpClient 没有同步替代方案,所以我必须弄清楚这一点。

c# asp.net asp.net-web-api async-await
1个回答
2
投票

我知道这是一个老问题,但我刚刚遇到。 超时很可能是由 ReadAsStringAsync 引起的,因为您忽略了对其使用“.ConfigureAwait(false)”。在 ASP.net 中运行异步任务可能非常挑剔,尤其是在执行上下文周围。当异步方法尝试在返回时恢复执行上下文时,它很可能是某种死锁。由于 IIS 托管的性质,这通常会失败。我不确定这实际上是死锁还是其他问题。只需确保始终使用 .ConfigureAwait(false)。

我知道这已经很旧了,但也许它可以帮助其他人解决这个问题。

© www.soinside.com 2019 - 2024. All rights reserved.