0

In web api setup the parameterized constructor AssignmentController(ProfileConfigCAWP pc) is getting called. I need to stop this.

I'm not trying to invoke parameterized .ctor one. Neither Web API pipleline will do, as IMHO web API pipeline will instantiate parameterless .ctor, (unless depency injection comes into picture, which in my case I am not passing ProfileConfigCAWP, anywhere right now.

public class BaseApiController : ApiController
{
    //1st call
    public BaseApiController()
    {

    }
}

//Web Api Controller
public class AssignmentController : BaseApiController
{

    public AssignmentController()
    {

    }

    //2nd call
    public AssignmentController(ProfileConfigCAWP pc)
    {
        //Why does this gets invoked, I'm worried Unity Container depency injection, is playing around
        if (pc != null && pc.LoggedinAccountId != Guid.Empty)
        {
            CAWPConfigure(pc);
        }
    }
}

Even I tried tweaking with base keyword so that base would explicitly know which constructor to call back.

public class BaseApiController : ApiController
{
    //1st call
    public BaseApiController()
    {

    }
}

//Web Api Controller
public class AssignmentController : BaseApiController
{

    public AssignmentController() : base()
    {

    }

    //2nd call
    public AssignmentController(ProfileConfigCAWP pc) : this()
    {
        //Why does this gets invoked, I'm worried Unity Container depency injection, is playing around
        if (pc != null && pc.LoggedinAccountId != Guid.Empty)
        {
            CAWPConfigure(pc);
        }
    }
}

EDIT: I'm not trying to invoke parameterized .ctor one. Neither Web API pipleline will do, as IMHO web API pipeline will instantiate parameterless .ctor, (unless depency injection comes into picture, which in my case I am not passing ProfileConfigCAWP, anywhere right now.

Abhijeet
  • 13,562
  • 26
  • 94
  • 175

1 Answers1

2

By default, Unity chooses a constructor with maximum number of arguments.

To override this behavior you can decorate the desired constructor with the InjectionConstructorAttribute.

public class AssignmentController : BaseApiController
{
    [InjectionConstructor]
    public AssignmentController() : base()
    {
    }
}

An alternative and less intrusive option is to change the registration.

container.RegisterType<AssignmentController>(new InjectionConstructor());
Lennart Stoop
  • 1,649
  • 1
  • 12
  • 18