Await kills process

后端 未结 2 1880
一生所求
一生所求 2021-01-19 07:46

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

try
{
    var clientCredential = new ClientCredential(_clientId, _clientSecret);
    var authCon         


        
2条回答
  •  走了就别回头了
    2021-01-19 08:22

    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();
    }
    

提交回复
热议问题