添加异步时“程序不包含适合入口点的静态‘Main’方法”

She*_*ays 2 c# asynchronous system.net httpwebrequest httpwebresponse

所以我一直在用 C# 摆弄 Web 响应和请求,当我尝试运行该程序时遇到了问题。我的其中一行代码:

var response = await httpClient.SendAsync(request);
Run Code Online (Sandbox Code Playgroud)

方法制作中需要异步

private static void Main(string[] args)
Run Code Online (Sandbox Code Playgroud)

进入

private static async Task Main(string[] args)
Run Code Online (Sandbox Code Playgroud)

看起来没有错误,但是在构建时,我收到错误消息:

程序不包含适合入口点的静态“Main”方法。

这是我的代码

private static async Task Main(string[] args)
        {

            try
            {
                /*HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://billing.roblox.com/v1/gamecard/redeem");
                            request.Method = "POST";
                            request.ContentType = "application/json";
                            request.Accept = "application/json";
                            JsonExtensionDataAttribute data = new JsonExtensionDataAttribute();
                            data = '3335996838'*/
                using (var httpClient = new HttpClient())
                {
                    using (var request = new HttpRequestMessage(new HttpMethod("POST"), "https://billing.roblox.com/v1/gamecard/redeem"))
                    {
                        request.Headers.TryAddWithoutValidation("Accept", "application/json");

                        request.Content = new StringContent("3335996838", Encoding.UTF8, "application/json");

                        var response = await httpClient.SendAsync(request);

                        Console.WriteLine(request.Content);
                        Console.ReadLine();
                    }
                }
            }
            catch (WebException ex)
            {
                string content;
                using (StreamReader reader = new StreamReader(ex.Response.GetResponseStream()))
                {
                    content = reader.ReadToEnd();
                    Console.WriteLine(content);
                    Console.ReadLine();
                }
            }
        }
Run Code Online (Sandbox Code Playgroud)

有人请帮助我!

Ste*_*ary 5

我怀疑您仍在使用 Visual Studio 2017,因为async Main Visual Studio 2019 上的新项目可以开箱即用

要允许async Task Main,您需要指定LatestMinor您的语言版本。(构建 -> 高级 -> 语言版本)。async Main是C# 7.1语言特性,在VS2017下,新项目默认使用C# 7.0。