简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM