Lara
Lara

Reputation: 3021

await is stopping the execution of the program

I have a simple method which uses await.But the problem is that as soon as i reaches to the line the execution of the program is getting stopped , i dont know why.Here is my code..

public async void GetUserdetails() {

var upn = "Custom Name";
var userLookupTask = activeDirectoryClient.Users.Where(
            user => user.UserPrincipalName.Equals(
                upn, StringComparison.CurrentCultureIgnoreCase)).ExecuteSingleAsync();

User userJohnDoe = (User)await userLookupTask;
Console.WriteLine(userJohnDoe.UserPrincipalName);
Console.WriteLine(userJohnDoe.DisplayName);
Console.ReadLine();
}

Any ways to get the execution running and see the values on Console.Please help.Thanks..

Upvotes: 2

Views: 2473

Answers (1)

Stephen Cleary
Stephen Cleary

Reputation: 456747

I suspect your Console app is simply exiting.

To prevent your Console app from exiting, you should return a Task from GetUserdetails and call Wait on that task:

static void Main()
{
  GetUserDetailsAsync().Wait();
}

static async Task GetUserDetailsAsync()
{
  ...
}

Note that Wait is not normally used in asynchronous programming; it is usually only used once in the Main method of Console apps.

Alternatively, you can move the ReadLine into your Main method.

Upvotes: 9

Related Questions