2

So I have been fiddling around with Web responses and requests in C# and I received a problem when I tried to run the program. One of the lines of code I had:

var response = await httpClient.SendAsync(request);

required async in the method making

private static void Main(string[] args)

into

private static async Task Main(string[] args)

It looks like there is no error, but on build, I got the error message:

Program does not contain a static 'Main' method suitable for an entry point.

Here is my code

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();
                }
            }
        }

Someone please help me!

Anthony
  • 3,595
  • 2
  • 29
  • 38
Sheng Plays
  • 31
  • 1
  • 2
  • Remove `Task`, add back `void`. But, you can take the suggestions you find here: [Can't specify the 'async' modifier on the 'Main' method of a console app](https://stackoverflow.com/q/9208921/7444103). You'll have other problems after. The connection has a good chance to be closed before you can get a response. – Jimi Apr 25 '19 at 00:27

2 Answers2

5

I suspect you are still using Visual Studio 2017, since async Main works out of the box for new projects on Visual Studio 2019.

To allow async Task Main, you need to specify LatestMinor as your language version. (Build -> Advanced -> Language Version). async Main is a C# 7.1 language feature, and under VS2017, new projects default to C# 7.0.

Stephen Cleary
  • 437,863
  • 77
  • 675
  • 810
  • Thank you, I have a question, how can I convert System.Net.Http.HttpContent to System.IO.Stream, kind of a out of the blue question but I cannot find a good answer online to help me with this issue. Here is my current code ``` var response = httpClient.SendAsync(request).Result; using (var content1 = new StreamReader(response.Content)) { Console.WriteLine(content1); Console.ReadLine(); }``` – Sheng Plays Apr 25 '19 at 01:03
  • 1
    I recommend asking a separate question since the new one really has nothing to do with this question. Keeping separate questions separate helps attract other answerers and helps future Googlers. – Stephen Cleary Apr 25 '19 at 01:05
  • Excellent. That was exactly it! had to set Language version to C# 7.1 in Visual Studio 2017 and viola, works like a champ. Thank you !! – FisheyJay Oct 26 '22 at 03:05
-1

It's required void return type for the main method. You can back to void main and change this line:

  var response = await httpClient.SendAsync(request);

to

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

.Result is getting the result value of the task.