Fix “An Asynchronous Operation Cannot Be Started at This Time” in ASP.NET MVC 5

Quick fix. In a controller action, return Task<ActionResult> and await the operation before returning the view. If the failing code is in a filter, module, layout, or event callback, identify which request stage actually started the asynchronous work.

This guide applies to ASP.NET MVC 5 on .NET Framework (System.Web.Mvc).

Match the exception to the request stage

ASP.NET can report this error when a synchronous request stage starts work that the pipeline cannot track. A related message can say that a module or handler finished while asynchronous work was pending. Check the stack trace and look for async void, fire-and-forget calls, or asynchronous work initiated by a synchronous callback. Add a breakpoint at the first such boundary.

Keep the asynchronous chain visible to MVC

This sample assumes an existing catalog service whose GetFeaturedAsync() method returns a Task carrying the view model. Move request-critical I/O into a task-returning service and await it from the action. If a helper has no result, have it return Task so its caller can still await completion.

using System.Threading.Tasks;
using System.Web.Mvc;

// Inside CatalogController
[HttpGet]
public async Task<ActionResult> Featured()
{
    var products = await catalog.GetFeaturedAsync();
    return View(products);
}

Avoid hiding unfinished work

A Task.Run wrapper or a discarded task does not make request-dependent work reliable. Do not start I/O from a synchronous Razor fragment and assume MVC will wait. Load the needed data before rendering the view. For a notification or export that should continue beyond the request, use a durable queue and worker that can track completion independently of the IIS request.

Test success, delay, and failure

Run the endpoint with a fast service and with a deliberately delayed development stub. Verify the page renders only after the result arrives. Make the stub fail and confirm the normal request error handling sees the exception. Check the application’s .NET Framework and MVC configuration if the exception persists with a fully awaited action.

Related reading

Comments