gpt4 book ai didi

asp.net - WebAPI : DelegatingHandler and ApiController on the same thread?

转载 作者:行者123 更新时间:2023-12-03 13:00:09 25 4
gpt4 key购买 nike

在WebAPI自托管应用程序中,DelegatingHandler的SendAsync()方法和ApiController的任何方法是否总是在同一线程上执行?

使用[ThreadStatic]变量将RequestId从SessionHandler传递到TestController是否安全?

例子:

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);
}
}

最佳答案

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 ?


不,不。 Web API上的 async将线程返回运行时,因此管道中的任何异步( Controller 操作,过滤器等)都可以在另一个线程上恢复。

关于asp.net - WebAPI : DelegatingHandler and ApiController on the same thread?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28813429/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com