Search code examples
c#asp.net-mvcinversion-of-controlstructuremapidisposable

Use HybridHttpOrThreadLocalScoped for All Instance


I use structure map 4.5.1 in my ASP.NET MVC 5.x Web Application and I do Scan Interface implementations and add HybridHttpOrThreadLocalScoped() for all of them with following codes:

 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. When i use this code, HybridHttpOrThreadLocalScoped() it do added for All Interfacs that inherent IDisposable Interface ? if answer is no how can I do this ?
  2. When i Sholud not use HybridHttpOrThreadLocalScoped() ?

Solution

    • HybridHttpOrThreadLocalScoped is just a life-time specifier. so when you specify it, it will be used for all of the requested and configured instances and here it's not important that it's IDisposable or not. It's not about disposing anything, it's a life-time specifier.
    • If you need new instances per each request from the IoC container, don't use it.