1

I have the need to add to my app.config file this section while my program is running

<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>

is there any way possible? Thanks in advance

Stefano Aquila
  • 107
  • 1
  • 10
  • Possible duplicate of [Write values in app.config file](https://stackoverflow.com/questions/4758598/write-values-in-app-config-file) – gilliduck Jan 26 '18 at 13:00
  • Sorry but it doesn't help...I would like to know if there is the possibility to add the string i have pasted to the file app.config while the project is running...How will be possible to add the system.serviceModel sections in c#?May i have some help? – Stefano Aquila Jan 26 '18 at 13:30

1 Answers1

1

.NET exposes configuration element classes for WCF services to manage them at runtime. Wrote a simple method to construct and generate the sections for you.

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

You can take it and modify it according to your needs.

Hope this helps.

Cinchoo
  • 6,088
  • 2
  • 19
  • 34