Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 异步无位置_C#_Asp.net - Fatal编程技术网

C# 异步无位置

C# 异步无位置,c#,asp.net,C#,Asp.net,我正在Global.asax.cs中的会话_start中重构ASP MVC代码,并对外部服务进行异步调用。我要么在IE中得到一个无休止旋转的白色页面,要么执行立即返回到调用线程。在会话_start()中,当我尝试.Result时,我得到了带有旋转IE图标的白色页面。当我尝试.ContinueWith()时,执行返回到下一行,这取决于异步的结果。因此authResult总是空的。有人能帮忙吗?谢谢 这是来自会话\u Start() 这来自用户\控制器类 public async Task &

我正在Global.asax.cs中的会话_start中重构ASP MVC代码,并对外部服务进行异步调用。我要么在IE中得到一个无休止旋转的白色页面,要么执行立即返回到调用线程。在会话_start()中,当我尝试.Result时,我得到了带有旋转IE图标的白色页面。当我尝试.ContinueWith()时,执行返回到下一行,这取决于异步的结果。因此authResult总是空的。有人能帮忙吗?谢谢

这是来自会话\u Start()

这来自用户\控制器类

   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";
       }
公共异步任务checkUserViaWebApi(字符串networkName){
UserProfile\u thisProfile=等待vhaHelperlib.WebApiBroker.Get(
System.Configuration.ConfigurationManager.AppSettings[“userwebapident”],“User/Profile/”+networkName);
AuthResult authenticationResult=新建AuthResult();
如果(_thisProfile==null)/*没有用户配置文件*/{
authenticationResult.Result=enumAuthenticationResult.NoLSV;
authenticationResult.Controller=“AccessRequest”;
authenticationResult.Action=“lsvinInstruction”;
}
这是使用HttpClient执行实际调用的助手类

         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) {
 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);
      }
publicstaticasync任务Get(stringbaseurl,stringurlsegment)
{
string content=string.Empty;
使用(HttpClient=GetClient(baseUrl))
{
HttpResponseMessage response=await client.GetAsync(urlsgment.TrimStart('/')).ConfigureAwait(false);
if(响应。IsSuccessStatusCode)
{
content=wait response.content.ReadAsStringAsync();
}
返回JsonConvert.DeserializeObject(内容);
}

从会话开始调用用户控制器是没有意义的

如果VHAHelperlib没有任何依赖项,您希望直接在会话启动中调用VHAHelperlib

由于会话启动不是异步的,因此您希望使用结果

var setting = ConfigurationManager.AppSettings["userWebApiEndpoint"];
 UserProfile profile = await VhaHelpersLib.WebApiBroker.Get<UserProfile>(
        setting, "User/Profile/" + networkName).Result;

if (profile == enumAuthenticationResult.Authorized) {
  ...
}
var setting=ConfigurationManager.AppSettings[“userwebapidendpoint”];
UserProfile profile=wait VhaHelpersLib.WebApiBroker.Get(
设置“User/Profile/”+networkName).Result;
if(profile==enumAuthenticationResult.Authorized){
...
}

从会话开始调用用户控制器是没有意义的

如果VHAHelperlib没有任何依赖项,您希望直接在会话启动中调用VHAHelperlib

由于会话启动不是异步的,因此您希望使用结果

var setting = ConfigurationManager.AppSettings["userWebApiEndpoint"];
 UserProfile profile = await VhaHelpersLib.WebApiBroker.Get<UserProfile>(
        setting, "User/Profile/" + networkName).Result;

if (profile == enumAuthenticationResult.Authorized) {
  ...
}
var setting=ConfigurationManager.AppSettings[“userwebapidendpoint”];
UserProfile profile=wait VhaHelpersLib.WebApiBroker.Get(
设置“User/Profile/”+networkName).Result;
if(profile==enumAuthenticationResult.Authorized){
...
}

你可能想看看这个问题:我试过了,但没有成功。看起来你使用async的唯一原因是因为你使用的是
HttpClient
,可以使用而不是异步。你可能想看看这个问题:我试过了,但没有成功。看起来你使用async的唯一原因是因为你正在使用
HttpClient
,use可以改为使用,而不是异步。用户\u控制器具有确定授权级别的业务逻辑。代码片段仅显示逻辑的一小部分。它不是会话\u start()问题。此外,您建议的等待将不会编译,因为这将要求会话_start()的签名异步更改。您是否在结束时使用了结果?基本上,结果将一直阻止,直到任务完成。例如,在您最初的问题中,
var userProfileTask=uc.checkUserViaWebApi(networkLogin[userLoginIdx]).Result;
谢谢。它成功了,我将重要的业务逻辑保留在user\u controller类中。user\u controller具有确定授权级别的业务逻辑。代码片段仅显示逻辑的一小部分。它不是会话\u start()问题。此外,您建议的等待将不会编译,因为这将要求会话_start()的签名异步更改。您是否在结束时使用了结果?基本上,结果将一直阻止,直到任务完成。例如,在您最初的问题中,
var userProfileTask=uc.checkUserViaWebApi(networkLogin[userLoginIdx]).Result;
谢谢。它成功了,我将强大的业务逻辑留在了user\u controller类中。