简体   繁体   English

如果服务托管在IIS上,如何修改WCF服务配置?

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

I have WCF service (NET 4) hosted on IIS. 我在IIS上托管了WCF服务(NET 4)。 It configured via web.config. 它是通过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. 似乎使用自定义ServiceHostFactory / ServiceHost迫使我重复代码中的所有设置。 Is there any trick? 有没有把戏?

Yes you can have a ServiceHostfatory : 是的,您可以拥有一个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. 并且您可以拥有一个ServiceHostFactory来实例化您的服务。 Because you instanciate your service "as usual" you can have some code that reads the XML configuration -look at code in the comments below : 因为您可以像往常一样实例化服务,所以可以有一些读取XML配置的代码-在下面的注释中查看代码:

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 问候

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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