HttpClient.PostAsync退出应用,退出代码为0

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

今天一切正常,直到停止为止...下面是最小的源代码(我使用的是VS 2012 Update 1,.Net 4.5)。当我运行它时,应用程序会在调用client.PostAsync()时退出,因此它永远不会到达Console.ReadLine()。在调试器中相同,没有例外,没有,退出代码为0。

我尝试重新启动计算机,然后重新启动VS2012-无效。

同样,今天一切都在运行,不确定更改了什么(没有安装软件等,所有其他网络应用程序仍在工作。

有什么想法吗?我想我正在失去理智。

class Program
{
    static void Main(string[] args)
    {
        Run();
    }

    private async static void Run()
    {
        using (var client = new System.Net.Http.HttpClient())
        {
            var headers = new List<KeyValuePair<string, string>>
                              {
                                  new KeyValuePair<string, string>("submit.x", "48"),
                                  new KeyValuePair<string, string>("submit.y", "15"),
                                  new KeyValuePair<string, string>("submit", "login")
                              };

            var content = new FormUrlEncodedContent(headers);

            HttpResponseMessage response = await client.PostAsync("http://www.google.com/", content);

            Console.ReadLine();
        }
    }
}

今天一切正常,直到停止为止...下面是最小的源代码(我使用的是VS 2012 Update 1,.Net 4.5)。当我运行它时,应用程序会在调用client.PostAsync()时退出,因此它永远不会...

c# visual-studio-2012 httpclient .net-4.5 async-await
1个回答
40
投票

您的问题是,程序在完成其Main()方法后通常会退出。当您击中Main()中的await时,您的Run()即会完成,因为这是async方法的工作方式。

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