Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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");
    }
like image 264
DfwForSale Avatar asked Nov 27 '22 17:11

DfwForSale


2 Answers

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);
    }
}
like image 196
DfwForSale Avatar answered Dec 06 '22 03:12

DfwForSale


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.

like image 23
Mike Gwilt Avatar answered Dec 06 '22 04:12

Mike Gwilt