“添加异步时,程序不包含适用于入口点的静态'Main'方法

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

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

var response = await httpClient.SendAsync(request);

在方法制作中需要async

private static void Main(string[] args)

private static async Task Main(string[] args)

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

程序不包含适用于入口点的静态“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();
                }
            }
        }

有人请帮帮我!

c# asynchronous httpwebrequest httpwebresponse system.net
2个回答
0
投票

我怀疑你还在使用Visual Studio 2017,因为async Main works out of the box for new projects on Visual Studio 2019

要允许async Task Main,您需要指定LatestMinor作为您的语言版本。 (构建 - >高级 - >语言版本)。 async Main是一种C#7.1语言功能,在VS2017下,新项目默认为C#7.0。


-1
投票

它是main方法的必需void返回类型。您可以返回void main并更改此行:

  var response = await httpClient.SendAsync(request);

var response = httpClient.SendAsync(request).Result;

.Result获取任务的结果值。

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