1

I'm using async methods in my global.asax file, and am experiencing a problem.

Even though the async methods are working as they should, when I fire them in Application_Start() I get 404 errors when my app starts, until it's done, when it shouldn't complete until it's done.

Here's the code in my global.asax:

public class MvcApplication : System.Web.HttpApplication
{
    protected async void Application_Start()
    {
        var Init = new Init();
        await Init.LoadAsync();

        AreaRegistration.RegisterAllAreas();
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
    }
}

I'm thinking since Application_Start() had to be made async to allow an await command, the Application_Start() function itself is not being awaited when the app starts. Therefore, it's necessary to find out either how to await the Application_Start() function or another way to fire the method without having to make Application_Start() an async method (The latter would be preferred).

Control Freak
  • 12,965
  • 30
  • 94
  • 145

1 Answers1

1

ASP.NET does not seem to allow aysnc for Application_Start. However, you can use async for BeginRequest by using HttpApplication.AddOnBeginRequestAsync(BeginEventHandler, EndEventHandler). Have a flag to indicate whether your Init.LoadAsync has been called so that you will only trigger the initialization on the very first request.

Then set up Application Initialization Module to create that first request.

http://msdn.microsoft.com/en-us/library/System.Web.HttpApplication_methods%28v=vs.110%29.aspx

Jeow Li Huan
  • 3,758
  • 1
  • 34
  • 52
  • They allow it alright, just can't override it's caller maybe. I was actually able to remove `async` and `await` and even though I get a warning, it still runs. – Control Freak Oct 18 '14 at 04:10
  • The problem with not awaiting the LoadAsync is that request can come in while your LoadAsync is still running, thus that request might run into uninitialized states. I have to verify if Application Initialization Module does block real requests before it completes though – Jeow Li Huan Oct 18 '14 at 04:15
  • I don't see how it is a problem for concurrent requests--My understanding is when you don't `await` it runs in sync – Control Freak Oct 18 '14 at 04:17
  • Well I was wrong, I'm getting errors now on uninitialized objects. – Control Freak Oct 18 '14 at 04:19