3

I have a method which calls database as shown below:

BL Method to call DAO method:

 public async Task<List<Classes>> GetClassesAndAddRules(string classId) { var classData = await Task.Run( () => _daoClass.GetClasses(classId)); //logic for adding rule //.................................. } 

DatabaseCall in DAO:

 //*below method takes 1 second approx to return* public List<Classes> GetClasses(string id) { var retVal = new List<Classes>(); using (var context = new test_db_context()) { var rows = context.GetClassesById(id); foreach (ClassesDBComplexType row in rows) { retVal.Add(Mapper.Map<GetClassesByClassIdOut>(row)); } } return retVal; } 

Is there any performance boost just my calling the DAO method using await ?

My understanding is GetClasses() will be called on a separate thread so that it doesn't block and continue processing other stuff.

Any help is appreciated.

1 Answer 1

6

The code you posted won't compile. From the title of your question, I'm assuming that your call actually looks like await Task.Run(() => _daoClass.GetClasses(classId));

In that case, the use of Task.Run will make a difference in performance: it will be worse.

The point of async on the server side is to free up the request thread instead of blocking it. What you're doing with await Task.Run(...) is to free up the request thread by starting work on another thread. In other words, the Task.Run code has the same amount of work to do plus thread marshaling.

Sign up to request clarification or add additional context in comments.

6 Comments

Hi Stephen, I have updated the post. Thanks for pointing the error in code. Could you please suggest how can I truly achieve async or parallel processing for database calls ? The database call is really expensive in my case, since it takes approx 1 sec to get the data.
@SanjaySutar: The amount of time it takes is immaterial. You should just call it directly: var classData = _daoClass.GetClasses(classId);. If you wrap it in a Task.Run and await it, it will still take 1 second...
You are absolutely correct. But then there should be some way to make it async. I don't want to block the thread for 1 sec, where the same thread can be used for something else. That's what most of the blogs mentioned.
Most non-Microsoft information about async assumes a UI application (e.g., WinForms, WPF, etc). This is on the server side (ASP.NET MVC). On a UI application, this makes sense because you're freeing up the UI thread (a scarce and important resource) by pushing work to a threadpool thread. On the server side, this argument doesn't make sense because you're freeing up one threadpool thread by pushing work to another threadpool thread. So you don't gain anything.
Makes sense to me...How about making it async as mentioned by Stephen Toub here : blogs.msdn.com/b/pfxteam/archive/2012/03/24/10287244.aspx
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.