Unity Bootstrapper (Unity.Mvc), Unity 3, MVC 5, EF6 Receiving Error: Parameterless Public Constructor on Controller <p>Ok, after searching Google, here and several ASP/MVC forums I am bound to have to ask what the hell I am doing wrong here.</p> <p>I have a good start to my application, an ok understanding of DI, IoC and am using the Repository, Service and UnitOfWork patterns. When I attempt to load a controller that needs the DI from Unity, it's as if unity is not resolving any of the registered items, or that I have done it poorly. From all the examples I can see for this version (not the version that creates the Bootstrap.cs file that is then called from Global.asax) I am doing what others have done with no love from Unity.</p> <p>My core question is: Have I setup/configured Unity to inject the items into the controller constructor as needed or not. If I have, any ideas why it's not working like examples I have seen?</p> <p>I keep getting the error that the AssetController needs to have a parameterless public constructor. If I add one, then it uses it without the DI and if I don't add one, then it yells about not having it.</p> <p>Thanks, code below.</p> <p><strong>UnityConfig.cs</strong></p> <pre><code>namespace CARS.web.App_Start { /// &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) { // NOTE: To load from web.config uncomment the line below. Make sure to add a Microsoft.Practices.Unity.Configuration to the using statements. // container.LoadConfiguration(); // TODO: Register your types here // container.RegisterType&lt;IProductRepository, ProductRepository&gt;(); container.RegisterType&lt;IDataContext, CARSDEMOContext&gt;(new PerRequestLifetimeManager()) .RegisterType&lt;IAssetService, AssetService&gt;() .RegisterType&lt;IUnitOfWork, UnitOfWork&gt;() .RegisterType&lt;IRepository&lt;Asset&gt;, Repository&lt;Asset&gt;&gt;(); //.RegisterType&lt;AssetController&gt;(new InjectionConstructor(typeof(IAssetService), typeof(IUnitOfWork))); } } } </code></pre> <p><strong>AssetController.cs</strong> (constructor portion where I am doing the injection params)</p> <pre><code>namespace CARS.web.Controllers { public class AssetController : Controller { private readonly IAssetService _assetService; private readonly IUnitOfWork _unitOfWork; public AssetController(IAssetService assetService, IUnitOfWork unitOfWork) { _assetService = assetService; _unitOfWork = unitOfWork; } //other methods for CRUD etc stripped for brevity } } </code></pre> <p><strong>IAssetService.cs</strong> (first param is the assetService )</p> <pre><code>namespace CARS.service { public interface IAssetService : IService&lt;Asset&gt; { Task&lt;IEnumerable&lt;Asset&gt;&gt; GetAsync(); Task&lt;Asset&gt; FindAsync(Guid id); Asset Add(Asset asset); Asset Update(Asset asset); void Remove(Guid id); } } </code></pre> <p><strong>AssetService.cs</strong> (concrete implementation for IAssetService interaction)</p> <pre><code>namespace CARS.service { public class AssetService : Service&lt;Asset&gt;, IAssetService { private readonly IRepositoryAsync&lt;Asset&gt; _repository; public AssetService(IRepositoryAsync&lt;Asset&gt; repository) : base(repository) { _repository = repository; } public Task&lt;IEnumerable&lt;Asset&gt;&gt; GetAsync() { //return _repository.Query().SelectAsync(); return _repository.Query().SelectAsync(); } public Task&lt;Asset&gt; FindAsync(Guid id) { return _repository.FindAsync(id); } public Asset Add(Asset asset) { _repository.Insert(asset); return asset; } public Asset Update(Asset asset) { _repository.Update(asset); return asset; } public void Remove(Guid id) { _repository.Delete(id); } } } </code></pre> <p><strong>IUnitOfWork.cs</strong> (this is from Long Le's Generic UofW and Repository - <a href="http://genericunitofworkandrepositories.codeplex.com/" rel="nofollow">http://genericunitofworkandrepositories.codeplex.com/</a>)</p> <pre><code>namespace Repository.Pattern.UnitOfWork { public interface IUnitOfWork : IDisposable { int SaveChanges(); Task&lt;int&gt; SaveChangesAsync(); void Dispose(bool disposing); IRepository&lt;TEntity&gt; Repository&lt;TEntity&gt;() where TEntity : IObjectState; void BeginTransaction(); bool Commit(); void Rollback(); } } </code></pre> <p><strong>UnitOfWork.cs</strong> (again from Long Le's framework)</p> <pre><code>namespace Repository.Pattern.Ef6 { public class UnitOfWork : IUnitOfWork, IUnitOfWorkAsync { #region Private Fields private readonly IDataContextAsync _dataContext; private bool _disposed; private ObjectContext _objectContext; private Dictionary&lt;string, object&gt; _repositories; private DbTransaction _transaction; #endregion Private Fields #region Constuctor/Dispose public UnitOfWork(IDataContextAsync dataContext) { _dataContext = dataContext; } public void Dispose() { if (_objectContext != null &amp;&amp; _objectContext.Connection.State == ConnectionState.Open) _objectContext.Connection.Close(); Dispose(true); GC.SuppressFinalize(this); } public virtual void Dispose(bool disposing) { if (!_disposed &amp;&amp; disposing) _dataContext.Dispose(); _disposed = true; } #endregion Constuctor/Dispose public int SaveChanges() { return _dataContext.SaveChanges(); } public IRepository&lt;TEntity&gt; Repository&lt;TEntity&gt;() where TEntity : IObjectState { return RepositoryAsync&lt;TEntity&gt;(); } public Task&lt;int&gt; SaveChangesAsync() { return _dataContext.SaveChangesAsync(); } public Task&lt;int&gt; SaveChangesAsync(CancellationToken cancellationToken) { return _dataContext.SaveChangesAsync(cancellationToken); } public IRepositoryAsync&lt;TEntity&gt; RepositoryAsync&lt;TEntity&gt;() where TEntity : IObjectState { if (_repositories == null) _repositories = new Dictionary&lt;string, object&gt;(); var type = typeof (TEntity).Name; if (_repositories.ContainsKey(type)) return (IRepositoryAsync&lt;TEntity&gt;) _repositories[type]; var repositoryType = typeof (Repository&lt;&gt;); _repositories.Add(type, Activator.CreateInstance(repositoryType.MakeGenericType(typeof (TEntity)), _dataContext, this)); return (IRepositoryAsync&lt;TEntity&gt;) _repositories[type]; } #region Unit of Work Transactions public void BeginTransaction() { _objectContext = ((IObjectContextAdapter) _dataContext).ObjectContext; if (_objectContext.Connection.State != ConnectionState.Open) { _objectContext.Connection.Open(); _transaction = _objectContext.Connection.BeginTransaction(); } } public bool Commit() { _transaction.Commit(); return true; } public void Rollback() { _transaction.Rollback(); ((DataContext)_dataContext).SyncObjectsStatePostCommit(); } #endregion // Uncomment, if rather have IRepositoryAsync&lt;TEntity&gt; IoC vs. Reflection Activation //public IRepositoryAsync&lt;TEntity&gt; RepositoryAsync&lt;TEntity&gt;() where TEntity : EntityBase //{ // return ServiceLocator.Current.GetInstance&lt;IRepositoryAsync&lt;TEntity&gt;&gt;(); //} } } </code></pre> <p><strong>Updated to include the SetResolver info from UnityMvcActivator.cs</strong></p> <pre><code>using System.Linq; using System.Web.Mvc; using Microsoft.Practices.Unity.Mvc; [assembly: WebActivatorEx.PreApplicationStartMethod(typeof(CARS.web.App_Start.UnityWebActivator), "Start")] namespace CARS.web.App_Start { /// &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)); } } } </code></pre> <p>I have read/tried the following info/data and nothing has fixed it:</p> <p><a href="http://stackoverflow.com/questions/22846327/the-type-iuserstore1-does-not-have-an-accessible-constructor">The type IUserStore`1 does not have an accessible constructor</a></p> <p><a href="http://stackoverflow.com/questions/20023065/how-to-add-mvc-5-authentication-to-unity-ioc">How to add MVC 5 authentication to Unity IoC?</a></p> <p><a href="http://stackoverflow.com/questions/20489460/types-not-resolving-with-unity-mvc-5">Types not resolving with Unity [MVC 5]</a></p> <p>I have ready where one must write a ControllerFactory for Unity to be able to do this, but that seems quite a bit of work when all the examples I have found simply have the config registered, and the injection apparently happening on the controllers and other classes as need.</p> <p>And finally the error:</p> <pre><code>The following server error was encountered: An error occurred when trying to create a controller of type 'CARS.web.Controllers.AssetController'. Make sure that the controller has a parameterless public constructor.Details are: at System.Web.Mvc.DefaultControllerFactory.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType) at System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext, String controllerName) at System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController&amp; controller, IControllerFactory&amp; factory) at System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionSte p.Execute() at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean&amp; completedSynchronously) </code></pre> <p>Thanks</p>