Michael
Michael

Reputation: 316

What is the benefit of putting async on a WebAPI controller action that performs no await operations?

I was recently asked an interview question for .NET WebAPI and it went like this...

Interviewer:

Is there any benefit to adding async to the method signature of a WebAPI controller action that does not perform any async operations?

My answer:

No, because there is no blocking code in the method which means the controller action should run synchronously in its own thread from the thread Pool. Is that correct?

Interviewer:

No, it's actually not.

I've searched the web for about 15-20 minutes to find a conclusive answer but have not been fully satisfied with the answers... So, am I right or was the interviewer right?

To further clarify the question: Would load testing a WebAPI controller action in this case perform better under load just because it has an async keyword places in the signature even though it has no async code in it?

Upvotes: 2

Views: 96

Answers (2)

Steve
Steve

Reputation: 11963

you are correct, async without await would be a Task running on the current thread (not a new thread) till the end which basically = running synchronously.

If you really want to take into account of everything then marking it as async would add the totally ignorable overhead of creating a Task

Upvotes: 1

DavidG
DavidG

Reputation: 118937

The async modifier on it's own does nothing.

From the docs:

If the method that the async keyword modifies doesn't contain an await expression or statement, the method executes synchronously. A compiler warning alerts you to any async methods that don't contain await statements, because that situation might indicate an error.

Upvotes: 3

Related Questions