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