久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

等待殺死進程

Await kills process(等待殺死進程)
本文介紹了等待殺死進程的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我正在嘗試連接到 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();
}

這篇關于等待殺死進程的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

ASP.NET Core authenticating with Azure Active Directory and persisting custom Claims across requests(ASP.NET Core 使用 Azure Active Directory 進行身份驗證并跨請求保留自定義聲明)
ASP.NET Core 2.0 Web API Azure Ad v2 Token Authorization not working(ASP.NET Core 2.0 Web API Azure Ad v2 令牌授權不起作用)
How do I get Azure AD OAuth2 Access Token and Refresh token for Daemon or Server to C# ASP.NET Web API(如何獲取守護進程或服務器到 C# ASP.NET Web API 的 Azure AD OAuth2 訪問令牌和刷新令牌) - IT屋-程序員軟件開發技
Azure KeyVault Active Directory AcquireTokenAsync timeout when called asynchronously(異步調用時 Azure KeyVault Active Directory AcquireTokenAsync 超時)
Getting access token using email address and app password from oauth2/token(使用電子郵件地址和應用程序密碼從 oauth2/token 獲取訪問令牌)
New Azure AD application doesn#39;t work until updated through management portal(新的 Azure AD 應用程序在通過管理門戶更新之前無法運行)
主站蜘蛛池模板: 本道综合精品 | 午夜精品久久久久久久星辰影院 | 九九久久国产精品 | 欧美一区2区三区4区公司 | 欧美一区免费在线观看 | 狠狠躁夜夜躁人人爽天天高潮 | 久久99精品久久久久久琪琪 | 国产成人免费视频网站高清观看视频 | 久久国产精品一区二区三区 | 欧美精品日韩精品国产精品 | www免费视频 | 男女羞羞视频网站 | 黄色三级在线播放 | 99这里只有精品视频 | 7777精品伊人久久精品影视 | av一区二区三区 | 精品综合网| 欧美激情国产日韩精品一区18 | 国产综合精品一区二区三区 | 午夜精品一区 | 国产偷录视频叫床高潮对白 | 欧美成人免费在线 | 国产精品99久久久久久www | 日韩av在线一区 | 91精品国产色综合久久不卡98口 | 久久在线视频 | 中文av网站| 热99在线 | 7777精品伊人久久精品影视 | 中文字幕免费视频 | 视频一区在线观看 | 天天综合久久 | 99re国产视频 | 中文字幕一二三 | 精品国产一级 | 农村黄性色生活片 | 欧美一区二区三区精品免费 | 亚洲成人精品一区二区 | 日本激情视频网 | 在线亚洲免费视频 | 久久久久久免费精品一区二区三区 |