繁体   English   中英

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

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

我试图动态更改app.config文件的终点地址。 在更改打印地址后,我得到了更改后的地址。 但是该服务似乎并未使用该地址。 即使我输入了错误的地址,它也似乎可以正常工作,似乎是使用默认地址。 请帮忙。 我的代码如下:

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

第一次读取app.config时,该进程将对其进行缓存。 如果要在运行时更改配置文件,则需要清除缓存并重新读取。 您可以通过致电:

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

您也可以不通过app.config来更改端点地址。 只需在WCF客户端实例上设置Endpoint属性即可。

您可以在服务实例创建本身中控制服务地址。 不需要更新配置文件(不需要时)。

检查下面的简单实现,此方法将为您提供服务客户端( 假定ServiceClient为代理 )。

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

在这里,我们利用现有的绑定配置(在配置部分中找到httpBindinConfigName )。 如果需要,我们还可以更改绑定配置。

暂无
暂无

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

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