Michael Schnerring
Michael Schnerring

Reputation: 3661

What about async/await in MVC?

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?

Upvotes: 1

Views: 1733

Answers (1)

Stephen Cleary
Stephen Cleary

Reputation: 457302

I recommend you start the asynchronous loading in Application_Start, and then await for 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 action await it.

// Or in a repository or whatever...
public static class SharedData
{
  public static Task<Dictionary<int, string>> MyDictionary;
}

...

Application_Start(..)
{
  // Start the dictionary filling, but don't wait for it to complete.
  // Note that we're saving the Task, not await'ing it.
  MyDictionary = MyLibrary.GetDictionary();
}

Then in each asynchronous action that needs it:

public async Task<ActionResult> Get()
{
  // Asynchronously wait for the dictionary to load if it hasn't already.
  var dict = await SharedData.MyDictionary;
  ...
  return View();
}

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:

// Or in a repository or whatever...
public static class SharedData
{
  public static AsyncLazy<Dictionary<int, string>> MyDictionary =
      new AsyncLazy<Dictionary<int, string>>(async () =>
      {
        var ret = new Dictionary<int, string>();
        await MyLibrary.FillDictionary(ret);
        return ret;
      });
}

Then in each asynchronous action that needs it:

public async Task<ActionResult> Get()
{
  // Asynchronously wait for the dictionary to load if it hasn't already.
  var dict = await SharedData.MyDictionary;
  ...
  return View();
}

Asynchronous lazy initialization has the capability to be lazy, so the first action needing a specific dictionary will await it. If there are dictionaries that aren't used, then they'll never be loaded.

Upvotes: 3

Related Questions