I have an existing MVC controller with a parameterized constructor and it works just fine until I tried to inject a new interface in the constructor. below is an example of the original code that works
public class HomeController : BaseController
{
private readonly IApartment apartment;
public HomeController(IApartment apartment)
{
this.apartment = apartment ?? throw new ArgumentNullException(nameof(apartment));
}
}
And in UnityConfig.cs
public static void RegisterComponents()
{
Container.RegisterType<IApartment, Apartment>();
}
I have a new interface that I would like to use in this controller. Below is how I modified the code
public class HomeController : BaseController
{
private readonly IApartment apartment;
private readonly ITenant tenant;
public HomeController(IApartment apartment, ITenant tenant)
{
this.apartment = apartment ?? throw new ArgumentNullException(nameof(apartment));
this.tenant = tenant ?? throw new ArgumentNullException(nameof(tenant));
}
}
I have also registered it in UnityConfig.cs
public static void RegisterComponents()
{
Container.RegisterType<IApartment, Apartment>();
Container.RegisterType<ITenant, Tenant>();
}
When I run my project I get the below error
An error occurred when trying to create a controller of type ‘PropertyManager.UI.Controllers.HomeController’. Make sure that the controller has a parameterless public constructor.
When I remove ITenant
references then the project runs.
Can someone please assist? Where do I get this wrong?