title: Autofac cannot resolve DbContext
body: <p>I am receiving this error message from Autofac;:</p>
  
  <p>None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'MyService`1[MyContext]' can be invoked with the available services and parameters:
  Cannot resolve parameter 'MyContext context' of constructor 'Void .ctor(MyContext)'.</p>
  
  <p>The error occurs on this line of code (also shown in the code below):</p>
  
  <pre><code>IMyService myService = container.Resolve&lt;IMyService&gt;();  // error here
  </code></pre>
  
  <p>I am interested to note that when I include this line in my registrations, everything works:</p>
  
  <pre><code>builder.RegisterSource(new AnyConcreteTypeNotAlreadyRegisteredSource());
  </code></pre>
  
  <p>The fact that it all works when I register AnyConcreteType... leads me to believe I am not not registering  something.  My question is what am I not registering?  The error message appears to name MyContext as the culprit but clearly I am registering it as shown below.<br>
  I really dont want to use the AnyConcreteType... catch all because I want to explicity register only the the classes I need.</p>
  
  <p>My service is constructed like this:</p>
  
  <pre><code>public class MyService&lt;T&gt; : BaseService&lt;T&gt;, IMyService where T:DbContext,IMyContext
  {
      public MyService(T context) : base(context)
      {
      }
  }
  </code></pre>
  
  <p>MyService derives from BaseService:</p>
  
  <pre><code>public abstract class BaseService&lt;T&gt; : IDisposable where T:DbContext, IMyContext
  {
      internal T db;
  
      public BaseService(T context)
      {
          db = context;
      }
  }
  </code></pre>
  
  <p>MyContext is passed to MyService and is constructed like this:</p>
  
  <pre><code>public partial class MyContext : DbContext, IMyContext
  {
      public MyContext(INamedConnectionString conn)
          : base(conn.ConnectionString)
      {
          Configuration.ProxyCreationEnabled = false;
      }
  }
  </code></pre>
  
  <p>Here is NamedConnectionString</p>
  
  <pre><code>public class NamedConnectionString : INamedConnectionString
  {
      public string ConnectionString { get; set; }
  }
  </code></pre>
  
  <p>Here is how I register the above:</p>
  
  <pre><code>builder.RegisterType&lt;MyService&lt;MyContext&gt;&gt;().As&lt;IMyService&gt;();  
  builder.RegisterType&lt;NamedConnectionString&gt;().As&lt;INamedConnectionString&gt;().SingleInstance();
  builder.RegisterType&lt;MyContext&gt;().As&lt;IMyContext&gt;().InstancePerLifetimeScope();
  builder.RegisterType&lt;DbContext&gt;(); // is this necessary??
  </code></pre>
  
  <p>And here is how I call it:</p>
  
  <pre><code>    var container = builder.Build();
      DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
      INamedConnectionString namedConnectionString = container.Resolve&lt;INamedConnectionString&gt;();
      namedConnectionString.ConnectionString = myConnectionString;
      IMyService myService = container.Resolve&lt;IMyService&gt;();  // error here
  </code></pre>