title: Autofac.Core.Registration.ComponentNotRegisteredException
body: <p>Im trying to implement CQRS pattern to my app. So I found how to register all command handlers from assembly here : <a href="https://stackoverflow.com/questions/26595829/autofac-resolve-dependency-in-cqrs-commanddispatcher">Autofac resolve dependency in CQRS CommandDispatcher</a></p>
  
  <p>But it doesnt work well for me. Here is the code:</p>
  
  <pre><code>        containerBuilder.RegisterAssemblyTypes(assembly)
              .AsClosedTypesOf(typeof(ICommandHandler&lt;&gt;));
  
          containerBuilder.RegisterAssemblyTypes(assembly)
              .AsClosedTypesOf(typeof(IQueryHandler&lt;,&gt;));
  </code></pre>
  
  <p>Handlers factory</p>
  
  <pre><code> public class CqrsHandlerFactory : ICqrsHandlerFactory
  {
      private readonly IContainer container;
  
      public CqrsHandlerFactory(IContainer container)
      {
          this.container = container;
      }
  
      public ICommandHandler&lt;TCommand&gt; GetCommandHandler&lt;TCommand&gt;(TCommand command) where TCommand : class, ICommand
      {
          return container.Resolve&lt;ICommandHandler&lt;TCommand&gt;&gt;();
      }
  
      public IQueryHandler&lt;TQuery, object&gt; GetQueryHandler&lt;TQuery&gt;(TQuery query) where TQuery : class, IQuery
      {
          return container.Resolve&lt;IQueryHandler&lt;TQuery, object&gt;&gt;();
      }
  }
  </code></pre>
  
  <p>Bus</p>
  
  <pre><code> public class CqrsBus : ICqrsBus
  {
      private readonly ICqrsHandlerFactory cqrsHandlerFactory;
  
      public CqrsBus(ICqrsHandlerFactory cqrsHandlerFactory)
      {
          this.cqrsHandlerFactory = cqrsHandlerFactory;
      }
  
      public void ExecuteCommand(ICommand command)
      {
          var handler = cqrsHandlerFactory.GetCommandHandler(command);
          if (handler == null)
              throw new NotImplementedHandlerException(string.Format("Cannot find handler for {0}", command.GetType()));
          handler.Handle(command);
      }
  
      public TResult RunQuery&lt;TResult&gt;(IQuery query)
      {
          var handler = cqrsHandlerFactory.GetQueryHandler(query);
          if (handler == null)
              throw new NotImplementedHandlerException(string.Format("Cannot find handler for {0}", query.GetType()));
          return (TResult)handler.Handle(query);
      }
  }
  </code></pre>
  
  <p>Exception</p>
  
  <blockquote>
    <p>An exception of type 'Autofac.Core.Registration.ComponentNotRegisteredException' occurred in Autofac.dll but was not handled in user code
    Additional information: The requested service 'PromocjeWsieciowkach.Messaging.Core.ICommandHandler`1[[PromocjeWsieciowkach.Messaging.Core.ICommand, PromocjeWsieciowkach.Messaging.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]' has not been registered. To avoid this exception, either register a component to provide the service, check for service registration using IsRegistered(), or use the ResolveOptional() method to resolve an optional dependency.</p>
  </blockquote>
  
  <p>Stacktrace</p>
  
  <blockquote>
    <p>at Autofac.ResolutionExtensions.ResolveService(IComponentContext context, Service service, IEnumerable<code>1 parameters)
       at Autofac.ResolutionExtensions.Resolve[TService](IComponentContext context, IEnumerable</code>1 parameters)
       at PromocjeWsieciowkach.Messaging.Factories.CqrsHandlerFactory.GetCommandHandler[TCommand](TCommand command) in C:\Users\Daniel\Desktop\PromocjeWsieciowkach\src\PromocjeWsieciowkach.Messaging\Factories\CqrsHandlersFactory.cs:line 17
       at PromocjeWsieciowkach.Messaging.Bus.CqrsBus.ExecuteCommand(ICommand command) in C:\Users\Daniel\Desktop\PromocjeWsieciowkach\src\PromocjeWsieciowkach.Messaging\Bus\CqrsBus.cs:line 17
       at PromocjeWsieciowkach.Controllers.PostController.Index() in C:\Users\Daniel\Desktop\PromocjeWsieciowkach\src\PromocjeWsieciowkach\Controllers\PostController.cs:line 20
       at lambda_method(Closure , Object , Object[] )
       at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.d__28.MoveNext()</p>
  </blockquote>
  
  <p>So what i'am doing wrong?</p>