Search code examples
c#xmlwcfapp-configservice-reference

Why isn't the WCF service getting called from the changed address?


I tried to dynamically change the end point address of the app.config file. After changing when I print the address I get the changed address. But the service doesn't seem to use that address. Even if I enter the wrong address it seems to work.Seems like it is using the default address. Please help. My code is below:

 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 + "/test1/test2.svc";

            }
        }
        doc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
    }

Solution

  • You can control the service address in service instance creation itself. no need to update the config file(when it is not required).

    check the simple implementation below, this method will give you the service client (assume that ServiceClient as proxy).

        public ServiceClient EndpointAddressConfiguration()
        {
            ServiceClient newClient = new ServiceClient("httpBindinConfigName","http://hostname/service.svc");
            return newClient;
        }
    

    here we make use of existing binding configuration (httpBindinConfigName found in the configuration section). if we required then we can change the binding configuration also.