title: "The type IUnitOfWork does not have an accessible constructor" with Umbraco 6.1, UmbracoApiController (Web API) & Dependency Injection (Unity)
body: <p>I am using Umbraco 6.1 with an UmbracoApiController which has a IUnitOfWork injected into it's constructor. To inject the dependencies, I am using Unity, like I have in the past with standard Web API projects. Normally, I set unity up in the Global.asax.cs. As Umbraco does not have this I have created my own UmbracoEvents handler, which inherits from IApplicationEventHandler, and has the methods:</p>
  
  <ol>
  <li>OnApplicationInitialized </li>
  <li>OnApplicationStarting </li>
  <li>OnApplicationStarted</li>
  <li>ConfigureApi</li>
  </ol>
  
  <p>In the OnApplicationStarted method I set up my EF database, db initializer etc and call ConfigureApi to set up Unity. My OnApplication Started and ConfigureApi methods looks like this:</p>
  
  <pre><code>    public void OnApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
      {
          _applicationContext = applicationContext;
          _umbracoApplication = umbracoApplication;
          _contentService = ApplicationContext.Current.Services.ContentService;
          this.ConfigureApi(GlobalConfiguration.Configuration);
          Database.SetInitializer(null);
          PropertySearchContext db = new PropertySearchContext();
          db.Database.Initialize(true);
      }
  
      private void ConfigureApi(HttpConfiguration config)
      {
          var unity = new UnityContainer();
          unity.RegisterType&lt;PropertiesApiController&gt;();
          unity.RegisterType&lt;IUnitOfWork, UnitOfWork&gt;(new HierarchicalLifetimeManager());
          config.DependencyResolver = new IoCContainer(unity);
      }
  </code></pre>
  
  <p>My Controller code:</p>
  
  <pre><code>public class PropertiesApiController : UmbracoApiController
  {
      private readonly IUnitOfWork _unitOfWork;
  
      public PropertiesApiController(IUnitOfWork unitOfWork)
      {
          if(null == unitOfWork)
              throw new ArgumentNullException();
          _unitOfWork = unitOfWork;
      }
  
      public IEnumerable GetAllProperties()
      {
          return new[] {"Table", "Chair", "Desk", "Computer", "Beer fridge"};
      }
  }
  </code></pre>
  
  <p>My Scope Container/IoC Container code: (as per <a href="http://www.asp.net/web-api/overview/extensibility/using-the-web-api-dependency-resolver" rel="nofollow">http://www.asp.net/web-api/overview/extensibility/using-the-web-api-dependency-resolver</a>)</p>
  
  <pre><code>public class ScopeContainer : IDependencyScope
  {
      protected IUnityContainer container;
  
      public ScopeContainer(IUnityContainer container)
      {
          if (container == null)
          {
              throw new ArgumentNullException("container");
          }
          this.container = container;
      }
  
      public object GetService(Type serviceType)
      {
          if (container.IsRegistered(serviceType))
          {
              return container.Resolve(serviceType);
          }
          else
          {
              return null;
          }
      }
  
      public IEnumerable&lt;object&gt; GetServices(Type serviceType)
      {
          if (container.IsRegistered(serviceType))
          {
              return container.ResolveAll(serviceType);
          }
          else
          {
              return new List&lt;object&gt;();
          }
      }
  
      public void Dispose()
      {
          container.Dispose();
      }
  }
  
  public class IoCContainer : ScopeContainer, IDependencyResolver
  {
      public IoCContainer(IUnityContainer container)
          : base(container)
      {
      }
  
      public IDependencyScope BeginScope()
      {
          var child = this.container.CreateChildContainer();
          return new ScopeContainer(child);
      }
  }
  </code></pre>
  
  <p>My IUnitOfWork code:</p>
  
  <pre><code>public interface IUnitOfWork : IDisposable
  {
      GenericRepository&lt;Office&gt; OfficeRepository { get; }
      GenericRepository&lt;Property&gt; PropertyRepository { get; }
      void Save();
      void Dispose(bool disposing);
      void Dispose();
  }
  </code></pre>
  
  <p>My UnitOfWork implementation:</p>
  
  <pre><code>public class UnitOfWork : IUnitOfWork
  {
      private readonly PropertySearchContext _context = new PropertySearchContext();
      private GenericRepository&lt;Office&gt; _officeRepository;
      private GenericRepository&lt;Property&gt; _propertyRepository;
  
      public GenericRepository&lt;Office&gt; OfficeRepository
      {
          get
          {
  
              if (this._officeRepository == null)
              {
                  this._officeRepository = new GenericRepository&lt;Office&gt;(_context);
              }
              return _officeRepository;
          }
      }
      public GenericRepository&lt;Property&gt; PropertyRepository
      {
          get
          {
  
              if (this._propertyRepository == null)
              {
                  this._propertyRepository = new GenericRepository&lt;Property&gt;(_context);
              }
              return _propertyRepository;
          }
      }
  
      public void Save()
      {
          _context.SaveChanges();
      }
  
      private bool disposed = false;
  
      public virtual void Dispose(bool disposing)
      {
          if (!this.disposed)
          {
              if (disposing)
              {
                  _context.Dispose();
              }
          }
          this.disposed = true;
      }
  
      public void Dispose()
      {
          Dispose(true);
          GC.SuppressFinalize(this);
      }
  }
  </code></pre>
  
  <p>I have used unity/DI with MVC4/WebAPI controllers and this implementation of UnitOfWork many times before without issue, so I'm thinking it's Umbraco specific.</p>
  
  <p>I have also debugged the application and made sure that it hits OnApplicationStarted and that its parameters are not null.</p>
  
  <p>The GetAllProperties method in the controller is just a test method to make sure it is all working fine, however, when I try and access this action I get the error:</p>
  
  <p><strong>"The type IUnitOfWork does not have an accessible constructor"</strong></p>
  
  <p>Does anyone have experience with using Umbraco 6.1 and it's UmbracoApiController with dependency injection/Unity?</p>
  
  <p>Also, on an unrelated note, is there a way to return JSON instead of XML in the action? In Web API you would just define the formatter in the WebApi.config but there is none in Umbraco.</p>
  
  <p>Thanks,
  Justin</p>