对所有实例使用 HybridHttpOrThreadLocalScoped

Use HybridHttpOrThreadLocalScoped for All Instance

我在我的 ASP.NET MVC 5.x Web 应用程序中使用 结构图 4.5.1 并且我扫描 Interface 实现并使用以下代码为所有这些实现添加 HybridHttpOrThreadLocalScoped()

 public static class SmObjectFactory
 {
    private static readonly Lazy<StructureMap.Container> _containerBuilder =
        new Lazy<StructureMap.Container>(DefaultContainer, LazyThreadSafetyMode.ExecutionAndPublication);

    public static IContainer Container => _containerBuilder.Value;

    private static StructureMap.Container DefaultContainer()
    {
        return new StructureMap.Container(config =>
        {

            config.Scan(scanner =>
            {
                scanner.AssemblyContainingType(typeof(IPostService));
                scanner.WithDefaultConventions();
                scanner.SingleImplementationsOfInterface()
                       .OnAddedPluginTypes(expression =>  
                        expression.HybridHttpOrThreadLocalScoped());
            });
        });
    }
 }
  1. 当我使用此代码时,HybridHttpOrThreadLocalScoped() 它确实为固有 IDisposable 接口的所有接口添加了?如果答案是否定的,我该怎么做?
  2. 什么时候我不应该使用 HybridHttpOrThreadLocalScoped() ?
  • HybridHttpOrThreadLocalScoped 只是一个 life-time 说明符。所以当你指定它时,它将用于所有请求和配置的实例,这里它是否是 IDisposable 并不重要。这与处理任何东西无关,它是一个生命周期说明符。
  • 如果来自 IoC 容器的每个请求都需要新实例,请不要使用它。