Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 参数化构造函数困境,源于基类?_C#_Oop_Asp.net Web Api_Constructor - Fatal编程技术网

C# 参数化构造函数困境,源于基类?

C# 参数化构造函数困境,源于基类?,c#,oop,asp.net-web-api,constructor,C#,Oop,Asp.net Web Api,Constructor,在web api设置中,将调用参数化构造函数AssignmentController(ProfileConfigCAWP pc)。我需要阻止这一切 我不想调用参数化的.ctorOne。都不是Web API pipleline将这样做,因为IMHO web API管道将实例化 无参数ctor(除非依赖注入进入画面, 在我的情况下,我不会在任何地方传递ProfileConfigCAWP 现在 甚至我也尝试了调整base关键字,以便base能够明确知道要调用哪个构造函数 public class Ba

在web api设置中,将调用参数化构造函数
AssignmentController(ProfileConfigCAWP pc)
。我需要阻止这一切

我不想调用参数化的.ctorOne。都不是Web API pipleline将这样做,因为IMHO web API管道将实例化 无参数ctor(除非依赖注入进入画面, 在我的情况下,我不会在任何地方传递
ProfileConfigCAWP
现在

甚至我也尝试了
调整base关键字
,以便base能够明确知道要调用哪个构造函数

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);
        }
    }
}
编辑:
我没有尝试调用parameterized.ctor one。Web API pipleline都不行,因为IMHO Web API pipeline将实例化parameterless.ctor,(除非依赖注入出现在图片中,在我的情况下,我现在不会在任何地方传递
ProfileConfigCAWP

默认情况下,
Unity
选择具有最大参数数的构造函数

要覆盖此行为,可以使用
InjectionConstructorAttribute
装饰所需的构造函数

public class AssignmentController : BaseApiController
{
    [InjectionConstructor]
    public AssignmentController() : base()
    {
    }
}
另一个不那么麻烦的选择是更改注册

container.RegisterType<AssignmentController>(new InjectionConstructor());
container.RegisterType(新注入构造函数());

默认情况下,
Unity
选择具有最大参数数的构造函数

要覆盖此行为,可以使用
InjectionConstructorAttribute
装饰所需的构造函数

public class AssignmentController : BaseApiController
{
    [InjectionConstructor]
    public AssignmentController() : base()
    {
    }
}
另一个不那么麻烦的选择是更改注册

container.RegisterType<AssignmentController>(new InjectionConstructor());
container.RegisterType(新注入构造函数());

可能重复:@LennartStoop,谢谢听起来很有说服力。将其发布为asnswer,我将标记正确。可能重复:@LennartStoop,谢谢听起来很有说服力。将其发布为asnswer,我将标记正确。