none
How can I change the endpoint address in app.config with the console input? RRS feed

  • Question

  • I have an app which has a service reference. I need to change the URL of the endpoint address in app.config file based on the console input from user. How do I do this through C#? Please advice.
    <configuration>
        <system.serviceModel>
            <bindings>
                <basicHttpBinding>
                    <binding name="BasicHttpBinding_IBrokerService" closeTimeout="00:01:00"
                        openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                        allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
                        maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                        messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
                        useDefaultWebProxy="true">
                        <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                            maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                        <security mode="None">
                            <transport clientCredentialType="None" proxyCredentialType="None"
                                realm="" />
                            <message clientCredentialType="UserName" algorithmSuite="Default" />
                        </security>
                    </binding>
                </basicHttpBinding>
            </bindings>
            <client>
                <endpoint address="http://abcd/defgf/test.svc" binding="basicHttpBinding"
                    bindingConfiguration="BasicHttpBinding_ITestService" contract="TestServiceProxy.ITestService"
                    name="BasicHttpBinding_ITestService" />
            </client>
        </system.serviceModel>
    </configuration>
    I need to change the "abcd" in endpoint address with the user specified value through console.

    mayooran99

    Thursday, June 19, 2014 9:52 AM

Answers

All replies

  • Hi Mayooran,

    See how to modify app.config at runtime from the following link. http://social.msdn.microsoft.com/Forums/vstudio/en-US/23457b84-1d00-4755-9210-8afa6b414b7b/how-to-change-application-configuration-files-values-programmatically-in-cnet-2005?forum=csharpgeneral.

    Hope useful.

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Mayooran99 Friday, June 20, 2014 2:48 AM
    Friday, June 20, 2014 1:51 AM
    Moderator
  • I managed to do it this way:

     static void UpdateAppConfig(String Name)
            {
                var doc = new XmlDocument();
                doc.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
                XmlNodeList endpoints = doc.GetElementsByTagName("endpoint");
                foreach (XmlNode item in endpoints)
                {
                    var addressAttribute = item.Attributes["address"];
                    if (!ReferenceEquals(null, addressAttribute))
                    {
                        addressAttribute.Value = "http://" + Name + "/defgf/test.svc";
    
                    }
                }
                doc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
            }


    mayooran99



    • Edited by Mayooran99 Friday, June 20, 2014 2:50 AM
    • Marked as answer by Mayooran99 Friday, June 20, 2014 2:50 AM
    Friday, June 20, 2014 2:49 AM
  • There is a small problem. Though the endpoint address I changed has changed, the WCF service doesn't use that address. It is using the default address. Why is that?

    mayooran99

    Friday, June 20, 2014 3:02 AM