在运行时的 APP.Config C# 中添加节点 system.serviceModel

Add node system.serviceModel in APP.Config C# in runtime

我需要在我的 app.config 文件中添加此部分,而我的程序是 运行

<system.serviceModel>
<bindings>
  <basicHttpBinding>
    <binding name="InvioTelematicoSS730pMtomPortBinding" messageEncoding="Mtom">
      <security mode="Transport" />
    </binding>
    <binding name="RicevutaPdf730PortBinding">
      <security mode="Transport" />
    </binding>
    <binding name="RicevutaPdf730PortBinding1" />
  </basicHttpBinding>
</bindings>
<client>
  <endpoint address="http://localhost:9080/InvioTelematicoSS730pMtomWeb/InvioTelematicoSS730pMtomPort"
      binding="basicHttpBinding" bindingConfiguration="InvioTelematicoSS730pMtomPortBinding"
      contract="InvioFlussi730.InvioTelematicoSS730pMtom" name="InvioTelematicoSS730pMtomPort" />
  <endpoint address="https://invioSS730pTest.sanita.finanze.it/Ricevute730ServiceWeb/ricevutePdf"
      binding="basicHttpBinding" bindingConfiguration="RicevutaPdf730PortBinding"
      contract="ServiceReference1.RicevutaPdf730" name="RicevutaPdf730Port" />
</client>

有什么办法吗?提前致谢

.NET 为 WCF 服务公开配置元素 类 以在运行时管理它们。编写了一个简单的方法来为您构建和生成这些部分。

private static void WriteWCFConfig()
{
    //standard method from System.Configuration 
    Configuration appConfig = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

    //the main section in the app.config file for WCF 
    ServiceModelSectionGroup serviceModel = ServiceModelSectionGroup.GetSectionGroup(appConfig);

    var httpBindings = serviceModel.Bindings.BasicHttpBinding;
    if (!httpBindings.ContainsKey("InvioTelematicoSS730pMtomPortBinding"))
    {
        BasicHttpBindingElement newHttpBindng = new BasicHttpBindingElement("InvioTelematicoSS730pMtomPortBinding");
        newHttpBindng.MessageEncoding = WSMessageEncoding.Mtom;
        newHttpBindng.Security.Mode = BasicHttpSecurityMode.Transport;

        httpBindings.Bindings.Add(newHttpBindng);
    }
    if (!httpBindings.ContainsKey("RicevutaPdf730PortBinding"))
    {
        BasicHttpBindingElement newHttpBindng = new BasicHttpBindingElement("RicevutaPdf730PortBinding");
        newHttpBindng.MessageEncoding = WSMessageEncoding.Mtom;
        newHttpBindng.Security.Mode = BasicHttpSecurityMode.Transport;

        httpBindings.Bindings.Add(newHttpBindng);
    }

    //the section 
    ChannelEndpointElementCollection endPoints = serviceModel.Client.Endpoints;

    //Get endpoint names
    List<string> endpointNames = new List<string>();
    foreach (ChannelEndpointElement endpointElement in endPoints)
    {
        endpointNames.Add(endpointElement.Name);
    }

    if (!endpointNames.Contains("InvioTelematicoSS730pMtomPort"))
    {
        ChannelEndpointElement endPoint = new ChannelEndpointElement(new EndpointAddress("http://localhost:9080/InvioTelematicoSS730pMtomWeb/InvioTelematicoSS730pMtomPort"), "InvioFlussi730.InvioTelematicoSS730pMtom");
        endPoint.Name = "InvioTelematicoSS730pMtomPort";
        endPoint.Binding = "basicHttpBinding";
        endPoint.BindingConfiguration = "InvioTelematicoSS730pMtomPortBinding";

        endPoints.Add(endPoint);
    }
    if (!endpointNames.Contains("RicevutaPdf730Port"))
    {
        ChannelEndpointElement endPoint = new ChannelEndpointElement(new EndpointAddress("https://invioSS730pTest.sanita.finanze.it/Ricevute730ServiceWeb/ricevutePdf"), "ServiceReference1.RicevutaPdf730");
        endPoint.Name = "RicevutaPdf730Port";
        endPoint.Binding = "basicHttpBinding";
        endPoint.BindingConfiguration = "RicevutaPdf730PortBinding";

        endPoints.Add(endPoint);
    }

    appConfig.Save();
}

大家可以拿去根据自己的需要修改。

希望对您有所帮助。