title: Autofac using Constructor
body: <p>I use unit of work pattern with entityFramework code first. Now I want to use <em>Autofac</em> to register UnitOfWork, Repositories and My dbContext.</p>
  
  <p>This Is my <code>UnitOfWork</code> code:</p>
  
  <pre><code>public class UnitOfWork : IUnitOfWork
  {
      private readonly DbContext _context;
  
      public UnitOfWork(DbContext context)
      {
          _context = context;
  
          Contact = new ContractRepository(context);
      }
  
      public void Dispose()
      {
          _context.Dispose();
          GC.SuppressFinalize(_context);
      }
  
      public IContactRepository Contact { get; private set; }
  
      public int Complete()
      {
          return _context.SaveChanges();
      }
  }
  </code></pre>
  
  <p>and this is my repository:</p>
  
  <pre><code>public class Repository&lt;Entity&gt; : IRepository&lt;Entity&gt; where Entity : class
  {
      protected readonly DbContext _noteBookContext;
      public Repository(DbContext noteBookContext)
      {
          _noteBookContext = noteBookContext;
      }
      public void Add(Entity entity)
      {
          _noteBookContext.Set&lt;Entity&gt;().Add(entity);
      }
  }
  </code></pre>
  
  <p>and this is one of my repositories:</p>
  
  <pre><code>public  class ContractRepository: Repository&lt;Contact&gt;,IContactRepository 
  {
      public ContractRepository(DbContext noteBookContext) : base(noteBookContext)
      {
  
      }
  
      public DbContext NotebookContext
      {
          get
          {
              return _noteBookContext;
          }
      }
  }
  </code></pre>
  
  <p>and this is my db context class:</p>
  
  <pre><code>public class NoteBookContext:DbContext
  {
      public NoteBookContext(string connectionstring):base(connectionstring)
      {
  
      }
      protected override void OnModelCreating(DbModelBuilder modelBuilder)
      {
          modelBuilder.Configurations.Add(new ContactConfig());
          modelBuilder.Configurations.Add(new PhoneConfig());
          modelBuilder.Configurations.Add(new PhoneTypeConfig());
          modelBuilder.Configurations.Add(new GroupConfig());
          base.OnModelCreating(modelBuilder);
      }
      public DbSet&lt;Contact&gt; Contacts { get; set; }
      public DbSet&lt;Phone&gt; Phones { get; set; }
  
      public DbSet&lt;Group&gt; Groups { get; set; }
  
      public DbSet&lt;PhoneType&gt; PhoneTypes { get; set; }
  }
  </code></pre>
  
  <p>Now I want to register UnitOfWork with constructor (a constructor like this: )</p>
  
  <pre><code>var uow = new UnitOfWork(new NotebookdbContext("connectionstring"));
  </code></pre>
  
  <p>Note that NoteBookContext is my entity framework model.</p>
  
  <p>I write registration but I got Error:</p>
  
  <pre><code>var builder = new ContainerBuilder();
  builder.RegisterType&lt;NoteBookContext&gt;()
         .As&lt;DbContext&gt;();
  builder.RegisterType&lt;UnitOfWork&gt;()
         .UsingConstructor(typeof(DbContext))
         .As&lt;IUnitOfWork&gt;();
  
  builder.RegisterGeneric(typeof(Repository&lt;&gt;))
         .As(typeof(IRepository&lt;&gt;))
         .InstancePerLifetimeScope();
  
  Container container = builder.Build();
  </code></pre>
  
  <p>This is my error:</p>
  
  <blockquote>
    <p>An unhandled exception of type 'Autofac.Core.DependencyResolutionException' occurred in Autofac.dll
    Additional information: None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'DataLayer.NoteBookContext' can be invoked with the available services and parameters:</p>
    
    <p>Cannot resolve parameter 'System.String connectionstring' of
    constructor 'Void .ctor(System.String)'.</p>
  </blockquote>
  
  <p>Edit 2 :</p>
  
  <p>after help from Cyril Durand's answer I write following registering config:</p>
  
  <pre><code> var builder = new ContainerBuilder();
          builder.RegisterType&lt;ConnectionStringProvider&gt;().As&lt;IConnectionStringProvider&gt;();
          builder.RegisterType&lt;NoteBookContext&gt;().As&lt;DbContext&gt;().WithParameter((pi, c) =&gt; pi.Name == "connectionstring",
                                                                                (pi, c) =&gt; c.Resolve&lt;IConnectionStringProvider&gt;().ConnectionString);
          builder.RegisterType&lt;UnitOfWork&gt;().As&lt;IUnitOfWork&gt;().WithParameter(ResolvedParameter.ForNamed&lt;DbContext&gt;("connectionstring"));
          builder.RegisterGeneric(typeof(Repository&lt;&gt;)).As(typeof(IRepository&lt;&gt;)).InstancePerLifetimeScope();
  </code></pre>
  
  <p>and In my code :</p>
  
  <pre><code>using (var scope = DependencyInjection.Container.BeginLifetimeScope())
          {
              var ConnectionString = scope.Resolve&lt;IConnectionStringProvider&gt;();
              ConnectionString.ConnectionString = "Context";
  
              var uw = scope.Resolve&lt;IUnitOfWork&gt;();
              var a =uw.Contact.GetAll();
  
          }
  </code></pre>
  
  <p>but I got Error again:</p>
  
  <blockquote>
    <p>An unhandled exception of type 'Autofac.Core.DependencyResolutionException' occurred in Autofac.dll</p>
    
    <p>Additional information: An exception was thrown while invoking the
    constructor 'Void .ctor(System.String)' on type 'NoteBookContext'.</p>
  </blockquote>
  
  <p>can everyone help me?</p>