Given: I've a class which manages user storage. Now when creating the class I want to ensure indexes which is a asynchronous method. Currently i just call wait, but i don't feel comfortable to it as I'm a little concerned about unexpected behaviors like hanging threads.
public MongoUserStorage(IMongoDatabase database)
{
userCollection = database.GetCollection<MongoUser>("Users");
roleCollection = database.GetCollection<MongoUserRole>("UserRoles");
CreateIndexesAsync().Wait();
}
Question:
What is best way to handle this asynchous mehtod? (I want to be sure that the indexes are present after the class is instantiated)
Just do: CreateIndexesAsync().Wait();
or
var task = CreateIndexesAsync();
task.ConfigureAwait(false);
task.Wait();
Or any better solution ?
ConfigureAwaitdoes nothing if used this way. As you may noticed, it returnsTaskAwaitablethat is supposed to beawait-ed instead of the task. The task itself is unaffected. I think you are stuck with what you are doing currently (along with all the related risks).