gpt4 book ai didi

c# - ASP.NET Core 管道多线程

转载 作者:太空狗 更新时间:2023-10-30 01:32:09 28 4
gpt4 key购买 nike

ASP.NET Core 管道是否通过多线程处理请求?如果是,如何配置线程数?还有,单例服务应该是线程安全的吗?

最佳答案

上面的评论已经回答了第一个问题(查看KestrelServerOptions)

关于线程安全,答案在 documentation 中:

Singleton lifetime services are created the first time they are requested (or when ConfigureServices is run if you specify an instance there) and then every subsequent request will use the same instance. If your application requires singleton behavior, allowing the services container to manage the service's lifetime is recommended instead of implementing the singleton design pattern and managing your object's lifetime in the class yourself.

这意味着对服务的所有请求都拉同一个对象,这意味着没有每线程对象,因此没有线程安全。

Thread safety

Singleton services need to be thread safe. If a singleton service has a dependency on a transient service, the transient service may also need to be thread safe depending how it’s used by the singleton.

再清楚不过了。由于对象不是按线程创建的,因此默认情况下它们不是线程安全的(尽管某些服务可能设计为线程安全)。

关于c# - ASP.NET Core 管道多线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38097661/

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