2012-08-27 55 views
0

我有一個asp.net webforms網站和一個wcf服務。我使用jQuery來執行我的WCF服務的AJAX操作,如下所示:通過javascript在web.config文件中引用WCF服務

$.ajax({ 
    type: "POST", 
    url: "192.168.1.24/ServiceMain.svc/" + serviceName, 
    dataType: "json", 
    contentType: "application/json; charset=utf-8", 
    data: "{}", 
    cache: true, 
    success: function (json) 
    { 
     //Success operation here 
    }, 
    error: function() 
    { 
     //Error operation here 
    } 
}); 

現在一切正常。但是,我希望能夠執行測試和生產環境,這兩種環境都將託管在具有不同IP地址的不同服務器上。

很明顯,對URL進行硬編碼以指向正確的WCF服務將會成爲一個單調乏味的問題。因此,我想知道檢索WCF服務URL的最佳方法是什麼。我想過使用的web.config文件有類似以下內容:

<%=ConfigurationManager.AppSettings("SomeWCFKey")%>

但是,我不知道如何正確地在我的web.config文件中引用地址:

<endpoint address="http://192.168.1.24/ServiceMain.svc" 
binding="basicHttpBinding" 
bindingConfiguration="BasicHttpBinding_IServiceMain" 
contract="ServiceMain.IServiceMain" 
name="BasicHttpBinding_IServiceMain" /> 

任何幫助,將不勝感激,謝謝。

回答

1

嗨,我認爲你是actully想讀端點地址,你可以通過這個代碼得到

private List<string> GetEndpoints() 
{ 
    var endpointList = new List<string>(); 
    var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); 
    var serviceModel = ServiceModelSectionGroup.GetSectionGroup(config); 
     foreach (ChannelEndpointElement endpoint in serviceModel.Client.Endpoints) 
    { 
     endpointList.Add(endpoint.Address.ToString()); 
    } 
     return endpointList; 
} 

這裏是蓬在細節descirpt MROE:Getting WCF Bindings and Behaviors from any config source

,這也可以幫助你:Programmatically enumerate WCF endpoints defined in app.config

+0

這指出我在正確的方向。我得到了它的工作。謝謝。 – ROFLwTIME