2012-04-23 36 views
0

我有win-service托管WCF服务。 Win-service正在计算机“MyComp1”上运行。在WCF服务的App.config样子:WCF:在安装过程中或运行时修改baseAddress

 <baseAddresses> 
     <add baseAddress="http://localhost:8732/MyService" /> 
     </baseAddresses> 

当我试图从服务导入WSDL(例如采用Delphi WSDLImp.exe)我越来越喜欢错误“无法导入http://localhost:8732/MyService?xsd=xsd0这是正确的行为导致服务未在本地主机上运行。但生成的WSDL中的XSD位置包含类似localhost的地址。

现在我想在安装过程中或运行时修改baseAddress,因为我不想让用户手动编辑App.config。 我听说过FlatWSDL,但有没有其他技术可以做到这一点?

回答

3

您可以使用System.Xml.XmlDocument以编程方式更改App.config文件。

XmlDocument xmlDoc = new XmlDocument(); 

xmlDoc.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile); 

xmlDoc.SelectNodes("/configuration/system.serviceModel/services/service/host/baseAddresses/add") 
    .Cast<XmlNode>().ToList() 
    .ForEach(o => o.Attributes["baseAddress"].Value = "http://localhost:8732/MyService"); 

xmlDoc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile); 

只要确保使用您的基地址的正确XPath表达式。希望这可以帮助。

+0

在你的代码去的wcf服务? – ZoomVirus 2014-10-06 14:59:13