title: How to inject webapi AccountController in WebApi
body: <p>I have default constuctor and constructor with params like here:</p>
  
  <pre><code>public class AccountController : ApiController
  {
      private const string LocalLoginProvider = "Local";
      private ApplicationUserManager _userManager;
      public ISecureDataFormat&lt;AuthenticationTicket&gt; AccessTokenFormat { get; private set; }
      [Dependency]
      public IRepository Repository{ get; private set; }
  
      public AccountController()
      {
      }
  
      public AccountController(ApplicationUserManager userManager, ISecureDataFormat&lt;AuthenticationTicket&gt; accessTokenFormat)
      {
          _userManager = userManager;
          AccessTokenFormat = accessTokenFormat;
      }
  }
  </code></pre>
  
  <p>And my unity config here:</p>
  
  <pre><code>.RegisterType&lt;DbContext, ApplicationDbContext&gt;(new HierarchicalLifetimeManager())
  .RegisterType&lt;UserManager&lt;ApplicationUser, int&gt;, ApplicationUserManager&gt;()                
  .RegisterType&lt;ApplicationDbContext&gt;(new HierarchicalLifetimeManager())
  .RegisterType&lt;ApplicationUserManager&gt;()
  
  .RegisterType&lt;ISecureDataFormat&lt;AuthenticationTicket&gt;, SecureDataFormat&lt;AuthenticationTicket&gt;&gt;()
  .RegisterType&lt;ITextEncoder, Base64UrlTextEncoder&gt;()
  .RegisterType&lt;IDataSerializer&lt;AuthenticationTicket&gt;, TicketSerializer&gt;()
  //.RegisterType&lt;IDataProtector&gt;(() =&gt; new DpapiDataProtectionProvider().Create("ASP.NET Identity"))
  
  .RegisterType&lt;IUserStore&lt;ApplicationUser, int&gt;, CustomUserStore&gt;(new InjectionConstructor(typeof(ApplicationDbContext)))
  .RegisterType&lt;IAuthenticationManager&gt;(new InjectionFactory(o =&gt; HttpContext.Current.GetOwinContext().Authentication))
  .RegisterType&lt;IOwinContext&gt;(new InjectionFactory(o =&gt; HttpContext.Current.GetOwinContext()))
  .RegisterType&lt;IRepository, Repository&gt;();
  </code></pre>
  
  <p>But the thing is that default constructor is always getting called.
  I read taht article <a href="http://tech.trailmax.info/2014/09/aspnet-identity-and-ioc-container-registration/" rel="nofollow noreferrer">blog</a> but they not talking how to resolve such case with constructor havaving params <code>AccountController(ApplicationUserManager userManager, ISecureDataFormat&lt;AuthenticationTicket&gt; accessTokenFormat)</code></p>
  
  <p>If I am taking off parameterless constructor I am getting an error: <code>"An error occurred when trying to create a controller of type 'AccountController'. Make sure that the controller has a parameterless public constructor.",</code>
  Is there anyone can help?</p>
  
  <p>Also I have just normal ApiController and i is not getting injected as well:</p>
  
  <pre><code>public class MyController : ApiController
  {
      [Dependency]
      public IRepository Repository { get; set; }
  
      public IHttpActionResult Get()
      {
          var test = Repository.GetSomething(); // Repository is null here always
      }
  }
  </code></pre>
  
  <p><strong>UPDATE 1</strong> Based on @IgorPashchuk suggeston now <code>MyController</code> is getting injected now.
  But AccoutController is not. I am removed the default constructor but still getting the error.</p>
  
  <p><strong>UPDATE 2</strong> I alter the constructor with params by taking out the second param:</p>
  
  <pre><code>public class AccountController : ApiController
  {
      private const string LocalLoginProvider = "Local";
      private ApplicationUserManager _userManager;
  
      [Dependency]
      public IRepository Repository{ get; private set; }
  
      public AccountController(ApplicationUserManager userManager)
      {
          _userManager = userManager;
      }
  }
  </code></pre>
  
  <p>So this way I've got the thing is working. Is I understand that is means that Unity wasnt able to construct type <code>ISecureDataFormat&lt;AuthenticationTicket&gt;</code>. I post another question about this issue <a href="https://stackoverflow.com/questions/32445571/how-to-construct-isecuredataformatauthenticationticket-with-unity">How to construct ISecureDataFormat&lt;AuthenticationTicket&gt; with unity</a></p>