[SOLVED] How to call some async code in an ASP.NET application_start

Table of Contents

Issue

In our application_startup, we seed up our database with some fake data, if no data exists.

To do this, we’re using the Async methods to store the data. Great. Only problem is, we’re not sure how to do this in the application_startup because that’s not an async method.

I’ve spent soooo much time trying to understand @StevenCleary’s tutorials and I’m always getting deadlocks. I totally grok what he consistently says:

As a general rule, you should use "async all the way down"; that is, don’t block on async code

but I just don’t get how I can do that, in this case 🙁

Lets imagine this is the code I’m trying to play with…

protected void Application_Start()
{
    var someFakeData = LoadSomeFakeData();
    var documentStore = new DocumentStore();
    await documentStore.InitializeAsync(someFakeData);

    ...

    // Registers this database as a singleton.
    Container.Register(documentStore);
}

and later on .. some code that uses this documentStore. It is injected via construction injection …

public SomeController(IDocumentStore documentStore)
{
    _documentStore = documentStore;
}

public ViewModel GetFoos()
{
    using (var session = _documentStore.OpenSession())
    {
        ... db code goes in here ... 
    }
}

Clarification

I’m not trying to do some async code in here. I’m actually trying to call this async method, synchronously. Sure, i loose the benefits of async blah blah de blah.. but i’m happy with that. This is start up and I’m happy to block on startup.

Solution

In this case, you’re asynchronously initializing a shared resource. So, I recommend that you either save the Task itself, or introduce an asynchronous wrapper type.

Using Task:

protected void Application_Start()
{
  var someFakeData = LoadSomeFakeData();
  var documentStore = new DocumentStore();
  var documentStoreTask = documentStore.InitializeAsync(someFakeData);

  ...

  // Registers this database task as a singleton.
  Container.Register(documentStoreTask);
}

That may be too awkward, though, depending on Container. In that case, you can introduce an asynchronous wrapper type:

public sealed class DocumentStoreWrapper
{
  private readonly Task<DocumentStore> _documentStore;

  public DocumentStoreWrapper(Data data)
  {
    _documentStore = CreateDocumentStoreAsync(data);
  }

  private static async Task<DocumentStore> CreateDocumentStoreAsync(Data data)
  {
    var result = new DocumentStore();
    await documentStore.InitializeAsync(data);
    ...
    return result;
  }

  public Task<DocumentStore> DocumentStoreTask { get { return _documentStore; } }
}

protected void Application_Start()
{
  var someFakeData = LoadSomeFakeData();
  var documentStoreWrapper = new DocumentStoreWrapper(someFakeData);

  ...

  // Registers this database wrapper as a singleton.
  Container.Register(documentStoreWrapper);
}

Or, you could use AsyncLazy<T>, which does much the same thing but uses a background thread to execute the initialization code.

Answered By – Stephen Cleary

Answer Checked By – Dawn Plyler (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *