When I new a WCF service in my solution, can I do the following, have a constructor with parameter to pass in? If yes, how, when and where does the runtime fill in my required IBusinessLogic object?
[ServiceContract]
public interface IServiceContract
{
[OperationContract]
...
}
public class MyService : IServiceContract
{
IBusinessLogic _businessLogic;
public ServiceLayer(IBusinessLogic businessLogic)
{
_businessLogic = businessLogic;
}
...
}
Whenever a class or struct is created, its constructor is called. A class or struct may have multiple constructors that take different arguments.
Constructors are special methods in C# that are automatically called when an object of a class is created to initialize all the class data members. If there are no explicitly defined constructors in the class, the compiler creates a default constructor automatically.
A Constructor is a special method in a class/struct with the same name as that of class/struct without any return type, used to initialize fields and members of a class/struct; A constructor can only be called by: Compiler using New keyword to initialize a class/struct.
Out of the box WCF will only use the default constructor, you can't use parameterised constructors. You have to do a bit of extra work to make WCF call parameterised constructors.
You could try this:
How do I pass values to the constructor on my wcf service?
Look at ServiceHostFactory.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With