2012-04-11 47 views
1

我使用C#并且想检查WSDL支持的SOAP版本。如何找到它?以编程方式检查WSDL文件支持的SOAP版本(1.1或1.2或两者)支持

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> 

+0

此链接有一些细节http://stackoverflow.com/questions/736845/can-a-wsdl-indicate-the-soap-version-1-1-or-1-2-of-the-web -服务。你正在寻找如何在WCF中做到这一点? – Gangadhar 2012-04-11 05:22:29

+0

我想写一个函数,其中如果我提供了WSDL文件的URL,那么它可以返回是否支持1.1或支持1.2或支持两者。解析WSDL文件时可以这样做。 – Abhi 2012-04-11 05:36:37

+0

用一些XSLT处理WSDL文档。轻松,轻量级,如果您需要更改XSLT正在查找的内容(另存为模板),则无需进行完整部署。 – Chris 2012-04-11 06:19:20

回答

0
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 
}