As the comments already suggested you need to wait for the task to finish. There is the method to call Task.Result
but there are some common cases where calling this method causes a deadlock in your application and everything comes to a stop.
As a general rule you can say: Never mix async/await
with .Result/.Wait
.
Now your main issue is that you are working inside a constructor and need to wait for your task to complete. The solution to this issue I prefer is a static factory method.
Like so:
public static async Task<YourClass> CreateAsync(…)
{
var rows = await Scrape();
return new YourClass(rows, …);
}
The constructor is set to private and when you need to create a instance of the class you call the CreateAsync
method.
This solution assumes that the Scrape
method is static as well. But it should be anyway if you call it from the constructor (or at least it should be easy to make it static
, since all the parameters it could receive have to be passed to the constructor.
A alternative solution is using a initialization function. This would be the solution if you can't turn the Scape
method static. Even in this case I would suggest a factory method, to avoid exposing instances of the class that are not properly initialized.
In this case you basically need two method like this:
public static async Task<YourClass> CreateAsync(…)
{
var result = new YourClass(…);
await result.InitializeAsync();
return result;
}
private async Task InitializeAsync()
{
TeamRows2 = await Scrape();
}