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 : IMySer
If all you need is to call a specific constructor in your service, then you only need to implement an IInstanceProvider
and attach an IServiceBehavior
to your service:
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)
{}
}
public class InstanceProviderBehaviorAttribute : Attribute, IServiceBehavior
{
public void AddBindingParameters(ServiceDescription serviceDescription,
ServiceHostBase serviceHostBase,
Collection 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)
{}
}
[InstanceProviderBehavior]
public class MyService : IMyService {
public MyService() { }
public MyService(...) : this() {
...
}
...
}
More info on this here: