I followed this tutorial , which did a great job until I changed my DbContext to have an additional constructor. I'm having permission issues, and I'm not sure what to do to fix this. Is there an easy way to make it capture a constructor without parameters, or am I approaching this incorrectly?
DbContext with two constructors:
public class DashboardDbContext : DbContext { public DashboardDbContext() : base("DefaultConnection") { } public DashboardDbContext(DbConnection dbConnection, bool owns) : base(dbConnection, owns) { } }
SiteController constructor:
private readonly IDashboardRepository _repo; public SiteController(IDashboardRepository repo) { _repo = repo; }
Repository:
DashboardDbContext _context; public DashboardRepository(DashboardDbContext context) { _context = context; }
UnityResolver code:
public class UnityResolver : IDependencyResolver { private readonly IUnityContainer _container; public UnityResolver(IUnityContainer container) { _container = container; } public object GetService(Type serviceType) { try { return _container.Resolve(serviceType); } catch (ResolutionFailedException) { return null; } } public IEnumerable<object> GetServices(Type serviceType) { try { return _container.ResolveAll(serviceType); } catch (ResolutionFailedException) { return new List<object>(); } } public IDependencyScope BeginScope() { var child = _container.CreateChildContainer(); return new UnityResolver(child); } public void Dispose() { _container.Dispose(); } }
WebApiConfig:
var container = new UnityContainer(); container.RegisterType<IDashboardRepository, DashboardRepository>(new HierarchicalLifetimeManager()); config.DependencyResolver = new UnityResolver(container);
Error when calling WebApi:
System.InvalidOperationException: An error occurred while trying to create a controller of type SiteController. Make sure that the controller does not have a constructor without parameters.
at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType) at System.Web.Http.Controllers.HttpControllerDescriptor.CreateController(HttpRequestMessage request) at System.Web.Http.Dispatcher.HttpControllerDispatcher.SendAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken) at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__0.MoveNext()
InnerException: System.ArgumentException: The type "Dashboard.Web.Controllers.SiteController" does not have a default constructor.
at System.Linq.Expressions.Expression.New(Type type) at System.Web.Http.Internal.TypeActivator.Create[TBase](Type instanceType) at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.GetInstanceOrActivator(HttpRequestMessage request, Type controllerType, Func`1& activator) at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)
The tutorial was great and worked fine for me until I added a second constructor.