the rest of our team and me created a Silverlight application. We’ve been using .NET 4.0. Though we already put the async/await pattern into our application (Microsoft.CompilerServices.AsyncTargetingPack.Net4).
Now that MS won’t put too much additional effort into Silverlight, we just thought, that we build a little HTML5/JS application – just for experimenting issues.
So… I tried to bootstrap the application, like we did in the previous SL app. I’m using a class library, which is used in our main application, to provide all the data. To fill all the dictionaries in our context, we call some asynchronous methods, to retrieve the data from the DB. I’d like to use this lib in the MVC app, either, but I can’t call those async methods, because Application_Start in Global.asax isn’t async.
How to achieve this?
Alternating the library isn’t a viable solution, because it’s used in production code. Writing a new lib won’t be a solution, too, because we’d really like to keep maintenance as small as possible.
Any suggestions?
I recommend you start the asynchronous loading in
Application_Start, and thenawaitfor it to complete in your MVC actions.You can do this easily by saving the
Task<Dictionary<..>>into a static variable and then having each asynchronous actionawaitit.Then in each asynchronous action that needs it:
Alternatively, you can use asynchronous lazy initialization for this. Asynchronous lazy initialization was first publicized by Stephen Toub; I documented the code and upgraded it to .NET 4.5 on my blog, and recently added it to my AsyncEx library.
Using
AsyncLazy<T>, your code would look like this:Then in each asynchronous action that needs it:
Asynchronous lazy initialization has the capability to be lazy, so the first action needing a specific dictionary will
awaitit. If there are dictionaries that aren’t used, then they’ll never be loaded.