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

问题描述

限时删除!!

在 ASP.NET Core 依赖注入中,我只是想知道注册 Singleton 实例是否会提高性能而不是注册 Transient 实例?

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

在我看来,对于 Singleton 实例,创建新对象和依赖对象只需要花费一次时间.对于 Transient 实例,每个服务请求都会重复此成本.所以 Singleton 似乎更好.但是当使用 Singleton 而不是 Transient 时,我们获得了多少性能?提前谢谢你!

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

推荐答案

正如其他人所说,性能不应由您决定:无论哪种方式,性能都不会受到显着影响.您应该考虑的是托管和非托管的依赖项.当您使用有限的资源(如套接字和连接)时,单例是最好的.如果每次注入服务时都不得不创建一个新的套接字(瞬态),那么你很快就会用完套接字,然后性能就会真正受到影响.

Like others have said, performance should not 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 computation, 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 既具有瞬态"特性,又具有瞬态"特性.和范围"寿命.这两者都是瞬态"的.从某种意义上说,它们来来去去,但有范围"每个范围"实例化一次(通常是请求),而transient"则是每次注入时总是实例化.在这里,您应该使用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 单例实例与瞬态实例性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-08 04:11