2

In a WebAPI Self-Host application, will SendAsync() method of DelegatingHandler and any method of ApiController always execute on the same thread?

Is it safe to use [ThreadStatic] variable to pass RequestId from SessionHandler to TestController ?

Example:

public class TestController : ApiController { [HttpGet] public string Run() { LogManager.GetLogger("Controller").InfoFormat("Thread id: {0} ", Thread.CurrentThread.ManagedThreadId); return "Bar"; } } public class SessionHandler : DelegatingHandler { protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { LogManager.GetLogger("SessionHandler").InfoFormat("Thread id: {0} ", Thread.CurrentThread.ManagedThreadId); return await base.SendAsync(request, cancellationToken); } } 

1 Answer 1

2

In a WebAPI Self-Host application, will SendAsync() method of DelegatingHandler and any method of ApiController always execute on the same thread?

Is it safe to use [ThreadStatic] variable to pass RequestId from SessionHandler to TestController ?

No, and no. async on WebAPI returns the thread to the runtime, so any asynchrony in the pipeline (controller actions, filters, etc) may resume on another thread.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.