Explicit service constructor call when hosting a WCF web service in IIS

I want to host my WCF service 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
[ServiceContract]
public interface IMyService {
    ...
}

I created an svc file (a good approach to provide a base address for my service):

<@ServiceHost Service="MyService" @>

But at the same time, when placing my service (just creating a virtual directory in IIS that points to the folder where my application is located, usually this is the project directory), IIS will call the default constructor.

How do I get IIS to call another constructor?

PS: I know what you can specify HostServiceFactory. Is that what I should use here? He returns me the factory and host. For the host, I cannot act on the parameters passed by the host. However, how to solve this problem?

. , (IoC) IoC, Unity, Spring.NET. . WCF ? , WCF , ...

+3
3

:

  • , IInstanceProvider. .
  • IServiceBehavior. .
  • ServiceHost, .
  • ServiceHostFactory, . factory .svc.

, . , , .

+6

, IInstanceProvider IServiceBehavior :

IInstanceProvider

public class ServiceInstanceProvider : IInstanceProvider
{
    public object GetInstance(InstanceContext instanceContext)
    {
        return this.GetInstance(instanceContext, null);
    }

    public object GetInstance(InstanceContext instanceContext, Message message)
    {
        return new MyService(...);
    }

    public void ReleaseInstance(InstanceContext instanceContext, object instance)
    {}
}

IServiceBehavior

public class InstanceProviderBehaviorAttribute : Attribute, IServiceBehavior
{
    public void AddBindingParameters(ServiceDescription serviceDescription,
            ServiceHostBase serviceHostBase,
            Collection<ServiceEndpoint> endpoints,
            BindingParameterCollection bindingParameters)
    {}

    public void ApplyDispatchBehavior(ServiceDescription serviceDescription,
            ServiceHostBase serviceHostBase)
    {
        foreach (ChannelDispatcher cd in serviceHostBase.ChannelDispatchers)
        {
            foreach (EndpointDispatcher ed in cd.Endpoints)
            {
                if (!ed.IsSystemEndpoint)
                {
                    ed.DispatchRuntime.InstanceProvider = new ServiceInstanceProvider();
                }
            }
        }
    }

    public void Validate(ServiceDescription serviceDescription,
            ServiceHostBase serviceHostBase)
    {}
}

MyService with custom attribute ServiceBehavior

[InstanceProviderBehavior]
public class MyService : IMyService {
    public MyService() { }
    public MyService(...) : this() {
        ...
    }
   ...
}

More on this here:

0
source

All Articles