简体   繁体   中英

Modify web.config system.ServiceModel/client/endpoint with Microsoft.Web.Administration.ServerManager

I'm having a bit of grief trying to modify my web applications web.config file using the Microsoft.Web.Administration.ServerManager library.

What I am trying to do is modify the client section located in System.ServiceModel .

Basically I would like to take an entry like this

<system.serviceModel>
    <client>
        <endpoint address="net.tcp://localhost:123/MyService.svc"
                  behaviorConfiguration="DefaultBehaviour" binding="netTcpBinding"
                  bindingConfiguration="TCPBinding" contract="MyService.IMyService"
                  name="MyService" />
    </client>
</system.serviceModel>

and change it to this

<system.serviceModel>
    <client>
        <endpoint address="net.tcp://192.168.0.1:123/MyService.svc"
                  behaviorConfiguration="DefaultBehaviour" binding="netTcpBinding"
                  bindingConfiguration="TCPBinding" contract="MyService.IMyService"
                  name="MyService" />
    </client>
</system.serviceModel>

I've been able to get as far as retrieving the SectionGroup as such

using (ServerManager server = new ServerManager())
{        
    var siteConfig = server.Sites.First().GetWebConfiguration();
    var clientSection = siteConfig.GetEffectiveSectionGroup().SectionGroups["system.ServiceModel"].Sections["client"];
}

but I am completely stuck as to how I can modify the actual entry.

Any guidance would be sincerely appreciated.

You can modify the attribute like this:

 using (ServerManager server = new ServerManager()) 
 {
     var siteConfig = server.Sites.First().GetWebConfiguration();
     var section = siteConfig.GetSection("system.serviceModel/client/endpoint");
     section.SetAttributeValue("address", "net.tcp://192.168.0.1:123/MyService.svc");
     server.CommitChanges();
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM