title: No parameterless constructor defined for this object exception while using Unity Container
body: <p>I am getting the above exception while trynig to load a view.</p>
  
  <p>I am using Unity to intialize my controller instance. Still getting the above error.</p>
  
  <p>Here is my controller.</p>
  
  <pre><code>public class SiteController : Controller
  {
  
      private ISiteRepository _repository;
  
      public SiteController(ISiteRepository repository)
      {
          _repository = repository;
      }
  
      //
      // GET: /Site/
  
      public ActionResult Index()
      {
          return View();
      }
  
      //
      // GET: /Site/Details/5
  
      public ActionResult Details(int id)
      {
          return View();
      }}
  </code></pre>
  
  <p>And here is my Global.asax.cs to </p>
  
  <pre><code>protected void Application_Start()
      {
          ConfigApi(GlobalConfiguration.Configuration);
          AreaRegistration.RegisterAllAreas();
  
          FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
          RouteConfig.RegisterRoutes(RouteTable.Routes);
          BundleConfig.RegisterBundles(BundleTable.Bundles);
      }
  
      static void ConfigApi(HttpConfiguration config)
      {
          var unity = new UnityContainer();
          unity.RegisterType&lt;SiteController&gt;();
          unity.RegisterType&lt;ISiteRepository, SiteRepository&gt;(new HierarchicalLifetimeManager());
  
          config.DependencyResolver = new IocContainer(unity);
      }
  </code></pre>
  
  <p>Here is my SiteRepository class.</p>
  
  <pre><code>public class SiteRepository:ISiteRepository
  {
      private readonly SampleMVCEntities _dbContext;
  
      public SiteRepository()
      {
          _dbContext = new SampleMVCEntities();
      }
  
      private IQueryable&lt;SiteConfig&gt; MapSiteConfig()
      {
          return _dbContext.SiteConfigs.Select(a =&gt; new SiteConfig
          {
              Name = a.Name,
              LinkColour = a.LinkColour,
              SiteLogo = a.SiteLogo,
              SiteBrands = a.SiteBrands.Select(b =&gt; new Models.SiteBrand { SiteId = b.SiteId, BrandId = b.BrandId })
          });
      }
  
      public IEnumerable&lt;SiteConfig&gt; GetAll()
      {
          return MapSiteConfig().AsEnumerable();
      }}
  </code></pre>
  
  <p>This is my error stack.</p>
  
  <p>No parameterless constructor defined for this object. 
  Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. </p>
  
  <blockquote>
    <p>Exception Details: System.MissingMethodException: No parameterless
    constructor defined for this object.</p>
    
    <p>Source Error: </p>
    
    <p>An unhandled exception was generated during the execution of the
    current web request. Information regarding the origin and location of
    the exception can be identified using the exception stack trace below.</p>
    
    <p>Stack Trace: </p>
    
    <p>[MissingMethodException: No parameterless constructor defined for this
    object.]    System.RuntimeTypeHandle.CreateInstance(RuntimeType type,
    Boolean publicOnly, Boolean noCheck, Boolean&amp; canBeCached,
    RuntimeMethodHandleInternal&amp; ctor, Boolean&amp; bNeedSecurityCheck) +0<br>
    System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean
    skipCheckThis, Boolean fillCache, StackCrawlMark&amp; stackMark) +114<br>
    System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly,
    Boolean skipCheckThis, Boolean fillCache, StackCrawlMark&amp; stackMark)
    +232    System.Activator.CreateInstance(Type type, Boolean nonPublic) +83    System.Activator.CreateInstance(Type type) +6    System.Web.Mvc.DefaultControllerActivator.Create(RequestContext
    requestContext, Type controllerType) +55</p>
    
    <p>[InvalidOperationException: An error occurred when trying to create a
    controller of type 'Config.Controllers.SiteController'. Make sure that
    the controller has a parameterless public constructor.]<br>
    System.Web.Mvc.DefaultControllerActivator.Create(RequestContext
    requestContext, Type controllerType) +179<br>
    System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(RequestContext
    requestContext, Type controllerType) +80<br>
    System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext
    requestContext, String controllerName) +74<br>
    System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase
    httpContext, IController&amp; controller, IControllerFactory&amp; factory)
    +197    System.Web.Mvc.&lt;>c_<em>DisplayClass6.b</em>_2() +49    System.Web.Mvc.&lt;>c__DisplayClassb<code>1.&lt;ProcessInApplicationTrust&gt;b__a()
    +13    System.Web.Mvc.SecurityUtil.&lt;GetCallInAppTrustThunk&gt;b__0(Action f) +7    System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action
    action) +22<br>
    System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Func</code>1 func) +88
    System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase
    httpContext, AsyncCallback callback, Object state) +98<br>
    System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext,
    AsyncCallback callback, Object state) +50<br>
    System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext
    context, AsyncCallback cb, Object extraData) +16<br>
    System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
    +268    System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean&amp; completedSynchronously) +155</p>
  </blockquote>
  
  <hr>
  
  <p>Can somebody help me?</p>
  
  <p>Thanks.</p>