我有一个asp.net webforms网站和一个wcf服务。我使用jQuery来执行与WCF服务的AJAX操作,如下所示:
$.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" />
任何帮助都将不胜感激,谢谢。
答案 0 :(得分:1)
您好我认为您实际上想要读取您可以通过此代码获取的端点地址
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;
}
这里有详细描述:Getting WCF Bindings and Behaviors from any config source
这也可以帮助您:Programmatically enumerate WCF endpoints defined in app.config