Kalpa-W
Kalpa-W

Reputation: 358

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

Upvotes: 0

Views: 153

Answers (2)

Vasistan
Vasistan

Reputation: 292

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.

Upvotes: 0

Mike Hixson
Mike Hixson

Reputation: 5189

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.

Upvotes: 1

Related Questions