title: Autofac multithreading issues
body: <p>I'm trying to use autofac DI in my application. I created a wrapper class to abstract away all the autofac dlls:</p>
  
  <p>FrameworkDependencyResolver : Logger, IFrameworkDependencyResolver</p>
  
  <p>In this class I hold the container builder, and register all my dependencies in the application root. After registering my types I build it and hold the container:</p>
  
  <p>Icontainer _container;
  ContainerBuilder _builder</p>
  
  <pre><code>public FrameworkDependencyResolver()
  {
    _builder = new ContainerBuilder();
  }
  </code></pre>
  
  <p>Deep in my application i want to use the FrameworkDependencyResolver object to resolve protocols and open connections to external applications, therefore I registered this object as IFrameworkDependencyResolver with the following code:</p>
  
  <pre><code>_builder.RegisterInstance(obj).As&lt;T&gt;();
  </code></pre>
  
  <p>Obj is thr FrameworkDependencyResolver, T is the interface</p>
  
  <p>In my starter thread, I resolve object that takes the FrameworkDependencyResolver in his ctor, and it works perfectly, resolvings are fine, however when I resolve an inner layer(on new thread) that takes the FrameworkDependencyResolver in it's ctor and try to resolve a registered protocol object I face deadlock.</p>
  
  <p>Exmaple:</p>
  
  <p>main:</p>
  
  <pre><code>var rootResolver = new FrameworkDependencyResolver();
  rootResolver.RegisterType&lt;IClass3, Class3&gt;(Lifecycles.Singleton);
  rootResolver.RegisterType&lt;IClass2, Class2&gt;(Lifecycles.Singleton);
  rootResolver.RegisterType&lt;Container, TestContainer&gt;(Lifecycles.Singleton);
  rootResolver.RegisterObject&lt;IFrameworkDependencyResolver, FrameworkDependencyResolver&gt;(rootResolver);
  
  rootResolver.BuildContainer();
  rootResolver.Resolve&lt;TestContainer&gt;();
  Console.ReadKey();
  </code></pre>
  
  <p>TestContainer code:</p>
  
  <pre><code>public TestContainer(IFrameworkDependencyResolver resolver) : base(resolver){}
  
  
  protected override void InitializeContainer()
     {
        _class2 = DependencyResolver.Resolve&lt;IClass2&gt;();
  
        Thread.Sleep(20000);
  
        Console.WriteLine("Container initialize finished");
     }
  </code></pre>
  
  <p>Class2 code:</p>
  
  <pre><code>    public class2(IFrameworkDependencyResolver resolver)
     {
       _resolver = resolver;
       var thread = new Thread(startMethod);
       thread.Start();
  
       Console.WriteLine("Class2 ctor ended");
  
      }
  
      void StartMethod()
      {
         _class3 = _resolver.Resolve&lt;IClass3&gt;();
          Console.WriteLine("Start method finished");
      }
  </code></pre>
  
  <p>The output of this simple example program is:</p>
  
  <pre><code>Class2 ctor ended
  Container initialize ended
  Start method finished
  </code></pre>
  
  <p>Meaning that the thread I created is waiting for the main thread to finish and only than it can resolve. I want to prevent this and make it possible to resolve anytime from every thread. Please help me understand what is causing this.
  Thank you</p>
  
  <p>Edit:
  The problem is not solved because autofac resolves singletons from the root scope..I believe my problem is similar to the one described here : <a href="https://stackoverflow.com/questions/15826228/autofac-resolving-a-singleton-creates-a-bottleneck">Autofac resolving a singleton creates a bottleneck</a>
   but I don't really understand the solution</p>
  
  <p>Edit 2:
  for the bottleneck issue I learned that ctors should not contain logic at all.
  I also learned I probably shouldn't pass around my IFrameworkDependencyResolver object and should probably use Func&lt;>.</p>
  
  <p>My application structure:
  I have a layer in my application that handles connection requests and for every kind of request creates a different kind of protocol (a different protocol object)</p>
  
  <pre><code>// For example lets say a protocol takes in ctor these 3 services + runtime configuration object:
  public Protocol1(IFramingAgent, IFramingAlgorithm, IFramingParser, configObject configuration)
  </code></pre>
  
  <p>Each service is registered with key because each protocol uses a different one</p>
  
  <p>And here is my terrible class:</p>
  
  <pre><code>public class ProtocolsLayer : Layer
  {
      private IFrameworkDependencyResolver _resolver;
      private IConfigurationService _configService;
  
      public ProtocolsLayer(IFrameworkDependencyResolver resolver, IConfigurationService configurationService)
      {
          _resolver = resolver;
          _configService = configurationService;
      }
  
      void HandleConnection1()
      {
          // What I have at the moment (terrible):
  
          // Resolve the fitting services (All keyed - key is received by the type, Resolve and ResolveWithParameters used here are my wrappers)
          var agent = _resolver.Resolve&lt;IFramingAgent&gt;(typeof(Protocol1FramingAgent));
  
          var algo = _resolver.Resolve&lt;IFramingAlgorithm&gt;(typeof(Protocol1FramingAlgorith));
  
          var parser = _resolver.Resolve&lt;IFramingParser&gt;(typeof(Protocol1FramingParser));
  
          // A parameter I get and pass to each protocol at runtime
          var protocolConfig = _configService.GetConfig&lt;Protocol1Configuration&gt;();
  
          // Finally resolve the protocol with it's parameters:
  
          protocol = _resolver.ResolveWithParameters&lt;IProtocol&gt;(typeof(Protocol1), new List&lt;object&gt;{
              agent, resolver, parser, protocolConfig 
          });
  
          //...
  
          // Theres gotta be a better way!! 
      }
  
      void HandleConntection2()
      {
          // Same as in protocol1
      }
  
      void HandleConnection3()
      {
          // Same as in protocol1
      }
  }
  </code></pre>
  
  <p>Take in mind that I don't want references to autofac, meaning I can't use IIndex&lt;> which I heard off.</p>
  
  <p>Thanks!   </p>