ASP.NET Core Singleton实例与瞬态实例的性能 [英] ASP.NET Core Singleton instance vs Transient instance performance

查看:198
本文介绍了ASP.NET Core Singleton实例与瞬态实例的性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在ASP.NET Core依赖注入中,我只是想知道注册 Singleton 实例是否比注册 Transient 实例吗?

In ASP.NET Core Dependency Injection, I just wonder if registering Singleton instances will improve performance better than registering Transient instances or not?

在我看来,对于 Singleton 实例,创建新对象只需花费一次时间和相关对象。对于 Transient 实例,此成本将针对每个服务请求重复。因此, Singleton 似乎更好。但是,使用 Singleton 而不是 Transient 时,我们可以获得多少性能?

In my thinking, for Singleton instance, it just cost once time for creating new object and dependent objects. For Transient instance, this cost will be repeat for each service request. So Singleton seems to be better. But how much performance gaining we have when using Singleton over Transient? Thank you in advanced!

推荐答案

就像其他人所说的那样,性能不应该在这里做出决定:性能不会受到重大影响无论哪种方式。您应该考虑的是托管和非托管的依赖项。当您利用有限的资源(例如套接字和连接)时,单例是最好的。如果最终每次注入服务(临时)时都不得不创建一个新的套接字,那么您将很快用尽套接字,那么性能确实会受到影响。

Like others have said, performance is not should make your decision here: performance will not be dramatically impacted either way. What should be your consideration is dependencies, both managed and unmanaged. Singletons are best when you're utilizing limited resources, like sockets and connections. If you end up having to create a new socket every time the service is injected (transient), then you'll quickly run out of sockets and then performance really will be impacted.

当资源使用是临时的并且影响最小时,临时范围会更好。例如,如果您仅在进行计算,那可以是临时作用域,因为您不会因为拥有多个副本而耗尽任何东西。

Transient scope is better when resource usage is temporary and of minimal impact. If you're only doing compute, for instance, that can be transient scoped because you're not exhausting anything by having multiple copies.

您还希望使用单例作用域当状态很重要时。如果某件事需要经过一项特定的操作后才能持续进行,那么瞬态将不起作用,因为您将没有状态,因为它基本上会在每次注入时开始。例如,如果您尝试使用信号量锁定来协调并发队列,那么您肯定需要单例作用域服务。如果状态无关紧要,则过渡可能是更好的范围。

You also want to use singleton scope when state matters. If something needs to persist past one particular operation, then transient won't work, because you'll have no state, because it will essentially start over each time it's injected. For example, if you were trying to coordinate a concurrent queue, using semaphores for locks, then you'd definitely want a singleton scoped service. If state doesn't matter, then transient is probably the better scope.

最后,您必须查看服务所依赖的其他服务。如果您需要访问作用域服务(例如请求范围内的服务),那么单例是不合适的。尽管您可以使用服务定位器模式来访问范围内的服务,但这是虚假的,不建议这样做。基本上,如果您的服务使用了除其他单例服务以外的任何其他服务,则应该将其设置为作用域或暂时的。

Finally, you must look at other services your service has a dependency on. If you need access to scoped services (such as things that are request-scoped), then a singleton is a bad fit. While you can possibly use a service-locator pattern to access the scoped services, that's a faux pas, and not recommended. Basically, if your service uses anything but other singleton services, it should likely be scoped or transient instead.

长短不一,除非您有良好的经验,否则请使用短暂的作用域,使其成为单身人士的明确原因。这就是上面提到的原因:维护状态,有效地利用有限的资源等。如果服务将在过渡范围内工作,并且没有充分的理由这样做,则使用过渡范围。

Long and short, use a transient scope unless you have a good, explicit reason to make it a singleton. That would be reasons like mentioned above: maintaining state, utilizing limited resources efficiently, etc. If the service will work in a transient scope, and there's no good reason to do otherwise, use transient scope.

现在,ASP.NET Core的DI具有瞬态和作用域寿命。从它们来回的意义上讲,这两个都是瞬态的,但是每个作用域(通常是一个请求)实例化 scoped一次,而每次 总是实例化 transient被注入。在这里,除非有充分明确的理由要使用瞬态,否则应使用作用域。

Now, ASP.NET Core's DI has both a "transient" and a "scoped" lifetime. Both of these are "transient" in the sense that they come and go, but "scoped" is instantiated once per "scope" (usually a request), whereas "transient" is always instantiated every time it is injected. Here, you should use "scoped" unless you have a good, explicit reason to use "transient".

这篇关于ASP.NET Core Singleton实例与瞬态实例的性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆