ASP.NET Core Singleton instance vs Transient instance performance

前端 未结 2 762
野性不改
野性不改 2021-02-12 14:42

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

相关标签:
2条回答
  • 2021-02-12 15:23

    Like mentioned in the replies this isn't really a matter over performance.

    For more details please see the link below where the difference is very elaborately explained. https://stackoverflow.com/a/38139500/10551193

    The only way that this will matter performance wise is if your constructor is doing a lot of stuff. This can and should be avoided though in all cases.

    0 讨论(0)
  • 2021-02-12 15:45

    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.

    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".

    0 讨论(0)
提交回复
热议问题