問題描述
我正在嘗試連接到 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模板網!