简体   繁体   English

为什么未从更改后的地址调用WCF服务?

[英]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. 我试图动态更改app.config文件的终点地址。 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. 第一次读取app.config时,该进程将对其进行缓存。 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. 您也可以不通过app.config来更改端点地址。 Just set Endpoint property on your WCF client instance. 只需在WCF客户端实例上设置Endpoint属性即可。

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 ). 检查下面的简单实现,此方法将为您提供服务客户端( 假定ServiceClient为代理 )。

    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). 在这里,我们利用现有的绑定配置(在配置部分中找到httpBindinConfigName )。 if we required then we can change the binding configuration also. 如果需要,我们还可以更改绑定配置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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