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

时间:2018-01-26 12:52:59

标签: c# runtime app-config

我需要在程序运行时将此部分添加到我的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>

有什么办法吗?提前致谢

1 个答案:

答案 0 :(得分:0)

.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();
}

您可以根据自己的需要进行修改。

希望这有帮助。