无法编写WCF的编程配置更改 [英] Trouble writing programmatic config changes for WCF

查看:55
本文介绍了无法编写WCF的编程配置更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要能够以编程方式更新我的配置文件并更改WCF设置。我一直在尝试使用一些在Web上找到的示例在一些测试代码中进行此操作,但是未能获得配置文件以反映对端点地址的更改。

I need to be able to update my config file programmatically and change my WCF settings. I've been trying to do this inside of some test code using some of the examples I found on the web but so have not been able to get the config file to reflect a change to an endpoint address.

配置(摘要):


  <!-- Sync Support -->
  <service   name="Server.ServerImpl"
             behaviorConfiguration="syncServiceBehavior">

    <host>
      <baseAddresses>
        <add baseAddress="http://localhost:8000/SyncServerHarness"/>
      </baseAddresses>
    </host>

    <endpoint name="syncEndPoint" 
              address="http://localhost:8000/SyncServerHarness/Sync"
              binding="basicHttpBinding"
              contract="Server.IServer" />

    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />

  </service>

代码:

Configuration config = ConfigurationManager.OpenExeConfiguration
                       (ConfigurationUserLevel.None);

ServiceModelSectionGroup section = (ServiceModelSectionGroup)
                                   config.SectionGroups["system.serviceModel"];

foreach (ServiceElement svc in section.Services.Services)
{
   foreach (ServiceEndpointElement ep in svc.Endpoints)
   {
       if (ep.Name == "syncEndPoint")
       {
          ep.Address = new Uri("http://192.168.0.1:8000/whateverService");

       }
   }
}

config.Save(ConfigurationSaveMode.Full);
ConfigurationManager.RefreshSection("system.serviceModel");

此代码无例外地执行,但不做任何更改。另外,我在索引端点和服务时遇到了麻烦。有找到它的简单方法吗?

This code executes with no exceptions but no changes are made. Also I had trouble indexing the endpoints and services. Is there an easy way to find it? Using name as the indexer did not seem to work.

谢谢!

Sieg

推荐答案

我改变了两件事,对我来说也很好:

I changed two things, and it works just fine for me:

1)我我在组装路径中使用 OpenExeConfiguration

1) I am using OpenExeConfiguration with the assembly path

2)我正在访问< services> 部分,而不是< system.serviceModel> 部分组

2) I'm accessing the <services> section, rather than the <system.serviceModel> section group

有了这两个更改,一切工作就很好了:

With those two changes, everything works just fine:

Configuration config = ConfigurationManager.OpenExeConfiguration
                       (Assembly.GetExecutingAssembly().Location);

ServicesSection section = config.GetSection("system.serviceModel/services") 
                             as ServicesSection;

foreach (ServiceElement svc in section.Services)
{
   foreach (ServiceEndpointElement ep in svc.Endpoints)
   {
       if (ep.Name == "syncEndPoint")
       {
          ep.Address = new Uri("http://192.168.0.1:8000/whateverService");
       }
   }
}

config.Save(ConfigurationSaveMode.Full);

这篇关于无法编写WCF的编程配置更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆