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

Pure.Krome

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.

Stephen Cleary

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to code ASP.NET WebMethod for Ajax Call

From Dev

How to call confirm message from code behind in asp.net?

From Dev

how to reuse a method in some other code-behind in asp.net?

From Dev

how to call high chart in asp.net

From Dev

How async / await can help in ASP.Net application?

From Dev

ASP.NET and async - how it works?

From Dev

Application_Start equivalent in ASP.NET 5

From Dev

ASP.NET call hangs on async await

From Dev

Call async method from ASP.NET validator handler

From Dev

How to call unsafe code from ASP.NET xproj

From Dev

How to run a method in ASP.net MVC only once when application Load Without calling it from Application_Start()

From Dev

How do I call a JavaScript function from Asp.Net Code Behind?

From Dev

How to call async Task using the .Net framework v3.5

From Dev

how to wait for async results in second async call in asp web api

From Dev

ASP.net OnClientClick has an async call - how to proceed with server-side OnClick only when that async call is complete and successful?

From Dev

How can I disable some APIs of my ASP.NET application

From Dev

ASP.NET MVC3 how to wait for async call back and return result

From Dev

How do I convert some .Vb code to .CS code for asp.net

From Dev

How to call confirm message from code behind in asp.net?

From Dev

How to call JS function in asp.net

From Dev

how to call high chart in asp.net

From Dev

How to call an asp.net web application via command line?

From Dev

ASP.NET call hangs on async await

From Dev

ASP.NET how to call this stored procedure

From Dev

Call TemplateField DropDownList in asp.net - code behind with FindControl()

From Dev

How to call web url from controller code asp.net mvc Core

From Dev

call on class click function in asp.net code behind

From Dev

Asp.Net - How to call ActionResult with ajax

From Dev

Asp.Net MVC application Property is always null after Application_Start() is called

Related Related

  1. 1

    How to code ASP.NET WebMethod for Ajax Call

  2. 2

    How to call confirm message from code behind in asp.net?

  3. 3

    how to reuse a method in some other code-behind in asp.net?

  4. 4

    how to call high chart in asp.net

  5. 5

    How async / await can help in ASP.Net application?

  6. 6

    ASP.NET and async - how it works?

  7. 7

    Application_Start equivalent in ASP.NET 5

  8. 8

    ASP.NET call hangs on async await

  9. 9

    Call async method from ASP.NET validator handler

  10. 10

    How to call unsafe code from ASP.NET xproj

  11. 11

    How to run a method in ASP.net MVC only once when application Load Without calling it from Application_Start()

  12. 12

    How do I call a JavaScript function from Asp.Net Code Behind?

  13. 13

    How to call async Task using the .Net framework v3.5

  14. 14

    how to wait for async results in second async call in asp web api

  15. 15

    ASP.net OnClientClick has an async call - how to proceed with server-side OnClick only when that async call is complete and successful?

  16. 16

    How can I disable some APIs of my ASP.NET application

  17. 17

    ASP.NET MVC3 how to wait for async call back and return result

  18. 18

    How do I convert some .Vb code to .CS code for asp.net

  19. 19

    How to call confirm message from code behind in asp.net?

  20. 20

    How to call JS function in asp.net

  21. 21

    how to call high chart in asp.net

  22. 22

    How to call an asp.net web application via command line?

  23. 23

    ASP.NET call hangs on async await

  24. 24

    ASP.NET how to call this stored procedure

  25. 25

    Call TemplateField DropDownList in asp.net - code behind with FindControl()

  26. 26

    How to call web url from controller code asp.net mvc Core

  27. 27

    call on class click function in asp.net code behind

  28. 28

    Asp.Net - How to call ActionResult with ajax

  29. 29

    Asp.Net MVC application Property is always null after Application_Start() is called

HotTag

Archive