查询Web服务以获取消息列表?

时间:2020-03-05 18:59:52  来源:igfitidea点击:

有没有一种简单的方法来查询Web服务以查看其支持哪些消息?我正在使用的C.NET应用程序需要能够处理较旧版本的Web服务,该版本不实现我尝试发送的消息。该Web服务未公开版本号,因此计划B将查看是否定义了该消息。

我假设我可以对WSDL发出一个HTTP请求并进行解析,但是在走那条路之前,我想确保没有一种更简单的方法。

更新:
我决定获取WSDL并直接获取消息。这是获取所有消息的草稿:

HttpWebRequest webRequest = (HttpWebRequest) WebRequest.Create( "http://your/web/service/here.asmx?WSDL" );
webRequest.PreAuthenticate = // details elided
webRequest.Credentials = // details elided
webRequest.Timeout = // details elided
HttpWebResponse webResponse = (HttpWebResponse) webRequest.GetResponse();

XPathDocument xpathDocument = new XPathDocument( webResponse.GetResponseStream() );
XPathNavigator xpathNavigator = xpathDocument.CreateNavigator();

XmlNamespaceManager xmlNamespaceManager = new XmlNamespaceManager( new NameTable() );
xmlNamespaceManager.AddNamespace( "wsdl", "http://schemas.xmlsoap.org/wsdl/" );

foreach( XPathNavigator node in xpathNavigator.Select( "//wsdl:message/@name", xmlNamespaceManager ) )
{
    string messageName = node.Value;
}

解决方案

回答

我很确定WSDL是做到这一点的方法。

回答

解析WSDL可能是最简单的方法。使用WCF,还可以在运行时下载WSDL,从本质上通过代码在其上运行svcutil,最后得到一个动态生成的代理,我们可以检查其结构。有关运行时生成的代理的示例,请参见http://blogs.msdn.com/vipulmodi/archive/2006/11/16/dynamic-programming-with-wcf.aspx。