9

I have a synchronous HttpModule that contains the following code.

    /// <summary>
    /// Occurs as the first event in the HTTP pipeline chain of execution 
    /// when ASP.NET responds to a request.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">An <see cref="T:System.EventArgs">EventArgs</see> that 
    /// contains the event data.</param>
    private async void ContextBeginRequest(object sender, EventArgs e)
    {
        HttpContext context = ((HttpApplication)sender).Context;
        await this.ProcessImageAsync(context);
    }

When I try to run the module from an empty MVC4 application (NET 4.5) I get the following error.

An asynchronous operation cannot be started at this time. Asynchronous operations may only be started within an asynchronous handler or module or during certain events in the Page lifecycle. If this exception occurred while executing a Page, ensure that the Page is marked <%@ Page Async="true" %>.

I'm missing something it seems but by my reading that the error shouldn't actually occur.

I've had a dig around but I cannot seem to find anything to help, does anyone have any ideas?

James South
  • 10,147
  • 4
  • 59
  • 115

1 Answers1

18

So you have asynchronous code in a synchronous HttpModule event handler, and ASP.NET throws an exception indicating that asynchronous operations may only be started within an asynchronous handler/module. Seems pretty straightforward to me.

To fix this, you should not subscribe to BeginRequest directly; instead, create a Task-returning "handler", wrap it in EventHandlerTaskAsyncHelper, and pass it to AddOnBeginRequestAsync.

Something like this:

private async Task ContextBeginRequest(object sender, EventArgs e)
{
  HttpContext context = ((HttpApplication)sender).Context;
  await ProcessImageAsync(context);

  // Side note; if all you're doing is awaiting a single task at the end of an async method,
  //  then you can just remove the "async" and replace "await" with "return".
}

and to subscribe:

var wrapper = new EventHandlerTaskAsyncHelper(ContextBeginRequest);
application.AddOnBeginRequestAsync(wrapper.BeginEventHandler, wrapper.EndEventHandler);
Stephen Cleary
  • 437,863
  • 77
  • 675
  • 810
  • This must be a few times you've helped me now, Thanks! I should have said sorry, The code is actually written in Net 4.0 and uses the BCL libraries to support the async keyword so I could support both 4.0 and 4.5. As such EventHandlerTaskAsyncHelper isn't available to me. – James South Jul 25 '13 at 18:20
  • 2
    Ah, unfortunately, `Microsoft.Bcl.Async` has [undefined behavior on ASP.NET 4.0](http://blogs.msdn.com/b/webdev/archive/2012/11/19/all-about-httpruntime-targetframework.aspx). On ASP.NET, you *have* to run on .NET 4.5, not .NET 4.0. – Stephen Cleary Jul 25 '13 at 18:26
  • Ah ok... So, testing this if I remove the `targetFramework="4.5"` from the `` node in the web.config this works. Does that mean that in NET 4.0 my httpModule is not, in fact, asynchronous? Does that also mean that I cannot use an asynchronous httpModule without `EventHandlerTaskAsyncHelper` with MVC or is there another way. I'm still a shade confused that it is throwing an error to be honest. The message seems to say that httpModules are ok. – James South Jul 25 '13 at 18:55
  • 1
    I believe you're defining "works" as "does not throw an exception". Take another look at the blog post: if you are writing ASP.NET on .NET 4.0, then you *cannot* use `async` (`Microsoft.Bcl.Async` does *not* reverse this). Some core pieces of ASP.NET were rewritten in .NET 4.5 to support `async` properly (in particular the `SynchronizationContext`), which are simply not available on .NET 4.0. – Stephen Cleary Jul 25 '13 at 19:05
  • I think I've been looking at this all the wrong way. BCL simply allows compilation with the async keyword to then but ignores it's behaviour then? So theoretically I should be able to build it in Net 4.5, use conditional #if statements to wrap your NET 4.5 sample and support NET 4.0 With my existing code? – James South Jul 25 '13 at 20:25
  • 1
    BCL provides the core (.NET) behavior, but ASP.NET has its own behavior that it needs (and BCL doesn't provide). So `Microsoft.Bcl.Async` will bring full `async` support to .NET 4.0 desktop apps, but not ASP.NET apps. If you need to support both, then compile-time conditionals are one approach. – Stephen Cleary Jul 25 '13 at 20:34