title: Unity DI with MVC 5 in controllers
body: <p>I'm currently having issues when trying to make a dependency injection inside a MVC controller.</p>
  
  <p>The current exception is the following:</p>
  
  <blockquote>
    <p>The current type, myproject.Core.ToolbarLogic, is an interface and
    cannot be constructed. Are you missing a type mapping?</p>
  </blockquote>
  
  <p>Debugging I realized this exception is raised from this sentence (included in a Razor view):</p>
  
  <pre><code>@{ Html.RenderAction("Toolbar", "Toolbar");  }
  </code></pre>
  
  <p>In the UnityConfig file, all types are registered conveniently, 
  I have an empty constructor in the controller, 
  the exception is raised from a place I cannot access debugging... Also I have read lots of answers here in StackOverflow and Google, I don't know what to try now (I have tried nearly everything).</p>
  
  <p>Anyone knows what is the problem with the DI?</p>
  
  <p><strong>Code:</strong></p>
  
  <p>ToolbarController.cs</p>
  
  <pre><code>public class ToolbarController : BaseController
  {
  
      [Dependency]
      public IToolbarLogic ToolbarLogic { get; set; }
  
      public ToolbarController()
      {
      }
  
  
      // GET: Common/Toolbar
      public ActionResult Toolbar()
      {
          bool ShowConfidential = ToolbarLogic.ShowConfidential();
          string linkHome = ToolbarLogic.BindHome(base.User.Identity.Name);
          return PartialView(new ToolbarModel() {
              ShowConfidential = ShowConfidential,
              lnkHome = linkHome
          });
          return PartialView();
      }
  
  }
  </code></pre>
  
  <p>UnityWebActivator.cs</p>
  
  <pre><code>/// &lt;summary&gt;Provides the bootstrapping for integrating Unity with ASP.NET MVC.&lt;/summary&gt;
  public static class UnityWebActivator
  {
      /// &lt;summary&gt;Integrates Unity when the application starts.&lt;/summary&gt;
      public static void Start() 
      {
          var container = UnityConfig.GetConfiguredContainer();
  
          FilterProviders.Providers.Remove(FilterProviders.Providers.OfType&lt;FilterAttributeFilterProvider&gt;().First());
          FilterProviders.Providers.Add(new UnityFilterAttributeFilterProvider(container));
  
          DependencyResolver.SetResolver(new UnityDependencyResolver(container));
  
          // TODO: Uncomment if you want to use PerRequestLifetimeManager
          // Microsoft.Web.Infrastructure.DynamicModuleHelper.DynamicModuleUtility.RegisterModule(typeof(UnityPerRequestHttpModule));
      }
  
      /// &lt;summary&gt;Disposes the Unity container when the application is shut down.&lt;/summary&gt;
      public static void Shutdown()
      {
          var container = UnityConfig.GetConfiguredContainer();
          container.Dispose();
      }
  }
  </code></pre>
  
  <p>UnityConfig.cs</p>
  
  <pre><code>    /// &lt;summary&gt;
  /// Specifies the Unity configuration for the main container.
  /// &lt;/summary&gt;
  public class UnityConfig
  {
      #region Unity Container
      private static Lazy&lt;IUnityContainer&gt; container = new Lazy&lt;IUnityContainer&gt;(() =&gt;
      {
          var container = new UnityContainer();
          RegisterTypes(container);
          return container;
      });
  
      /// &lt;summary&gt;
      /// Gets the configured Unity container.
      /// &lt;/summary&gt;
      public static IUnityContainer GetConfiguredContainer()
      {
          return container.Value;
      }
      #endregion
  
      /// &lt;summary&gt;Registers the type mappings with the Unity container.&lt;/summary&gt;
      /// &lt;param name="container"&gt;The unity container to configure.&lt;/param&gt;
      /// &lt;remarks&gt;There is no need to register concrete types such as controllers or API controllers (unless you want to 
      /// change the defaults), as Unity allows resolving a concrete type even if it was not previously registered.&lt;/remarks&gt;
      public static void RegisterTypes(IUnityContainer container)
      {
          container.RegisterType&lt;IToolbarLogic, ToolbarLogic&gt;();
  
          // There is an Unity.config file
          container.LoadConfiguration();
      }
  }
  </code></pre>
  
  <p>Unity.config</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;!-- Configuración de Unity --&gt;
  &lt;unity xmlns="http://schemas.microsoft.com/practices/2010/unity"&gt;
    &lt;container&gt;
  
  
    &lt;/container&gt;
  &lt;/unity&gt;
  </code></pre>
  
  <p>Edit 1:</p>
  
  <p>I have commented the <code>LoadConfiguration</code> line as @Nkosi said, because it makes sense since I don't have any relevant information in my Unity.config (see above, it's empty).</p>
  
  <p>The error stils the same:</p>
  
  <pre><code>Resolution of the dependency failed, type = "MVCControls.Controllers.ToolbarController", name = "(none)".
  Exception occurred while: while resolving.
  
  Exception is: InvalidOperationException - The current type, interfaces_logic.Interfaces.IToolbarLogic, is an interface and cannot be constructed. Are you missing a type mapping?
  -----------------------------------------------
  At the time of the exception, the container was:
  
    Resolving MVCControls.Controllers.ToolbarController,(none)
    Resolving value for property ToolbarController.ToolbarLogic
    Resolving interfaces_logic.Interfaces.IToolbarLogic,(none)
  </code></pre>