以编程方式检查 WSDL 文件支持的 SOAP 版本(1.1 或 1.2 或两者)
Posted
技术标签:
【中文标题】以编程方式检查 WSDL 文件支持的 SOAP 版本(1.1 或 1.2 或两者)【英文标题】:Check programatically the version of SOAP(1.1 or 1.2 or both) a WSDL file supports 【发布时间】:2012-04-23 10:00:54 【问题描述】:我正在使用 C#,我想检查 WSDL 支持哪个版本的 SOAP。我怎样才能找到它?
WSDL 1.1 文件中包含以下命名空间
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
WSDL 1.2 文件中包含以下命名空间
xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/"
如果一个文件同时支持这两个版本,它可以包含以下类型的内容
<wsdl:binding name="CustServiceSoap" type="tns:CustServiceSoap">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" />
<wsdl:operation name="GetAllCustomers">
<soap:operation soapAction="http://tempuri.org/GetAllCustomers" style="document" />
<wsdl:input>
<soap:body use="literal" />
</wsdl:input>
<wsdl:output>
<soap:body use="literal" />
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="GetNCustomers">
<soap:operation soapAction="http://tempuri.org/GetNCustomers" style="document" />
<wsdl:input>
<soap:body use="literal" />
</wsdl:input>
<wsdl:output>
<soap:body use="literal" />
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="GetRangeOfCustomers">
<soap:operation soapAction="http://tempuri.org/GetRangeOfCustomers" style="document" />
<wsdl:input>
<soap:body use="literal" />
</wsdl:input>
<wsdl:output>
<soap:body use="literal" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:binding name="CustServiceSoap12" type="tns:CustServiceSoap">
<soap12:binding transport="http://schemas.xmlsoap.org/soap/http" />
<wsdl:operation name="GetAllCustomers">
<soap12:operation soapAction="http://tempuri.org/GetAllCustomers" style="document" />
<wsdl:input>
<soap12:body use="literal" />
</wsdl:input>
<wsdl:output>
<soap12:body use="literal" />
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="GetNCustomers">
<soap12:operation soapAction="http://tempuri.org/GetNCustomers" style="document" />
<wsdl:input>
<soap12:body use="literal" />
</wsdl:input>
<wsdl:output>
<soap12:body use="literal" />
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="GetRangeOfCustomers">
<soap12:operation soapAction="http://tempuri.org/GetRangeOfCustomers" style="document" />
<wsdl:input>
<soap12:body use="literal" />
</wsdl:input>
<wsdl:output>
<soap12:body use="literal" />
</wsdl:output>
</wsdl:operation>
【问题讨论】:
这个链接有一些细节***.com/questions/736845/…。您是否正在寻找如何在 WCF 中执行此操作? 我想写一个函数,如果我提供了 WSDL 文件的 URL,那么它可以返回是支持 1.1 还是支持 1.2 或者两者都支持。我认为在解析 WSDL 文件时可以做到这一点。 使用一些 XSLT 处理 WSDL 文档。简单、轻量级,如果您需要更改 XSLT 查找的内容(保存为模板),则无需进行完整部署。 【参考方案1】:public void Foo()
//var uri = new Uri("http://kozhevnikov.com/foo.asmx?WSDL");
//var uri = new Uri("http://kozhevnikov.com/bar.svc?WSDL");
var importer = new WsdlImporter(new MetadataExchangeClient(uri, MetadataExchangeClientMode.HttpGet).GetMetadata());
var version = importer.ImportAllEndpoints().Aggregate(Soap.None, (v, e) => v | Parse(e.Binding.MessageVersion.Envelope));
if (version == Soap.None)
Console.WriteLine("Is None.");
if (version.HasFlag(Soap.Both))
Console.WriteLine("Has Both.");
Console.WriteLine(version);
private static Soap Parse(EnvelopeVersion version)
if (version == EnvelopeVersion.None)
return Soap.None;
if (version == EnvelopeVersion.Soap11)
return Soap.Soap11;
if (version == EnvelopeVersion.Soap12)
return Soap.Soap12;
throw new NotImplementedException(version.ToString());
public enum Soap
None,
Soap11,
Soap12,
Both = Soap11 | Soap12
【讨论】:
【参考方案2】:尽管这个问题已经过去了近十年,而且肥皂服务也成为传统,但可能仍然需要像我一样处理它们!
using System.Web.Services.Description;
using System.Net.Http;
using System.Collections.Generic;
using System.Linq;
private static async Task<string[]> GetSoapVersions()
var client = new HttpClient();
var stream = await client.GetStreamAsync(WSDL_URL);
var serviceDescription = ServiceDescription.Read(stream);
var allVersionsCollection = new List<string>();
foreach (Binding binding in serviceDescription.Bindings)
foreach (var extension in binding.Extensions)
if (extension is Soap12Binding)
allVersionsCollection.Add("Soap12");
else if (extension is SoapBinding)
allVersionsCollection.Add("Soap11");
return allVersionsCollection.Distinct().ToArray();
【讨论】:
以上是关于以编程方式检查 WSDL 文件支持的 SOAP 版本(1.1 或 1.2 或两者)的主要内容,如果未能解决你的问题,请参考以下文章