0

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?

Emmanuel DURIN
  • 4,803
  • 2
  • 28
  • 53
garry
  • 25
  • 5
  • If you modify the configuration file during runtime, you will restart the application. In this case, you have two options - either do the configuration in code, or store the configuration information in a separate file, but then you'll have to read a separate file and may have to create the code to do that. Easier to do it in the code for this scenario, IMO :) – Tim Oct 23 '15 at 18:00

1 Answers1

1

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

Emmanuel DURIN
  • 4,803
  • 2
  • 28
  • 53