Mr. B
Mr. B

Reputation: 2965

WebAPI - Session and Async Methods

I'm looking for a definitive answer on this. I was unable to find something definitive looking on my web.

  1. Is it strictly a no no to use the Session in a WebAPI REST service?

  2. Why does every example I see use Async/Await? (What is the advantage of this given each request already lives in its own thread?)

Thanks in advance!

Upvotes: 0

Views: 276

Answers (2)

Stephen Cleary
Stephen Cleary

Reputation: 456437

Is it strictly a no no to use the Session in a WebAPI REST service?

Not strictly forbidden, but generally discouraged. This is because session data can restrict your ability to scale out.

Why does every example I see use Async/Await? (What is the advantage of this given each request already lives in its own thread?)

It's all bout using fewer threads. While the request is (asynchronously) waiting, it won't use any thread. Thus, async/await enable you to scale less frequently. Async makes maximum use of your existing thread pool.

Upvotes: 2

TomTom
TomTom

Reputation: 62093

for 2:

What is the advantage of this given each request already lives in its own thread?

It is assumed that the WebApi call will somewhere down the line make another async call - for example database calls. In which case the Thread can be reused while waiting for the results.

Is it strictly a no no to use the Session in a WebAPI REST service?

Not sure about technically - but from a concept point of view, a REST service should not rely on sessions.

Upvotes: 2

Related Questions