EntityFramework (6) and async ( waitingForActivation)?

.net-4.5 async-await c# entity-framework entity-framework-6


In order to use EF6, I downloaded it.async )

I therefore created the following straightforward procedure:

  public async Task<List<int>> MyasyncMethod()
          var locations = await MyDumpEntities.AgeGroups.Select(f=>f.endYear).ToListAsync();
          return locations;


  DumpEntities1 MyDumpEntities = new DumpEntities1();
  var data = MyDumpEntities.AgeGroups.ToListAsync();
  MyasyncMethod().ContinueWith(s => { Response.Write("f"); });

However, when I study the screen, I don't see anything.data As I see it

enter image description here

Aside: This is theToListAsync signatureenter image description here

What am I overlooking?

7/11/2013 7:12:16 AM

Accepted Answer

Using the comments and the line that bothers you as a guide:

var data = MyDumpEntities.AgeGroups.ToListAsync();

How willdata kind be?Task<List<AgeGroup>> . Yes, that's accurate.List<AgeGroup> Therefore, you must either mark thePage_Load async (if at all possible)

public async void Page_Load(object sender, EventArgs e)
    using(var MyDumpEntities = new DumpEntities1())
       var data = await MyDumpEntities.AgeGroups.ToListAsync();

or find a way to delay until the execution (continuation, blocking wait).

Another issue, but since you're using continuation for the second call, I would be very careful about disposing of the context outside of continuation (someone else might want to correct me if I'm wrong on that). It could be that you eliminate the context beforehand. Although you don't use the context in this specific instance, it seems fishy.

therefore I would either

MyasyncMethod().ContinueWith(s => { Response.Write("f"); MyDumpEntities.Dispose();});

or simply useasync also there

var result = await MyasyncMethod();

plus addAsync="True" to the page command

7/11/2013 7:29:31 AM

Popular Answer

Others have noted that the ideal response is to useawait I can't think of a good explanation for why, though.

The original code is incorrect for two reasons. In the beginning, you useContinueWith in an ASP.NET application without capturing the context, thus the continuation (theResponse.Write call) has no response to write to because it lacks a request context.

await takes care of this for you by gathering the background information prior to theawait and continuing with the remainder of the process utilizing that; in this instance, it will capture anAspNetSynchronizationContext displaying the most recent request/response.

The concurrent execution of asynchronous code is the second justification. So,MyasyncMethod will start to run, come to itsawait and hand off an unfinished project toPage_Load . Page_Load then adds a continuation to that task before disposing of the context by continues to work. Therefore, the context could be removed while theToListAsync There is still an open request.

await also fixes this for you as it "pauses" thePage_Load using tillMyasyncMethod is finished.

As a last remark, keep in mind these things when usingasync using ASP.NET

  1. You must use.NET 4.5 as your target version.Microsoft.Bcl.Async .
  2. You must decidetargetFramework Zzz-90-Zzz set to 4.5UseTaskFriendlySynchronizationContext a fact.
  3. (Only WebForms) SetPage.Async a fact.
  4. Think about usingRegisterAsyncTask in place ofawait I typically favorawait due to alternative approaches' greater issue separation, while the ASP.NET team favorsRegisterAsyncTask as there is only one "synchronize" point followingPreRender where the runtime waits until each operation is finished.
  5. Include timeouts for your own requests. The standard timeouts that are inherent to synchronous ASP.NET requests are not automatically used by asynchronous ASP.NET requests. There are two possibilities
    • Apply theHttpRequest.TimedOut token for cancellation.
    • (WebForms/RegisterAsyncTask only) By specifying an asynchronous timeout, you can add one.Page.AsyncTimeout as well as yourasync method take aCancellationToken .

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow