title: Autofac register types from Assembly
body: <p>I am using autofac in my solution to resolve library dependencies across the different projects. The idea is to make hotplug libraries in the sense that, there are only a few core libraries that the application is dependent on, they are prefixed <code>"vmecore.*.dll"</code>. Which must be registered with autofac before any other libraries.</p>
  
  <p>Any other library that is prefixed with "vme.*.dll is are loaded, I then look for the first type that implements "IVmeExtension" - which defines an <em>Initiate()</em> method -, i register it, activate it and finally initialize it.</p>
  
  <p>The code that does the registering of core libraries:</p>
  
  <pre><code>        Directory.GetFiles(path, "vmecore.*.dll")
              .Select(Assembly.LoadFile)
              .ToList()
              .ForEach(ass =&gt;
              {
                  //var validAss = Assembly.Load(ass.FullName);
  
                  builder.RegisterAssemblyTypes(ass)
                      .Where(t =&gt; t.IsAssignableTo&lt;IVmeExtension&gt;())
                      .SingleInstance();
                  //return;
  
                  // Iterate through all the types in the assembly
                  foreach (var type in ass.GetExportedTypes()
                          .Where(a =&gt; a.IsClass &amp;&amp;
                                      !a.IsAbstract &amp;&amp;
                                      a.Namespace != null &amp;&amp;
                                      a.Namespace.Contains(@"VME")))
                  {
                      // Get the first type of IVmeExtension
                      if (!typeof(IVmeExtension).IsAssignableFrom(type)) continue;
  
                      //builder.RegisterType(type).SingleInstance();
                      coreExtension.Add(type);
                      break;
                  }
              });
  </code></pre>
  
  <p>The snippet below is the callback delegate invoked when the container is built. Core libraries need to be resolved and initialized before hotplugged extensions since most extensions depend on them.</p>
  
  <pre><code>           //Register a call back when container is built so we can resolve core extensions first
              builder.RegisterBuildCallback(container =&gt;
              {
                  coreExtension.ForEach(ext =&gt;
                  {
                      try
                      {
                          var inst = (IVmeExtension)container.Resolve(ext);
                          inst.Initiate();
  
                          Interface.LogInfo($"VME: Core extension '{inst.Name}'");
                      }
                      catch (Exception e)
                      {
                          // we log the error here.
                          throw;
                      }
                  });
              });
  </code></pre>
  
  <p>The second extension that's resolved needs the former to be inject on the constructor -for this experiment i manually resolved it in the constructor to see the registered services- . The problem occurs here; I get the exception "The requested service '..' has not been registered" yet looking at the registration the type is literary there.</p>
  
  <p><a href="https://i.stack.imgur.com/sdq3y.png" rel="nofollow noreferrer">Screenshot</a></p>
  
  <p>I have tried the solution suggest on this post <a href="https://github.com/autofac/Autofac/issues/593" rel="nofollow noreferrer">https://github.com/autofac/Autofac/issues/593</a> but when i do that, I get a system pointer exception, and its very clear since when step into the activation of the instance <code>(var inst = (IVmeExtension)container.Resolve(ext))</code> the IDE is actually taking me to the constructor of a completely different class - the different class is in the executing assembly - </p>