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
Try returning Task or Task instead of void.
or
await Testy();
When you compiling your code you get the following warning pointing to
Testy();
:Without applying
await
to theTesty();
your program will not await this method to be finishing execution and the program will terminating, when more likely theTesty()
method is performing theawait response.Content.ReadAsStringAsync();
call.Applying
await
to theTesty();
call will lead you to change thepublic static void Main(string[] args)
signature tostatic public async Task Main(string[] args)
.See the following note from the documentation Don’t block, await instead post:
Therefore change you application accordingly: