异步操作方法返回一个任务,不能被同步执行 [英] The asynchronous action method returns a Task, which cannot be executed synchronously

查看:120
本文介绍了异步操作方法返回一个任务,不能被同步执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这code是我的本地机器上运行良好。

This Code is running fine on my local machine

public async Task<ActionResult> teststuff()
    {

        using (var client = new HttpClient())
        {

            var twitter = await client.GetStringAsync("http://www.twitter.com");
            Response.Write(twitter);
        }

        return View();
    }

但是当我上传到我的错误响应服务器

However when I upload to the server I Get the error response

异步操作方法teststuff返回任务,
  不能同步执行。说明:未处理
  当前Web请求的执行过程中发生异常。
  请检查堆栈跟踪有关该错误和更多信息
  它起源于code。

Server Error in '/' Application.

The asynchronous action method 'teststuff' returns a Task, which cannot be executed synchronously. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

异常详细信息:System.InvalidOperationException:异步
  操作方法teststuff'返回一个任务,它不能执行
  同步。

Exception Details: System.InvalidOperationException: The asynchronous action method 'teststuff' returns a Task, which cannot be executed synchronously.

源错误:

的执行过程中生成了未处理的异常
  当前Web请求。有关的起源和位置信息
  除了可以使用异常堆栈跟踪下面来识别。

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

堆栈跟踪:

[出现InvalidOperationException:异步操作方法teststuff
  返回一个任务,不能被同步执行。]结果
  System.Web.Mvc.Async.TaskAsyncActionDescriptor.Execute(ControllerContext
  controllerContext,IDictionary的 2参数)+125结果
  System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext
  controllerContext,ActionDescriptor actionDescriptor,IDictionary的
2
  参数)+39结果
  System.Web.Mvc&LT;> c__DisplayClass13.b__10()
  + 120 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter
  过滤器,ActionExecutingContext preContext,Func键 1续)637结果
  System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext
  controllerContext,IList的
1的过滤器,ActionDescriptor actionDescriptor,
  IDictionary`2参数)307结果
  System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext
  controllerContext,字符串actionName)688结果
  NotFoundMvc.ActionInvokerWrapper.InvokeActionWith404Catch(ControllerContext
  controllerContext,字符串actionName)+54结果
  NotFoundMvc.ActionInvokerWrapper.InvokeAction(ControllerContext
  controllerContext,字符串actionName)+19结果
  System.Web.Mvc&LT;> c__DisplayClass1d.b__19()+ 40结果
  System.Web.Mvc.Async&LT;> c__DisplayClass1.b__0()+15结果
  System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult的asyncResult)+53
  System.Web.Mvc.Async&LT;> c__DisplayClass4.b__3(IAsyncResult的
  AR)+15结果
  System.Web.Mvc&LT;> c__DisplayClass8.b__3(IAsyncResult的
  asyncResult)+42结果
  System.Web.Mvc.Async&LT;> c__DisplayClass4.b__3(IAsyncResult的
  AR)+15结果
  System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
  606 System.Web.HttpApplication.ExecuteStep(IExecutionStep一步,布尔和放大器; completedSynchronously)+ 288

[InvalidOperationException: The asynchronous action method 'teststuff' returns a Task, which cannot be executed synchronously.]
System.Web.Mvc.Async.TaskAsyncActionDescriptor.Execute(ControllerContext controllerContext, IDictionary2 parameters) +125
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary
2 parameters) +39
System.Web.Mvc.<>c__DisplayClass13.b__10() +120 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func1 continuation) +637
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList
1 filters, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +307
System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +688
NotFoundMvc.ActionInvokerWrapper.InvokeActionWith404Catch(ControllerContext controllerContext, String actionName) +54
NotFoundMvc.ActionInvokerWrapper.InvokeAction(ControllerContext controllerContext, String actionName) +19
System.Web.Mvc.<>c__DisplayClass1d.b__19() +40
System.Web.Mvc.Async.<>c__DisplayClass1.b__0() +15
System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +53 System.Web.Mvc.Async.<>c__DisplayClass4.b__3(IAsyncResult ar) +15
System.Web.Mvc.<>c__DisplayClass8.b__3(IAsyncResult asyncResult) +42
System.Web.Mvc.Async.<>c__DisplayClass4.b__3(IAsyncResult ar) +15
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +606 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +288

----------------------------------------------- ---------------------------------版本信息:Microsoft .NET Framework版本:4.0.30319;
  ASP.NET版本:4.0.30319.17929

-------------------------------------------------------------------------------- Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.17929

有是缺少的东西我的服务器上,但只是无法找出它是任何帮助将是AP preciated。

There is something missing on my server but just can't find out what it is any help would be appreciated.

我曾尝试在服务器上重新安装的.NET Framework 4.5但没有任何效果。但是我做了断绝通知它既有

I have tried re-installing .net framework 4.5 on the server but that had no effect. However I did notice on the sever it has both the

Microsoft .NET框架4多目标包和
Microsoft .NET框架4.5多目标包

Microsoft .NET Framework 4 Multi-Targeting Pack and Microsoft .NET Framework 4.5 Multi-Targeting Pack

不知道我是否应该删除4个一与否?

Not sure if I should remove the 4 one or not?

感谢

推荐答案

好吧很奇怪我只是删除了网站上的所有文件,然后重新发布的网站,它的所有工作正常。

Well it was odd I just deleted all the files in the website then re published the site and it all work fine.

我觉得这可能会发生,因为我最近从MVC3网站MVC4升级的站点,并有可能已被从目录中MVC3现场留下了一个奇怪的DLL。

I think this may have happen because I recently upgraded the site from a MVC3 site to MVC4 and there may have been an odd DLL left from the MVC3 site in the Directory.

感谢斯蒂芬,你建议它给我擦了现场整洁的想法,重新正在增加它

Thanks Stephen for you suggestion it gave me the idea of wiping the site clean and re upping it

这篇关于异步操作方法返回一个任务,不能被同步执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆