Search code examples
c#wcfiisservicehost

How to modify WCF service configuration if services hosted on IIS?


I have WCF service (NET 4) hosted on IIS. It configured via web.config. I just want to make some little changes at existsing configuration in runtime. It seems using custom ServiceHostFactory/ServiceHost force me to duplicate all settings in code. Is there any trick?


Solution

  • Yes you can have a ServiceHostfatory :

    <%@ ServiceHost Language="C#" Debug="true"   
                    Service="IISHost.HelloService"   
                    CodeBehind="/App_code/HelloService.svc.cs" 
                    Factory="MyServiceHostFactory" %>
    

    and you can have a ServiceHostFactory that instanciates you service. Because you instanciate your service "as usual" you can have some code that reads the XML configuration -look at code in the comments below :

    public class MyServiceHostFactory : ServiceHostFactory{
     protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses ) {
        ServiceHost host = new ServiceHost(typeof(HelloService ));
        // add/modify the endpoints, Behaviors, ... through  
        // host.Description.Endpoints, host.Description.Behaviors …
        return host;
     }
    }
    

    Regards