本文介绍了使用IServiceCollection.AddTransient,IServiceCollection.AddSingleton和IServiceCollectionAddScoped方法的实际方案是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

阅读发布后,我可以理解AddTransientAddScopedAddSingleton之间的区别,但是,我看不到它们各自的实际用法.

After reading this post I can understand the differences between AddTransient,AddScoped and AddSingleton however, I am unable to see the practical usage of each of them.

我的理解是

AddTransient

每次客户要求时创建一个新实例.

Creates a new instance every time when the client asks for it.

services.AddTransient<IDataAccess, DataAccess>();

每当客户端代码要求

时,它将返回一个新的DataAccess对象.更有可能是构造函数.

will return a new DataAccess object every time a client code asks for it. More likely a constructor.

AddTransient的用法

在必须访问数据库以读取和更新它并销毁访问对象(DataAccess)的情况下,最好使用AddTransient-不确定线程​​的安全性.

In cases when we have to access a database to read and update it and destroy the access object (DataAccess) its best to use AddTransient - Not sure about the thread safty.

AddScoped

为每个http Web请求创建一个新实例.

Creates a new instance for each http web request.

AddScoped的使用

 services.AddScoped<ShoppingCart>(serviceProvider => ShoppingCart.GetShoppingCart(serviceProvider));

这意味着每个Web请求将具有其自己的购物车实例,这意味着每个用户/客户将对该HTTP Web请求具有其自己的购物车实例.

this mean each web request will be having its own shopping cart instance which intern means each user / client will be having its own shoping cart instance for that http web request.

AddSingleton

为所有http网络请求创建一个实例.

Create single instance for all the http web requests.

AddSingleton的使用

在示例应用程序中找到了此代码,但我不明白它的用处.

Found this code in an sample application but I dont understand how it is being useful.

services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>(); 

有人可以在使用AddSingleton时举一个体面的实际例子,并检查我对AddTransient和AddScoped的理解是否正确吗?

Can someone please give a decent practical example when to use AddSingleton and check if my understanding of AddTransient and AddScoped is correct?

推荐答案

您对所有3个范围的理解都是正确的.

Your understanding of all 3 scopes is correct.

瞬态将在无法共享组件时使用.一个非线程安全的数据库访问对象就是一个示例.

Transient would be used when the component cannot be shared. A non-thread-safe database access object would be one example.

作用域可用于Entity Framework数据库上下文.主要原因是,然后将从数据库中获得的实体附加到请求中所有组件都可以看到的相同上下文上.当然,如果您打算并行进行查询,则不能使用Scoped.

Scoped can be used for Entity Framework database contexts. The main reason is that then entities gotten from the database will be attached to the same context that all components in the request see. Of course if you plan on doing queries with it in parallel, you can't use Scoped.

范围对象的另一个示例是某种RequestContext类,其中包含例如呼叫者的用户名.中间件/MVC筛选器可以请求它并填写信息,而其他组件也可以请求它,并且肯定会包含当前请求的信息.

Another example of a Scoped object would be some kind of a RequestContext class, that contains e.g. the username of the caller. A middleware/MVC filter can request it and fill out the info, and other components down the line can also request for it, and it will surely contain the info for the current request.

Singleton 组件总是共享的,因此它们最适合不需要绑定到请求的线程安全组件.一个示例是IOptions,它可以访问配置设置.在单个静态HttpClient实例上使用SendAsyncHttpClient包装器类也将完全是线程安全的,并且是成为Singleton的理想选择.

Singleton components are shared always, so they are best for thread-safe components that do not need to be bound to a request. An example would be IOptions, which gives access to configuration settings. An HttpClient wrapper class that uses SendAsync on a single static HttpClient instance would also be completely thread-safe, and a good candidate for being a Singleton.

请注意,如果您具有依赖于Scoped组件的Singleton组件,则其依赖项将在此组件之前得到释放.因此,一个组件不能依赖于范围比其自身小的另一个组件.

Note that if you have a Singleton component that depends on a Scoped component, its dependency would get disposed before it. Thus a component cannot depend on another component that has smaller scope than itself.

这篇关于使用IServiceCollection.AddTransient,IServiceCollection.AddSingleton和IServiceCollectionAddScoped方法的实际方案是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 10:23