首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web Service >

怎样在运行时读取及修改app.config文件中的wcf配置内容?解决方案

2012-04-05 
怎样在运行时读取及修改app.config文件中的wcf配置内容?服务端app.config文件内容如下:?xml version1.0

怎样在运行时读取及修改app.config文件中的wcf配置内容?
服务端app.config文件内容如下:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.serviceModel>
  <services>
  <service behaviorConfiguration="NewBehavior" name="Learn.Library.WCF.MyService">
  <host>
  <baseAddresses>
  <add baseAddress="http://localhost:8080/MyService" />
  </baseAddresses>
  </host>
  <endpoint address="" binding="basicHttpBinding"
  contract="Learn.Library.WCF.IContract" />
  <endpoint address="mex" binding="mexHttpBinding"
  contract="IMetadataExchange" />
  </service>
  </services>
  <behaviors>
  <serviceBehaviors>
  <behavior name="NewBehavior">
  <serviceMetadata httpGetEnabled="true" />
  </behavior>
  </serviceBehaviors>
  </behaviors>
  </system.serviceModel>
</configuration>

我想在运行时读取及修改
<add baseAddress="http://localhost:8080/MyService" />
引号中间的字符串,如何实现?

[解决办法]
Configuration conf = ConfigurationManager.OpenExeConfiguration(Assembly.GetEntryAssembly().Location);
ServiceModelSectionGroup svcmod = (ServiceModelSectionGroup)conf.GetSectionGroup("system.serviceModel");
foreach (ServiceElement el in svcmod.Services.Services)
{
Type svcType = Type.GetType(el.Name + "," + "");
if (svcType == null)
throw new Exception("Invalid");
aServiceHost = new ServiceHost(svcType);
}
参考
[解决办法]

探讨
Configuration conf = ConfigurationManager.OpenExeConfiguration(Assembly.GetEntryAssembly().Location);
            ServiceModelSectionGroup svcmod = (ServiceModelSectionGroup)conf.GetSectionGroup("system.serviceModel");
            foreach (ServiceElement el in svcmod.Services.Services)
            {
                Type svcType = Type.GetType(el.Name + "," + "");
                if (svcType == null)
                    throw new Exception("Invalid");
                aServiceHost = new ServiceHost(svcType);
            }
参考

热点排行