Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Await kills process

I am trying to connect to Azure AD and I am using this code.

try
{
    var clientCredential = new ClientCredential(_clientId, _clientSecret);
    var authContext = new AuthenticationContext(AuthUri + _tenant);
    var authResult = await authContext.AcquireTokenAsync(GraphUri,clientCredential);
    var authString = authResult.CreateAuthorizationHeader();
    var client = new HttpClient();
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    var request = new HttpRequestMessage
    {
        Method = HttpMethod.Get,
        RequestUri = _requestUri,
    };
    request.Headers.Add("Authorization", authString);
    HttpResponseMessage response = null;
    await client.SendAsync(request).ContinueWith(taskWithMessage =>
    {
        response = taskWithMessage.Result;
    });
    return await response.Content.ReadAsStringAsync();
}
catch (Exception ex)
{
   Console.WriteLine(ex);
}

The big problem that I don't understand is that when the execution reaches the first await (var authResult = await authContext.AcquireTokenAsync(GraphUri,clientCredential);) the process is simply killed. No exception is thrown, nothing.

If I replace that line with

var authResult = authContext.AcquireTokenAsync(GraphUri,clientCredential); 
var authString = authResult.Result.CreateAuthorizationHeader();

the execution goes on until await client.SendAsync(request).ContinueWith(taskWithMessage... where the process is killed again without any exception being thrown or any message of warning or something.

The even weirder thing is that this code runs just fine in another project but here it just wont work.

Edit:

static void ImportLicence()
{
   InsertToDb();
}

public async void InsertoDb()
{
   var x = await GetSP();
}

public async Task<Dictionary<ServicePlanViewModel, List<ServicePlanViewModel>>> GetSP()
{
   var sp = await MakeRq();
}

public async Task<string> MakeRequest()
{
   var authString = await GetAuth();
   ..........
   return await response.Content.ReadAsStringAsync();
}

private async Task<string> GetAuth()
{
   .....
   var authResult = await authContext.AcquireTokenAsync(GraphUri, clientCredential);
   return authResult.CreateAuthorizationHeader();
}
like image 331
viktorfilim Avatar asked Jan 05 '23 15:01

viktorfilim


1 Answers

the process is simply killed. No exception is thrown, nothing.

I assume that you are running this in a Console application, and that your top-level code would look something like this:

static void Main()
{
  MyMethodAsync();
}

In which case, the main method would in fact exit, since it is not waiting for your asynchronous code to complete.

One way to work with async in Console applications is to block in the Main method. Normally, you want to go "async all the way", but a Console app's Main method is an exception to this rule:

static void Main() => MainAsync().GetAwaiter().GetResult();
static async Task MainAsync()
{
  // Original code from Main, but adding any necessary `await`s.
  await MyMethodAsync();
}

Update: Don't use async void; use async Task instead:

static async Task ImportLicenceAsync()
{
  await InsertToDbAsync();
}

public async Task InsertoDbAsync()
{
  var x = await GetSPAsync();
}
like image 60
Stephen Cleary Avatar answered Jan 15 '23 20:01

Stephen Cleary