[英]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.