I have a class with 2 constructors.
MyClass()
and
MyClass(IMyService service)
How do I tell StructureMap then whenever I do a ‘new MyClass()’ it should actually call the second constructor and not the first constructor.
Please help.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If you call
new MyClass(), then StructureMap is not involved at all. No amount of StructureMap configuration will change the behavior.If you call
ObjectFactory.GetInstance<MyClass>(), StructureMap will by default call the constructor with more parameters.If you want StructureMap to use a different constructor, you can specify the constructor (via PHeiberg’s answer):
Or you can just tell StructureMap explicitly how to create the instance using the overload of
Use()that accepts aFunc<>: