读书人

怎的在运行时读取及修改app.config文件

发布时间: 2012-12-31 11:57:51 作者: rapoo

怎样在运行时读取及修改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);
? ? ? ? ? ? }
参考


可以试试

读书人网 >Web Service

热点推荐