使用 PerThreadLifetimemanager 与 Transientlifetimemanager 的原因

Reason to use PerThreadLifetimemanager vs Transientlifetimemanager

我在构造函数中使用 DI。 BaseShoppingCart 是我在其中设置 ShoppingCartId(用于购物车商品)的服务。 这是构造函数:

public BaseShoppingCart( HttpContextBase context, IRepository<TCart> cart  ,IProductService productservice)
{  
    _context = context;
    _cart = cart;
     _productService = productservice;
    ShoppingCartId = _context.Request.IsAuthenticated ? _context.User.Identity.Name : getSessionCartId();
}

我需要你的意见:

  1. 我对这个问题的理解正确吗?
  2. PerThreadLifetimemanager 在哪些情况下比 Transientlifetimemanager 更有用?

谢谢!

要隔离用户(请求),请为每个请求创建新的子容器并从子容器中解析服务。因此,您可以使用 HierarchicalLifetimeManager 注册您的服务,您将为每个用户(请求)获得新实例。

Here 是很好的例子。

如果您不知道自己实际做什么,我不建议您使用 PerThreadLifetimeManager。