HttpClient在GetStringAsync方法上冻结

本文关键字:冻结 方法 GetStringAsync HttpClient | 更新日期: 2023-09-27 18:24:55

我有这个c#MVC代码,它可以很好地与GetAsync和GetPost方法配合使用,但当使用GetStringAsync时,它会冻结在以下行:

version = await client.GetStringAsync("/API/Version");

驱动程序代码:

Task<string>[] tasks = new Task<string>[count];
for (int i = 0; i < count; i++)
{
    tasks[i] = MyHttpClient.GetVersion(port, method);
}
Task.WaitAll(tasks);
string[] results = new string[tasks.Length];
for(int i=0; i<tasks.Length; i++)
{
    Task<string> t = (Task<string>)(tasks[i]);
    results[i] = (string)t.Result;
}

HttpCilent代码:

public static async Task<string> GetVersion(int port, string method)
{
    try
    {
        var client = new HttpClient();
        client.BaseAddress = new Uri("http://localhost:" + port);
        string version = null;
        if (method.ToUpper().Equals("GETSTR"))
        {
            version = await client.GetStringAsync("/API/Version");
        }
        else if (method.ToUpper().Equals("GET"))
        {
            HttpResponseMessage res = client.GetAsync("/API/Version").Result;
            version = res.Content.ReadAsStringAsync().Result;
        }
        else if (method.ToUpper().Equals("POST"))
        {
            var content = new FormUrlEncodedContent(new[] 
            {
                new KeyValuePair<string, string>("name", "jax")
            });
            HttpResponseMessage res = client.PostAsync("/API/Version", content).Result;
            version = res.Content.ReadAsStringAsync().Result;
        }
        client.Dispose();
        return version;
    }
    catch (Exception ex)
    {
        return "Error: " + ex.Message;
    }
}
}

有什么想法吗?

HttpClient在GetStringAsync方法上冻结

通常,不应将async/await.Result.Wait()混合使用。首先修复GetVersion方法:

public static async Task<string> GetVersion(int port, string method)
{
    try
    {
        using (var client = new HttpClient())
        {
            client.BaseAddress = new Uri("http://localhost:" + port);
            if (method.ToUpper().Equals("GETSTR"))
            {
                return await client.GetStringAsync("/API/Version")
                    .ConfigureAwait(false);
            }
            else if (method.ToUpper().Equals("GET"))
            {
                var res = await client.GetAsync("/API/Version").ConfigureAwait(false);
                return await res.Content.ReadAsStringAsync().ConfigureAwait(false);
            }
            else if (method.ToUpper().Equals("POST"))
            {
                var content = new FormUrlEncodedContent(new[] 
                {
                    new KeyValuePair<string, string>("name", "jax")
                });
                var res = await client.PostAsync("/API/Version", content)
                    .ConfigureAwait(false);
                return await res.Content.ReadAsStringAsync().ConfigureAwait(false);
            }
        }
    }
    catch (Exception ex)
    {
        return "Error: " + ex.Message;
    }
}

我在这里所做的只是用await <task>替换<task>.Result调用。对于每个await,如果遇到每个await表达式,则await之后的方法部分将被注册为正在等待的Task的延续,而不是在.Result的情况下阻塞线程。

现在,在你的呼叫代码中,你需要做同样的事情:

var tasks = new Task<string>[count];
for (int i = 0; i < count; i++)
{
    tasks[i] = MyHttpClient.GetVersion(port, method);
}
var results = new string[tasks.Length];
for (int i = 0; i < tasks.Length; i++)
{
    results[i] = await tasks[i];
}

或者更简单地说:

var tasks = new Task<string>[count];
for (int i = 0; i < count; i++)
{
    tasks[i] = MyHttpClient.GetVersion(port, method);
}
var results = await Task.WhenAll(tasks);