title: Dependency Injection Issue with EF, ASP.NET MVC and StructureMap
body: <p>I know there are a few similar question out there regarding this issue with structure map, but my issue seems to not be resolvable with the solutions they needed. I must state I have used this same setup many times, but cannot see why this code is causing issues. </p>
  
  <p>Here are all of the classes I think that are needed for help with troubleshooting my code.</p>
  
  <p>ControllerConvention.cs</p>
  
  <pre><code>public class ControllerConvention : IRegistrationConvention
  {
      public void ScanTypes(TypeSet types, Registry registry)
      {
          foreach (Type type in types.AllTypes())
          {
              if (type.CanBeCastTo(typeof(Controller)) &amp;&amp; !type.IsAbstract)
              {
                  registry.For(type).LifecycleIs(new UniquePerRequestLifecycle());
              }
          }
      }
  }
  </code></pre>
  
  <p>IoC.cs</p>
  
  <pre><code>public static class IoC
  {
      public static IContainer Container { get; set; }
  
      static IoC()
      {
          Container = new Container();
      }
  }
  </code></pre>
  
  <p>StandardRegistry.cs</p>
  
  <pre><code>public class StandardRegistry : Registry
  {
      public StandardRegistry()
      {
          Scan(scan =&gt;
          {
              scan.TheCallingAssembly();
              scan.Assembly("PotSmart.Service");
              scan.Assembly("PotSmart.Data");
              scan.WithDefaultConventions();
          });
      }
  }
  </code></pre>
  
  <p>StructureMapDependencyResolver.cs</p>
  
  <pre><code> public class StructureMapDependencyResolver : IDependencyResolver
  {
      private readonly Func&lt;IContainer&gt; _factory;
  
      public StructureMapDependencyResolver(Func&lt;IContainer&gt; factory)
      {
          _factory = factory;
      }
  
      public object GetService(Type serviceType)
      {
          if (serviceType == null)
          {
              return null;
          }
  
          var factory = _factory();
  
  
  
          return serviceType.IsAbstract || serviceType.IsInterface
              ? factory.TryGetInstance(serviceType)
              : factory.GetInstance(serviceType);
      }
  
      public IEnumerable&lt;object&gt; GetServices(Type serviceType)
      {
          return _factory().GetAllInstances(serviceType).Cast&lt;object&gt;();
      }
  }
  </code></pre>
  
  <p>Global.asax</p>
  
  <pre><code> public IContainer Container
      {
          get
          {
              return (IContainer)HttpContext.Current.Items["_Container"];
          }
          set
          {
              HttpContext.Current.Items["_Container"] = value;
          }
      }
  
      protected void Application_Start()
      {
  
  
          AreaRegistration.RegisterAllAreas();
          FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
          RouteConfig.RegisterRoutes(RouteTable.Routes);
          BundleConfig.RegisterBundles(BundleTable.Bundles);
  
          DependencyResolver.SetResolver(new StructureMapDependencyResolver(() =&gt; Container ?? IoC.Container));
  
          IoC.Container.Configure(cfg =&gt;
          {
              cfg.AddRegistry(new StandardRegistry());
              cfg.AddRegistry(new ControllerRegistry());
  
          });
  
  
      }
  </code></pre>
  
  <p>Here are parts of my data and entities classes:</p>
  
  <p>RepositoryBase.cs</p>
  
  <pre><code>public class RepositoryBase 
  {
      private readonly PotSmartEntityModel1 _dataContext;
      protected IDbFactory DbFactory { get; private set; }
  
      protected PotSmartEntityModel1 DbContext
      {
          get
          {
              return _dataContext ?? DbFactory.Init();
          }
      }
  
      protected RepositoryBase(IDbFactory dbFactory)
      {
          DbFactory = dbFactory;
          var adapter = (IObjectContextAdapter)this;
          var objectContext = adapter.ObjectContext;
          // objectContext.CommandTimeout = 120; // value in seconds
      }
  
  }
  
  public abstract class RepositoryBase&lt;T&gt; where T : class
  {
      private readonly PotSmartEntityModel1 _dataContext;
      private readonly IDbSet&lt;T&gt; _dbSet;
  
      protected IDbFactory DbFactory { get; private set; }
  
      protected PotSmartEntityModel1 DbContext
      {
          get
          {
              return _dataContext ?? DbFactory.Init();
          }
      }
  
      protected RepositoryBase(IDbFactory dbFactory)
      {
          DbFactory = dbFactory;
          _dbSet = DbContext.Set&lt;T&gt;();
      }
  
      public virtual void Add(T entity)
      {
          _dbSet.Add(entity);
      }
  
      public virtual void Update(T entity)
      {
          _dbSet.Attach(entity);
          DbContext.Entry(entity).State = EntityState.Modified;
      }
  
      public virtual void Delete(T entity)
      {
          _dbSet.Remove(entity);
      }
  
      public virtual void Delete(Expression&lt;Func&lt;T, bool&gt;&gt; where)
      {
          IEnumerable&lt;T&gt; objects = _dbSet.Where&lt;T&gt;(where);
          foreach (T obj in objects)
          {
              _dbSet.Remove(obj);
          }
      }
  
      public virtual T GetById(int id)
      {
          return _dbSet.Find(id);
      }
  
      public virtual T GetById(string id)
      {
          return _dbSet.Find(id);
      }
  
      public virtual IEnumerable&lt;T&gt; GetAll()
      {
          return _dbSet.ToList();
      }
  
      public virtual IEnumerable&lt;T&gt; GetMany(Expression&lt;Func&lt;T, bool&gt;&gt; where)
      {
          return _dbSet.Where(where).ToList();
      }
  
      public T Get(Expression&lt;Func&lt;T, bool&gt;&gt; where)
      {
          return _dbSet.Where(where).SingleOrDefault();
      }
  
      public virtual IQueryable&lt;T&gt; Query()
      {
          return _dbSet;
      }
  
      public virtual IQueryable&lt;T&gt; Query(Expression&lt;Func&lt;T, bool&gt;&gt; where)
      {
          return _dbSet.Where(where);
      }
  
      public virtual ObjectQuery&lt;U&gt; CreateQuery&lt;U&gt;(string query, ObjectParameter[] parameters)
      {
          return CastAsObjectContext().CreateQuery&lt;U&gt;(query, parameters);
      }
  
      public virtual ObjectQuery&lt;U&gt; CreateQuery&lt;U&gt;(string query)
      {
          return CreateQuery&lt;U&gt;(query, new ObjectParameter[0] { });
      }
  
      public virtual ObjectQuery&lt;DbDataRecord&gt; CreateQuery(string query, ObjectParameter[] parameters)
      {
          return CreateQuery&lt;DbDataRecord&gt;(query, parameters);
      }
  
      public virtual ObjectQuery&lt;DbDataRecord&gt; CreateQuery(string query)
      {
          return CreateQuery&lt;DbDataRecord&gt;(query);
      }
  
      private ObjectContext CastAsObjectContext()
      {
          var oContext = (DbContext as IObjectContextAdapter).ObjectContext;
  
          return oContext;
      }
  
  }
  </code></pre>
  
  <p>DBFactory.cs</p>
  
  <pre><code>class DbFactory : Disposable, IDbFactory
  {
      private PotSmartEntityModel1 _dbContext;
  
      public PotSmartEntityModel1 Init()
      {
          return _dbContext ?? (_dbContext = new PotSmartEntityModel1());
      }
  
      protected override void DisposeCore()
      {
          if (_dbContext != null)
          {
              _dbContext.Dispose();
          }
      }
  }
  </code></pre>
  
  <p>IDBFactory.cs</p>
  
  <pre><code>public interface IDbFactory : IDisposable
  {
      PotSmartEntityModel1 Init();
  }
  </code></pre>
  
  <p>IRepository.cs</p>
  
  <pre><code>public interface IRepository
  {
  }
  
  
  public interface IRepository&lt;T&gt; where T : class
  {
      void Add(T entity);
      void Update(T entity);
      void Delete(T entity);
      void Delete(Expression&lt;Func&lt;T, bool&gt;&gt; where);
      T GetById(int id);
      T GetById(string id);
  
      IEnumerable&lt;T&gt; GetAll();
  
      IEnumerable&lt;T&gt; GetMany(Expression&lt;Func&lt;T, bool&gt;&gt; where);
  
      IQueryable&lt;T&gt; Query();
  
      IQueryable&lt;T&gt; Query(Expression&lt;Func&lt;T, bool&gt;&gt; where);
  
      ObjectQuery&lt;U&gt; CreateQuery&lt;U&gt;(string query, ObjectParameter[] parameters);
  
      ObjectQuery&lt;U&gt; CreateQuery&lt;U&gt;(string query);
  
      ObjectQuery&lt;DbDataRecord&gt; CreateQuery(string query, ObjectParameter[] parameters);
  
      ObjectQuery&lt;DbDataRecord&gt; CreateQuery(string query);
  
  
  }
  </code></pre>
  
  <p>Now, sorry for so much code but that way I get all the answer out there that some in the community may ask. The issue is on my controller, I use no default constructor and do the dependency injection like the following code show:</p>
  
  <p>MarkerController.cs</p>
  
  <pre><code>public class MarkerController : Controller
  {
  
      private readonly IMarkerService _markerService;
  
      public MarkerController(IMarkerService markerService)
      {
          _markerService = markerService;
      }
  
      // GET: Markers
      public ActionResult Index()
      {
          return View();
      }
  </code></pre>
  
  <p>}</p>
  
  <p>When I run the code I keep getting the following error, and have tried anything I could think of. </p>
  
  <blockquote>
    <p>StructureMap.StructureMapConfigurationException: 'No default Instance is registered and cannot be automatically determined for type 'PotSmart.Data.Interfaces.IDbFactory'</p>
  </blockquote>
  
  <p>I know StructureMap does not always have the clearest error messages, but I feel this meant my Entity model was not being initialized at run time for some reason.It fails during "factory.GetInstance(serviceType)". Has anyone had this error, or does anyone see what I am obviously overlooking in the code sets I have above that would cause this issue? Thanks a million as always in advance.</p>