Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can WCF Service have constructors?

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;
    }
    ...
}
like image 867
Ray Avatar asked Dec 19 '08 18:12

Ray


People also ask

Can C# class have multiple constructors?

Whenever a class or struct is created, its constructor is called. A class or struct may have multiple constructors that take different arguments.

Are there Constructors in C#?

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.

What is constructor in .NET framework?

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.


2 Answers

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?

like image 96
Kev Avatar answered Nov 03 '22 13:11

Kev


Look at ServiceHostFactory.

like image 6
Mark Cidade Avatar answered Nov 03 '22 14:11

Mark Cidade