DfwForSale
DfwForSale

Reputation: 374

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");
    }

Upvotes: 4

Views: 5339

Answers (4)

Mike Gwilt
Mike Gwilt

Reputation: 2449

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.

Upvotes: 3

AaronH
AaronH

Reputation: 109

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

Upvotes: 2

DfwForSale
DfwForSale

Reputation: 374

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);
    }
}

Upvotes: 23

Display Name
Display Name

Reputation: 8128

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.

Upvotes: 0

Related Questions