久久久久久久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 應用程序在通過管理門戶更新之前無法運行)
主站蜘蛛池模板: 国产98色在线 | 日韩 | 成年人网站免费 | 欧美 日韩 在线播放 | 国产成人精品综合 | 国产视频中文字幕 | 国产精品久久欧美久久一区 | 在线亚洲免费视频 | 国产精品久久久久久久免费大片 | 日本高清中文字幕 | 中文字幕免费视频 | 韩国精品一区二区三区 | 久青草影院 | 日日夜夜精品视频 | www视频在线观看 | 国产欧美一级 | 蜜桃精品视频在线 | 99精品国产一区二区青青牛奶 | 精品在线一区二区 | 婷婷开心激情综合五月天 | 国产精品无码久久久久 | 亚洲激情一级片 | 在线免费看黄 | 中文字幕一区二区三区四区五区 | 999热精品| 久久婷婷国产 | 亚洲欧美一区在线 | 天天影视色综合 | 国产在线精品一区二区 | 羞羞色视频 | 国产精品一区二区不卡 | 日韩黄色小视频 | 亚洲欧美日韩精品久久亚洲区 | 国产成人影院 | av中文字幕在线观看 | 黄a免费网络| 99久久精品国产一区二区三区 | 老牛影视av一区二区在线观看 | 国产99久久精品一区二区永久免费 | 天堂在线1| 国产精品成人久久久久a级 久久蜜桃av一区二区天堂 | 免费黄色大片 |