Andry Andry - 10 months ago 46
C# Question

Explicitly calling a service constructor when hosting a WCF web service on IIS

I want to host a WCF service of mine on Microsoft IIS (IIS hosting).

To do this, I created my service:

// The service
public class MyService : IMyService {
// Ctors
public MyService() {
// Def ctor: I don't want to call it
public MyService(...) : this() {
// Parametric ctor, I want to call it!

// The contract
public interface IMyService {

I created a svc file (a good approach to give a base address to my service):

<@ServiceHost Service="MyService" @>

But doing so, when hosting my service (simply creating a virtual directory in IIS pointing to the folder where my application resides, the project directory usually), IIS will call the default constructor.

How do I make IIS call a different constructor?

PS: I know that it is possible to specify a
. Is it something I should use here? It gives me back the factory and a host. For the host, I cannot act on the host passed parameters. However, how do I solve this problem?

NOTE: I understood that many solutions are based on Inversion of control (IoC) and several IoC frameworks like Unity, Castle Project and Spring.NET. However I would not really use them at all. Can WCF make this on its own? I cannot accept that WCF cannot let a programmer host a service by constructing it in the appropriate manner...

Answer Source

You must write several custom classes to support creating service with parameters:

  • Custom class implementing IInstanceProvider. This class will be responsible for creating your service instance with your non default constructor.
  • Custom class implementing IServiceBehavior. This class will be responsible for adding custom instance provider into endpoint dispatcher.
  • Custom ServiceHost which will apply your behavior.
  • Custom ServiceHostFactory which will instantiate your custom service host. You will reference this factory from .svc file.

This is generally the same as building support for dependency injection. You can check for example this article.