skip to Main Content

I don’t really know why my code is breaking up at line 24, when it’s trying to execute the first await method. I mean, it doesn’t show me nor a compilation nor execution error. I already installed all 5 Jsonize packages the guide says. I think the problem could be because the call is not awaited, but I don’t really know what might be happening.

I’m trying to follow step by step this guide about what I want to achieve. This is my goal:
https://github.com/JackWFinlay/jsonize

Here is my entire code:

using Jsonize;
using Jsonize.Parser;
using Jsonize.Serializer;
using System;
using System.Net.Http;
using System.Threading.Tasks;

namespace JsonizeExample
{
    class Program
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            Testy();
        }

        public static async void Testy()
        {
            using (var client = new HttpClient())
            {
                string url = @"https://jackfinlay.com";

                HttpResponseMessage response = await client.GetAsync(url);

                string html = await response.Content.ReadAsStringAsync();

                // The use of the parameterless constructors will use default settings.
                JsonizeParser parser = new JsonizeParser();
                JsonizeSerializer serializer = new JsonizeSerializer();
                Jsonizer jsonizer = new Jsonizer(parser, serializer);
            }
        }

    }
}

I’m working on Visual Studio 2019, .NET Core 3.1, C#.

What am I doing wrong?

2

Answers


  1. Try returning Task or Task instead of void.
    or
    await Testy();

    Login or Signup to reply.
  2. When you compiling your code you get the following warning pointing to Testy();:

    warning CS4014: Because this call is not awaited, execution of the
    current method continues before the call is completed.

    Without applying await to the Testy(); your program will not await this method to be finishing execution and the program will terminating, when more likely the Testy() method is performing the await response.Content.ReadAsStringAsync(); call.

    Applying await to the Testy(); call will lead you to change the public static void Main(string[] args) signature to static public async Task Main(string[] args).

    See the following note from the documentation Don’t block, await instead post:

    NOTE: The Main method returns Task, despite not having a return
    expression—this is by design. For more information, see Evaluation of
    a void-returning async function
    .

    Therefore change you application accordingly:

    static async Task Main(string[] args)
    {
        Console.WriteLine("Hello World!");
        await Testy();
    }
    
    public static async Task Testy()
    {
        using (var client = new HttpClient())
        {
            ... your code           
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search