等待杀死进程

Await kills process(等待杀死进程)
本文介绍了等待杀死进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

试试{var clientCredential = new ClientCredential(_clientId, _clientSecret);var authContext = new AuthenticationContext(AuthUri + _tenant);var authResult = 等待 authContext.AcquireTokenAsync(GraphUri,clientCredential);var authString = authResult.CreateAuthorizationHeader();var client = new HttpClient();client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));var request = 新的 HttpRequestMessage{方法 = HttpMethod.Get,RequestUri = _requestUri,};request.Headers.Add("授权", authString);HttpResponseMessage 响应 = 空;等待 client.SendAsync(request).ContinueWith(taskWithMessage =>{响应 = taskWithMessage.Result;});返回等待响应.Content.ReadAsStringAsync();}捕捉(例外前){Console.WriteLine(ex);}

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

如果我用

替换该行

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

执行一直持续到 await client.SendAsync(request).ContinueWith(taskWithMessage... 进程再次被杀死,没有抛出任何异常或任何警告消息或其他东西.p>

更奇怪的是,这段代码在另一个项目中运行得很好,但在这里它就不起作用了.

静态无效 ImportLicence(){InsertToDb();}公共异步无效 InsertoDb(){var x = 等待 GetSP();}公共异步任务<字典<ServicePlanViewModel,列表<ServicePlanViewModel>>>获取SP(){var sp = 等待 MakeRq();}公共异步任务<字符串>生成请求(){var authString = 等待 GetAuth();…………返回等待响应.Content.ReadAsStringAsync();}私有异步任务<字符串>获取授权(){......var authResult = 等待 authContext.AcquireTokenAsync(GraphUri, clientCredential);返回 authResult.CreateAuthorizationHeader();}

解决方案

进程被简单地杀死.没有抛出异常,什么都没有.

我假设您在控制台应用程序中运行它,并且您的顶级代码看起来像这样:

static void Main(){我的方法异步();}

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

在控制台应用程序中使用 async 的一种方法是阻塞 Main 方法.通常,您希望一直异步",但控制台应用程序的 Main 方法是此规则的一个例外:

static void Main() =>MainAsync().GetAwaiter().GetResult();静态异步任务 MainAsync(){//Main 的原始代码,但添加了任何必要的 `await`.等待 MyMethodAsync();}

更新: 不要使用 异步无效;使用 async Task 代替:

静态异步任务 ImportLicenceAsync(){等待 InsertToDbAsync();}公共异步任务 InsertoDbAsync(){var x = 等待 GetSPAsync();}

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

解决方案

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

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

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

How to MOQ an Indexed property(如何最小起订量索引属性)
Mocking generic methods in Moq without specifying T(在 Moq 中模拟泛型方法而不指定 T)
How Moles Isolation framework is implemented?(Moles Isolation 框架是如何实现的?)
Difference between Dependency Injection and Mocking Framework (Ninject vs RhinoMocks or Moq)(依赖注入和模拟框架之间的区别(Ninject vs RhinoMocks 或 Moq))
How to mock Controller.User using moq(如何使用 moq 模拟 Controller.User)
How do I mock a class without an interface?(如何模拟没有接口的类?)