等待杀死过程 [英] Await kills process

查看:79
本文介绍了等待杀死过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试连接到Azure AD,并且正在使用此代码.

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

我不明白的一个大问题是,当执行到达第一次等待(var authResult = await authContext.AcquireTokenAsync(GraphUri,clientCredential);)时,进程就被杀死了.没有异常,什么也没有抛出.

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.

如果我用

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

执行一直进行到await client.SendAsync(request).ContinueWith(taskWithMessage...为止,在此再次终止进程,而不会引发任何异常或任何警告消息.

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.

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

推荐答案

该过程被简单地杀死了.没有异常,什么也没有抛出.

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

在这种情况下,main方法实际上将退出,因为它不等待异步代码完成.

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

在控制台应用程序中使用async的一种方法是阻止Main方法.通常,您想一直异步",但是Console应用程序的Main方法是该规则的例外:

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

更新:请勿使用async void ;使用async Task代替:

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

public async Task InsertoDbAsync()
{
  var x = await GetSPAsync();
}

这篇关于等待杀死过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆