title: How to use Autofac to register components in non referencing projects
body: <p>Ho do you register components with <code>Autofac</code> without referencing all the projects in your web application?</p>
  
  <p>I have layered my application in the following format:</p>
  
  <ul>
  <li>Website</li>
  <li>Tasks</li>
  <li>Services</li>
  </ul>
  
  <p>Please use the code below for my explanations.</p>
  
  <p>My controller class:</p>
  
  <pre><code>public class MyController : Controller
  {
       private IMyTask myTask;
  
       public MyController(IMyTask myTask)
       {
            this.myTask = myTask;
       }
  
       public ActionResult Index()
       {
            HomeViewModel model = myTask.Index();
  
            return View(model);
       }
  }
  </code></pre>
  
  <p>My task class:</p>
  
  <pre><code>public class MyTask : IMyTask
  {
       private IMyService myService;
  
       public MyTask(IMyService myService)
       {
            this.myService = myService;
       }
  
       public HomeViewModel Index()
       {
            HomeViewModel homeViewModel = new HomeViewModel();
  
            // Do something with myService
  
            return homeViewModel;
       }
  }
  </code></pre>
  
  <p>My Index action method would call a method from my task class, this would create my view model and return it to the action method.</p>
  
  <p>I am only referencing the required references in each project, I don't want to reference any project that shouldn't be there.  For example, my website will only reference <code>MyProject.Tasks</code>.  And <code>MyProject.Tasks</code> will only reference <code>MyProject.Services</code>.  My website will not contain references to both these projects.</p>
  
  <p>I'm struggling to understand how I would use <code>Autofac</code> to register the above dependencies seeing that I don't reference both projects in my website?</p>
  
  <p>Normally I would do it like this in <code>Application_Start</code>:</p>
  
  <pre><code>ContainerBuilder builder = new ContainerBuilder();
  
  builder.RegisterControllers(Assembly.GetExecutingAssembly());
  
  builder.RegisterType&lt;MyTask&gt;().As&lt;IMyTask&gt;().InstancePerHttpRequest();
  builder.RegisterType&lt;MyService&gt;().As&lt;IMyService&gt;().InstancePerHttpRequest();
  
  IContainer container = builder.Build();
  DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
  </code></pre>
  
  <p>I have tried this from my website:</p>
  
  <pre><code>builder.RegisterAssemblyTypes(Assembly.ReflectionOnlyLoad("MyProject.Services"))
       .Where(t =&gt; t.Name.EndsWith("Service"))
       .AsImplementedInterfaces();
  </code></pre>
  
  <p>...but it won't work because I did not reference <code>MyProject.Services</code> in the website.</p>
  
  <p>What would be the best way to do this?</p>