RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
WCF修改App.config配置文件技巧分享

WCF对App.config配置文件的修改方法我们将会通过以下这段代码为大家详细介绍相关操作方法。希望对于又需要的朋友们能够从今天介绍的内容中获得一些帮助,来解决他们在实际应用中碰到的一些问题。

WCF修改App.config配置文件代码示例:

 
 
 
  1. using System.ServiceModel.Configuration;using System.Text.
    RegularExpressions;// 修改配置文件
  2. private void ChanageConfig()
  3. {
  4. Configuration config = ConfigurationManager.OpenExeConfiguration
    (Application.ExecutablePath);
  5. ConfigurationSectionGroup sct = config.SectionGroups
    ["system.serviceModel"];
  6. ServiceModelSectionGroup serviceModelSectionGroup = 
    sct as ServiceModelSectionGroup;
  7. ClientSection clientSection = serviceModelSectionGroup.Client;
  8. foreach (ChannelEndpointElement item in clientSection.Endpoints)
  9. {
  10. string pattern = "://.*/";
  11. string address = item.Address.ToString();
  12. string replacement = string.Format("://{0}:{1}/", 
    Global.ServerIP, Global.ServerPort);
  13. address = Regex.Replace(address, pattern, replacement);
  14. item.Address = new Uri(address);
  15. }
  16. config.Save(ConfigurationSaveMode.Modified);
  17. ConfigurationManager.RefreshSection("system.serviceModel");
  18. return;
  19. /*
  20. Configuration configuration = ConfigurationManager.
    OpenExeConfiguration(ConfigurationUserLevel.None);
  21. ServiceModelSectionGroup serviceModelSectionGroup = 
    ServiceModelSectionGroup.GetSectionGroup(configuration);
  22. ClientSection clientSection = serviceModelSectionGroup.Client;
  23. foreach(ChannelEndpointElement item in clientSection.Endpoints)
  24. {
  25. MessageBox.Show(item.Address.Host);
  26. }
  27. configuration.Save();
  28. */
  29. return;
  30. XmlDocument xmlDoc = new XmlDocument();
  31. xmlDoc.Load("Rca.exe.config");
  32. XmlNodeList nodeList = xmlDoc.SelectSingleNode
    ("configuration/appSettings").ChildNodes;
  33. foreach (XmlNode node in nodeList)
  34. {
  35. switch (node.Attributes["key"].InnerText.ToLower())
  36. {
  37. case "serverip":
  38. node.Attributes["value"].InnerText = Global.ServerIP;
  39. break;
  40. case "serverport":
  41. node.Attributes["value"].InnerText = Global.ServerPort;
  42. break;
  43. case "langdataid":
  44. node.Attributes["value"].InnerText = Global.LangDataID;
  45. break;
  46. case "uidataid":
  47. node.Attributes["value"].InnerText = Global.UIDataID;
  48. break;
  49. }
  50. }
  51. nodeList = xmlDoc.SelectSingleNode("configuration/
    system.serviceModel/client").ChildNodes;
  52. foreach (XmlNode node in nodeList)
  53. {
  54. string pattern = "://.*/";
  55. string address = node.Attributes["address"].InnerText;
  56. string replacement = string.Format("://{0}:{1}/", 
    Global.ServerIP, Global.ServerPort);
  57. address = Regex.Replace(address, pattern, replacement);
  58. node.Attributes["address"].InnerText = address;
  59. if (node.Attributes["contract"].InnerText == "LogicCommon")
  60. {
  61. LogicCommonCfgName = node.Attributes["name"].InnerText;
  62. LogicCommonAddress = node.Attributes["address"].InnerText;
  63. }
  64. }
  65. xmlDoc.Save("Rca.exe.config");
  66. }

以上就是我们为大家介绍的WCF修改App.config配置文件的全部操作步骤。


新闻标题:WCF修改App.config配置文件技巧分享
文章链接:http://www.jxjierui.cn/article/dpjocog.html