.Net核心DI范围验证,范围与瞬态? [英] .Net core DI Scope validation , scoped vs transient?

查看:127
本文介绍了.Net核心DI范围验证,范围与瞬态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

阅读 docs


当应用在开发环境中运行时,默认
服务提供商执行检查以验证:

When the app is running in the Development environment, the default service provider performs checks to verify that:


  • 范围服务不是从根服务提供商直接或间接解析的。

  • 作用域服务不会直接或间接注入单例中

  • Scoped services aren't directly or indirectly resolved from the root service provider.
  • Scoped services aren't directly or indirectly injected into singletons

这意味着我不应该将范围服务注入到单例服务中。

This means that I shouldn't inject Scoped services into a singleton service.

基于以下事实:临时服务每次在它们创建时都会创建一个实例被请求,VS作用域服务在请求的整个生命周期中都是单个实例:

Based on the fact that transient services creates an instance each time they're requested, VS scoped services which are single instances throughout the request's lifecycle :

问题:

为什么DI只验证范围内的serv难道不是临时服务吗?

Why does the DI only validate scoped services and not also transient services?

推荐答案


从单例中解析作用域服务很危险。在处理后续请求时,可能会导致服务的状态不正确。

It's dangerous to resolve a scoped service from a singleton. It may cause the service to have incorrect state when processing subsequent requests.

来自文档上面介绍了为什么他们要进行此检查。为了更好地理解这一点,我开始阅读各种有关生命的文章,我自己就是一个了解.net核心注册样式的人。

From the Docs the above describes why they do this check. To understand this better I started reading about the various lifetimes more being an autofac guy myself to understand the .net core flavor of registrations.


  • 范围内注册需要每个请求(连接)一个
    实例的服务生命期

  • Singleton在注册时或构造函数运行时仅定义一个状态。
    (在 startup.cs 中)

  • 瞬态是每次构造函数注入的新实例,即每个
    依赖项

  • The scoped registration entails a services lifetime being one instance per request(connection)
  • Singleton has only a single state defined at time of registration or constructor run time. (in startup.cs)
  • A transient is a new instance per constructor injection ie per dependency.

通常,我们会使用单例模式在应用程序生命周期内维护内存中的某种排序状态,等等,有很多用例但是重要的是要了解,您的singleton类的构造函数(在其中注入依赖项)将在整个应用程序生命周期中仅运行一次

Generally you would us the singleton pattern to maintain perhaps some sort state in memory for your application lifetime etc, there are many use cases but the important thing to understand that the constructor of your singleton class (where you inject your dependencies) will run once and only once for your entire application lifetime.

您可以想像,在不考虑上述情况的情况下向单例注入有范围的服务或临时服务会导致一些...意外的结果,您期望您的服务能够坚持下去到其特定的生存时间,但实际上由于单例的性质,每次实际上都是相同的实例。

You could imagine that injecting a scoped or transient service into singleton while baring the above in mind will lead to some ... unexpected results , you are expecting your service to adhere to its specific life time but in actual fact its really just the same instance every time due to the nature of the singleton.

使用我的理解来回答您的问题:注入到一个单例中(虽然本质上是不正确的)仍然可以正常工作,因为它的生存期很短并且没有破坏状态的风险,而作用域保证了单个请求的生存期(以某种形式的请求缓存为例),在注入单例的情况下,被瞄准镜的人无法兑现这一生。

To answer your question using my understanding : A transient injected into a singleton (while inherently not correct) will still function fine as its lifetime is short and runs of little risk of breaking state whereas scoped guarantees a lifetime across a single request (think some sort of request caching as an example), there is no way that scoped can honor that lifetime while being injected into a singleton.

这篇关于.Net核心DI范围验证,范围与瞬态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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