Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.0k views
in Technique[技术] by (71.8m points)

asp.net mvc 4 - The asynchronous action method returns a Task, which cannot be executed synchronously

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

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.

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

Source Error:

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.

Stack Trace:

[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

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

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

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 Framework 4 Multi-Targeting Pack and Microsoft .NET Framework 4.5 Multi-Targeting Pack

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

Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I had this error after subclassing ControllerActionInvoker in order to inject dependencies into MVC attributes using Ninject. This page describes the process of doing this: http://codeclimber.net.nz/archive/2009/02/10/how-to-use-ninject-to-inject-dependencies-into-asp.net-mvc.aspx

I had some asynchronous methods in my controller, and this error was appearing when these asynchronous methods were called. It turns out that I needed to not subclass ControllerActionInvoker, but System.Web.Mvc.Async.AsyncControllerActionInvoker.

After changing the base class, my code was happy and it ran just fine.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...