Elisabeth
Elisabeth

Reputation: 21206

async datareader and the order of execution

I am using Web API. I know I have to use a full async stack to get its benefits. That means the API Controller are async calls to my service and my service does async calls to my dataprovider which do async datareader for example.

I guess when I start with using async in the controller I also should do an OpenAsync() on a database connection and use only the async Non-/Query calls.

  1. Is this true?

  2. When I have 3 NonQueries (A) doing stuff on Table 1,2,3. When this task is finished there MUST follow 4 NonQueries (B) doing stuff on Table 1,2,3.

Can I make use of the ExecuteNonQueryAsync method for A OR do I have to fear that B will be faster executed than A which might cause inconsistent data in my case?

Upvotes: 1

Views: 148

Answers (2)

i3arnon
i3arnon

Reputation: 116518

  1. You don't have to use only async methods, but I recommend you mostly do when there's an async option. But make sure those operations are truly async.
  2. If you await all your async calls than the order will be the same as if you didn't use async. There's a difference between parallel and async. An async flow isn't necessarily a parallel one.

Upvotes: 3

riezebosch
riezebosch

Reputation: 2018

1) False, not necessarily 2) Doesn't apply then.

By marking your actions async the apppool thread will be released for new requests and the current request is handled by a background thread.

Upvotes: 1

Related Questions