我在Global.asax.cs中的session_start中使用异步调用外部服务来重构我的ASP MVC代码。我要么在IE中获得无限旋转的白页,要么立即执行返回到调用线程。在Session_start()中,当我尝试.Result时,我得到了带有旋转IE图标的白页。当我尝试.ContinueWith()时,执行返回到依赖于异步结果的下一行。因此,authResult始终为空。有人可以帮忙吗?谢谢。async getting no
这是从在session_start()
if (Session["userProfile"] == null) {
//call into an async method
//authResult = uc.checkUserViaWebApi(networkLogin[userLoginIdx]).Result;
var userProfileTask = uc.checkUserViaWebApi(networkLogin[userLoginIdx])
.ContinueWith(result => {
if (result.IsCompleted) {
authResult = result.Result;
}
});
Task.WhenAll(userProfileTask);
if (authResult.Result == enumAuthenticationResult.Authorized) {
这是User_Controller类
public async Task <AuthResult> checkUserViaWebApi(string networkName) {
UserProfile _thisProfile = await VhaHelpersLib.WebApiBroker.Get <UserProfile> (
System.Configuration.ConfigurationManager.AppSettings["userWebApiEndpoint"], "User/Profile/" + networkName);
AuthResult authenticationResult = new AuthResult();
if (_thisProfile == null) /*no user profile*/ {
authenticationResult.Result = enumAuthenticationResult.NoLSV;
authenticationResult.Controller = "AccessRequest";
authenticationResult.Action = "LSVInstruction";
}
这是助手的类,它使用的HttpClient
实际调用public static async Task<T> Get<T>(string baseUrl, string urlSegment)
{
string content = string.Empty;
using(HttpClient client = GetClient(baseUrl))
{
HttpResponseMessage response = await client.GetAsync(urlSegment.TrimStart('/')).ConfigureAwait(false);
if(response.IsSuccessStatusCode)
{
content = await response.Content.ReadAsStringAsync();
}
return JsonConvert.DeserializeObject<T>(content);
}
你可能想看看这个问题:http://stackoverflow.com/questions/15167243/session-issue-when-having-async-session-start-method –
我试过了,但事实并非如此工作。 – user266909
它看起来像你使用异步的唯一原因是因为你正在使用'HttpClient',可以使用['WebClient.DownloadString'](https://msdn.microsoft.com/en-us/library/fhd1f0sw(v = vs.110).aspx),而不是异步。 –