简体   繁体   中英

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

The app.config is cached by the process the first time it is read. If you want to change the config file at run-time, you will need to clear the cache and have it read again. You can do this by calling:

ConfigurationManager.RefreshSection("system.serviceModel/client");

You can also change the endpoint address without going through the app.config. Just set Endpoint property on your WCF client instance.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM