简体   繁体   中英

Why is this returning System.Threading.Tasks.Task`1[System.Web.Mvc.ActionResult]

Not sure why this is not returning my view after the task is completed and I can find much on Google as to why.

public async Task<ActionResult> GetUserAsync()
    {

        var value = Task.Factory.StartNew(() => _userService.GetUser("ausername"));

        await Task.WhenAll(value);

        return View("GetUser");
    }

Ok, so after too many hours of debugging and pulling what is left of my hair out, I found the culprit. It was my WindsorActionInvoker!! A change from ControllerActionInvoker to AsyncControllerActionInvoker fixed the issue with async Task not working as intended.

I hope this helps someone.

public class WindsorActionInvoker : AsyncControllerActionInvoker
{
    private readonly IKernel _kernel;


    public WindsorActionInvoker(IKernel kernel)
    {
        _kernel = kernel;
    }

    protected override ActionExecutedContext InvokeActionMethodWithFilters(ControllerContext controllerContext,
                                                                           IList<IActionFilter> filters,
                                                                           ActionDescriptor actionDescriptor,
                                                                           IDictionary<string, object> parameters)
    {
        foreach (IActionFilter actionFilter in filters)
        {
            _kernel.InjectProperties(actionFilter);
        }
        return base.InvokeActionMethodWithFilters(controllerContext, filters, actionDescriptor, parameters);
    }
}

Was this project upgraded from a prior version? Check that any libraries you're referencing are not referencing an old version of MVC as a dependency. To fix this exact issue I...

Removed the following from my web.config:

  <dependentAssembly>
    <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
    <bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
  </dependentAssembly>

Ensured targetFramework is .NET 4.5

<compilation debug="true" targetFramework="4.5">

And removed a reference to the library Fluent Filters which was a legacy solution for global filters.

With those changes I was able to return a task from a controller.

I had this exact same problem, I was using .NET framework 4.5 but MVC 3, apparently MVC 3 does not support .NET 4.5 and this was causing the error, after upgrading to MVC 4 I was able to fix the error. I found a package in NuGet called Upgrade MVC 3 To MVC 4 which did wonders for the upgrade however I got an error afterwords. The problem afterwards was the line

<appSettings>
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
<add key="webpages:Version" value="2.0.0.0" />  <-------- This line
<add key="PreserveLoginUrl" value="true" />

in the Web.config after modifying it as instructed in the error thrown

<add key="webpages:Version" value="1.0.0.0" />

everything started tor work properly.

I hope this helps someone, I spent 3 days trying to find an answer myself

It returns a Task, which, when completed, contains the result of computation.

This is C#'s syntactic sugar, and it may look strange until you get used to it. If you want just grab the result without going async (do blocking call), you can write var r = await GetUserAsync(); , and r will be of type ActionResult .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM