5

The problem:

I get the following error when I build:

"'.Controllers.ControllerBase' does not contain a constructor that takes 0 arguments"

My base controller looks like this:

public abstract class ControllerBase : Controller
{
    public CompanyChannel<IAuthorizationService> authorizationServiceClient;
         public ControllerBase(CompanyChannel<IAuthorizationService> authService)
    {
        this.authorizationServiceClient = authService;
    }
}

An example controller that makes use of the Base..

public partial class SearchController : ControllerBase
{
    protected CompanyChannel<IComplaintTaskService> complaintTaskServiceChannel;
    protected IComplaintTaskService taskServiceClient;      

    protected ComplaintSearchViewModel searchViewModel;

    #region " Constructor "

    public SearchController(CompanyChannel<IComplaintTaskService> taskService, CompanyChannel<IAuthorizationService> authService, ComplaintSearchViewModel viewModel)
        : base(authService)
    {
        searchViewModel = viewModel;
        this.complaintTaskServiceChannel = taskService;
        this.taskServiceClient = complaintTaskServiceChannel.Channel;
    }

    #endregion

    public virtual ActionResult Index()
    {
        return View();
    }
}

This seems to be tripping T4MVC.

Should I just not be passing params into the base constructor?

David McLean
  • 1,462
  • 1
  • 12
  • 27
  • It seems fine, can you double check the where the error coming from. I noticed your ControllerBase constructor name is 'HavenControllerBase' I believe it's a typo – Nasmi Sabeer Jan 16 '13 at 15:56
  • It was a typo, good spot. Fixed it now. The error is from SearchController.generated.cs - I can see that the generated code has a constructor that does not call on the base controller. – David McLean Jan 16 '13 at 16:03
  • This is I think the same as http://stackoverflow.com/questions/6730989/t4mvc-cannot-inherit-a-controller-class-which-has-no-default-constructor, which was never quite resolved. – David Ebbo Jan 16 '13 at 23:31
  • @David Ebbo - I think it is the same issue, I have just added a public default constructor so it works. Not a huge problem really. – David McLean Jan 18 '13 at 08:38

1 Answers1

3

Your abstract class must have a default constructor. When you have any constructors in the subclasses that doesn't call the base class ctor means, compiler will automatically call base class's default ctor, therefore you must have one in base class.

Following demo will be helpful to understand ctor chaining in c#

class Base
{
    public Base()
    {
        Console.WriteLine("Base() called");
    }

    public Base(int x)
    {
        Console.WriteLine("Base(int x) called");
    }
}

class Sub : Base
{
    public Sub()
    {
        Console.WriteLine("Sub() called");     
    }
}

and from within your Main() create

new Sub();

and observe the console output

Nasmi Sabeer
  • 1,370
  • 9
  • 21
  • Thanks, I kind of wanted to force people to use the constructor provided, as it now they would have to as it wont compile unless they do. By adding the default constructor it will compile if they don't use the constructor I want them to use, which will side step some constructor injection. If there is no other way then I guess that's what I will have to do. – David McLean Jan 16 '13 at 16:38