C# 参数化构造函数困境,源于基类?
在web api设置中,将调用参数化构造函数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
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,我将标记正确。