.NET 5.0 和 SOAP Web 服务

Posted

技术标签:

【中文标题】.NET 5.0 和 SOAP Web 服务【英文标题】:.NET 5.0 and SOAP Web services 【发布时间】:2021-06-12 23:02:24 【问题描述】:

在我写这个问题的时候终于弄明白了!以为我还是会把它贴在盒子里帮助其他人。

问题:

我正在将我的 Windows 服务从 .NET Framework 4.x 升级到 .NET 5.0,它使用 Hangfire 在 .NET 中自动执行计划任务,其中一项任务是与 SAP 中的 Web 服务通信。

其中一个自动化作业使用来自 SAP I 的 SOAP 服务,但不能在 .NET 5.0.NET Core 3.1 中使用它,但我可以在 .NET Framework 中使用它

我们正在使用很多定制的 SAP 服务。然而,这是最简单的一个。这是 WSDL(确实删除了绑定 URL 和身份属性):

<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:n1="urn:sap-com:document:sap:rfc:functions" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="urn:sap-com:document:sap:soap:functions:mc-style" xmlns:wsoap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="urn:sap-com:document:sap:soap:functions:mc-style">
   <wsdl:documentation>
      <sidl:sidl xmlns:sidl="http://www.sap.com/2007/03/sidl" />
   </wsdl:documentation>
   <wsp:UsingPolicy wsdl:required="true" />
   <wsp:Policy wsu:Id="BN__Z_DATA_FEEDS">
      <wsp:ExactlyOne>
         <wsp:All>
            <sapattahnd:Enabled xmlns:sapattahnd="http://www.sap.com/710/features/attachment/">false</sapattahnd:Enabled>
            <saptrnbnd:OptimizedMimeSerialization xmlns:saptrnbnd="http://schemas.xmlsoap.org/ws/2004/09/policy/optimizedmimeserialization" wsp:Optional="true" />
            <wsaw:UsingAddressing xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" wsp:Optional="true" />
            <wsp:All>
               <sp:TransportBinding xmlns:sp="http://docs.oasis-open.org/ws-sx/ws-securitypolicy/200702" xmlns:sapsp="http://www.sap.com/webas/630/soap/features/security/policy" xmlns:wsa="http://www.w3.org/2005/08/addressing" xmlns:wst="http://docs.oasis-open.org/ws-sx/ws-trust/200512" xmlns:wsu="http://schemas.xmlsoap.org/ws/2002/07/utility" xmlns:wsx="http://schemas.xmlsoap.org/ws/2004/09/mex">
                  <wsp:Policy>
                     <sp:TransportToken>
                        <wsp:Policy>
                           <sp:HttpsToken>
                              <wsp:Policy>
                                 <sp:HttpBasicAuthentication />
                              </wsp:Policy>
                           </sp:HttpsToken>
                        </wsp:Policy>
                     </sp:TransportToken>
                     <sp:AlgorithmSuite>
                        <wsp:Policy>
                           <sp:Basic128Rsa15 />
                        </wsp:Policy>
                     </sp:AlgorithmSuite>
                     <sp:Layout>
                        <wsp:Policy>
                           <sp:Strict />
                        </wsp:Policy>
                     </sp:Layout>
                  </wsp:Policy>
               </sp:TransportBinding>
            </wsp:All>
         </wsp:All>
         <wsp:All>
            <sapattahnd:Enabled xmlns:sapattahnd="http://www.sap.com/710/features/attachment/">false</sapattahnd:Enabled>
            <saptrnbnd:OptimizedXMLTransfer xmlns:saptrnbnd="http://www.sap.com/webas/710/soap/features/transportbinding/" uri="http://xml.sap.com/2006/11/esi/esp/binxml" wsp:Optional="true" />
            <wsaw:UsingAddressing xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" wsp:Optional="true" />
            <wsp:All>
               <sp:TransportBinding xmlns:sp="http://docs.oasis-open.org/ws-sx/ws-securitypolicy/200702" xmlns:sapsp="http://www.sap.com/webas/630/soap/features/security/policy" xmlns:wsa="http://www.w3.org/2005/08/addressing" xmlns:wst="http://docs.oasis-open.org/ws-sx/ws-trust/200512" xmlns:wsu="http://schemas.xmlsoap.org/ws/2002/07/utility" xmlns:wsx="http://schemas.xmlsoap.org/ws/2004/09/mex">
                  <wsp:Policy>
                     <sp:TransportToken>
                        <wsp:Policy>
                           <sp:HttpsToken>
                              <wsp:Policy>
                                 <sp:HttpBasicAuthentication />
                              </wsp:Policy>
                           </sp:HttpsToken>
                        </wsp:Policy>
                     </sp:TransportToken>
                     <sp:AlgorithmSuite>
                        <wsp:Policy>
                           <sp:Basic128Rsa15 />
                        </wsp:Policy>
                     </sp:AlgorithmSuite>
                     <sp:Layout>
                        <wsp:Policy>
                           <sp:Strict />
                        </wsp:Policy>
                     </sp:Layout>
                  </wsp:Policy>
               </sp:TransportBinding>
            </wsp:All>
         </wsp:All>
      </wsp:ExactlyOne>
   </wsp:Policy>
   <wsp:Policy wsu:Id="BN__Z_DATA_FEEDS_soap12">
      <wsp:ExactlyOne>
         <wsp:All>
            <sapattahnd:Enabled xmlns:sapattahnd="http://www.sap.com/710/features/attachment/">false</sapattahnd:Enabled>
            <saptrnbnd:OptimizedMimeSerialization xmlns:saptrnbnd="http://schemas.xmlsoap.org/ws/2004/09/policy/optimizedmimeserialization" wsp:Optional="true" />
            <wsaw:UsingAddressing xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" wsp:Optional="true" />
            <wsp:All>
               <sp:TransportBinding xmlns:sp="http://docs.oasis-open.org/ws-sx/ws-securitypolicy/200702" xmlns:sapsp="http://www.sap.com/webas/630/soap/features/security/policy" xmlns:wsa="http://www.w3.org/2005/08/addressing" xmlns:wst="http://docs.oasis-open.org/ws-sx/ws-trust/200512" xmlns:wsu="http://schemas.xmlsoap.org/ws/2002/07/utility" xmlns:wsx="http://schemas.xmlsoap.org/ws/2004/09/mex">
                  <wsp:Policy>
                     <sp:TransportToken>
                        <wsp:Policy>
                           <sp:HttpsToken>
                              <wsp:Policy>
                                 <sp:HttpBasicAuthentication />
                              </wsp:Policy>
                           </sp:HttpsToken>
                        </wsp:Policy>
                     </sp:TransportToken>
                     <sp:AlgorithmSuite>
                        <wsp:Policy>
                           <sp:Basic128Rsa15 />
                        </wsp:Policy>
                     </sp:AlgorithmSuite>
                     <sp:Layout>
                        <wsp:Policy>
                           <sp:Strict />
                        </wsp:Policy>
                     </sp:Layout>
                  </wsp:Policy>
               </sp:TransportBinding>
            </wsp:All>
         </wsp:All>
         <wsp:All>
            <sapattahnd:Enabled xmlns:sapattahnd="http://www.sap.com/710/features/attachment/">false</sapattahnd:Enabled>
            <saptrnbnd:OptimizedXMLTransfer xmlns:saptrnbnd="http://www.sap.com/webas/710/soap/features/transportbinding/" uri="http://xml.sap.com/2006/11/esi/esp/binxml" wsp:Optional="true" />
            <wsaw:UsingAddressing xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" wsp:Optional="true" />
            <wsp:All>
               <sp:TransportBinding xmlns:sp="http://docs.oasis-open.org/ws-sx/ws-securitypolicy/200702" xmlns:sapsp="http://www.sap.com/webas/630/soap/features/security/policy" xmlns:wsa="http://www.w3.org/2005/08/addressing" xmlns:wst="http://docs.oasis-open.org/ws-sx/ws-trust/200512" xmlns:wsu="http://schemas.xmlsoap.org/ws/2002/07/utility" xmlns:wsx="http://schemas.xmlsoap.org/ws/2004/09/mex">
                  <wsp:Policy>
                     <sp:TransportToken>
                        <wsp:Policy>
                           <sp:HttpsToken>
                              <wsp:Policy>
                                 <sp:HttpBasicAuthentication />
                              </wsp:Policy>
                           </sp:HttpsToken>
                        </wsp:Policy>
                     </sp:TransportToken>
                     <sp:AlgorithmSuite>
                        <wsp:Policy>
                           <sp:Basic128Rsa15 />
                        </wsp:Policy>
                     </sp:AlgorithmSuite>
                     <sp:Layout>
                        <wsp:Policy>
                           <sp:Strict />
                        </wsp:Policy>
                     </sp:Layout>
                  </wsp:Policy>
               </sp:TransportBinding>
            </wsp:All>
         </wsp:All>
      </wsp:ExactlyOne>
   </wsp:Policy>
   <wsp:Policy wsu:Id="IF__Z_DATA_FEEDS">
      <wsp:ExactlyOne>
         <wsp:All>
            <sapsession:Session xmlns:sapsession="http://www.sap.com/webas/630/soap/features/session/">
               <sapsession:enableSession>false</sapsession:enableSession>
            </sapsession:Session>
            <sapcentraladmin:CentralAdministration xmlns:sapcentraladmin="http://www.sap.com/webas/700/soap/features/CentralAdministration/" wsp:Optional="true">
               <sapcentraladmin:BusinessApplicationID>#### REMOVED ####</sapcentraladmin:BusinessApplicationID>
            </sapcentraladmin:CentralAdministration>
         </wsp:All>
      </wsp:ExactlyOne>
   </wsp:Policy>
   <wsp:Policy wsu:Id="OP__ZOrderStatus">
      <wsp:ExactlyOne>
         <wsp:All>
            <saptrhnw05:required xmlns:saptrhnw05="http://www.sap.com/NW05/soap/features/transaction/">no</saptrhnw05:required>
            <sapcomhnd:enableCommit xmlns:sapcomhnd="http://www.sap.com/NW05/soap/features/commit/">false</sapcomhnd:enableCommit>
            <sapblock:enableBlocking xmlns:sapblock="http://www.sap.com/NW05/soap/features/blocking/">true</sapblock:enableBlocking>
            <saprmnw05:enableWSRM xmlns:saprmnw05="http://www.sap.com/NW05/soap/features/wsrm/">false</saprmnw05:enableWSRM>
         </wsp:All>
      </wsp:ExactlyOne>
   </wsp:Policy>
   <wsdl:types>
      <xsd:schema attributeFormDefault="qualified" targetNamespace="urn:sap-com:document:sap:rfc:functions">
         <xsd:simpleType name="char10">
            <xsd:restriction base="xsd:string">
               <xsd:maxLength value="10" />
            </xsd:restriction>
         </xsd:simpleType>
         <xsd:simpleType name="char50">
            <xsd:restriction base="xsd:string">
               <xsd:maxLength value="50" />
            </xsd:restriction>
         </xsd:simpleType>
         <xsd:simpleType name="cuky5">
            <xsd:restriction base="xsd:string">
               <xsd:maxLength value="5" />
            </xsd:restriction>
         </xsd:simpleType>
         <xsd:simpleType name="curr15.2">
            <xsd:restriction base="xsd:decimal">
               <xsd:totalDigits value="15" />
               <xsd:fractionDigits value="2" />
            </xsd:restriction>
         </xsd:simpleType>
         <xsd:simpleType name="date10">
            <xsd:restriction base="xsd:string">
               <xsd:maxLength value="10" />
               <xsd:pattern value="\d\d\d\d-\d\d-\d\d" />
            </xsd:restriction>
         </xsd:simpleType>
         <xsd:simpleType name="quantum13.3">
            <xsd:restriction base="xsd:decimal">
               <xsd:totalDigits value="13" />
               <xsd:fractionDigits value="3" />
            </xsd:restriction>
         </xsd:simpleType>
         <xsd:simpleType name="unit3">
            <xsd:restriction base="xsd:string">
               <xsd:maxLength value="3" />
            </xsd:restriction>
         </xsd:simpleType>
      </xsd:schema>
      <xsd:schema xmlns:n0="urn:sap-com:document:sap:rfc:functions" attributeFormDefault="qualified" targetNamespace="urn:sap-com:document:sap:soap:functions:mc-style">
         <xsd:import namespace="urn:sap-com:document:sap:rfc:functions" />
         <xsd:simpleType name="char10">
            <xsd:restriction base="xsd:string">
               <xsd:maxLength value="10" />
            </xsd:restriction>
         </xsd:simpleType>
         <xsd:simpleType name="char2">
            <xsd:restriction base="xsd:string">
               <xsd:maxLength value="2" />
            </xsd:restriction>
         </xsd:simpleType>
         <xsd:simpleType name="char20">
            <xsd:restriction base="xsd:string">
               <xsd:maxLength value="20" />
            </xsd:restriction>
         </xsd:simpleType>
         <xsd:simpleType name="char4">
            <xsd:restriction base="xsd:string">
               <xsd:maxLength value="4" />
            </xsd:restriction>
         </xsd:simpleType>
         <xsd:simpleType name="char8">
            <xsd:restriction base="xsd:string">
               <xsd:maxLength value="8" />
            </xsd:restriction>
         </xsd:simpleType>
         <xsd:complexType name="ZdfSOrderStatus">
            <xsd:sequence>
               <xsd:element name="VbelnVa" type="n0:char10" />
               <xsd:element name="VbelnVl" type="n0:char10" />
               <xsd:element name="Edatu" type="n0:date10" />
               <xsd:element name="Status" type="n0:char50" />
               <xsd:element name="StatusDate" type="tns:char10" />
               <xsd:element name="StatusTime" type="tns:char8" />
               <xsd:element name="Netwr" type="n0:curr15.2" />
               <xsd:element name="Waerk" type="n0:cuky5" />
               <xsd:element name="Ntgew" type="n0:quantum13.3" />
               <xsd:element name="Gewei" type="n0:unit3" />
               <xsd:element name="Bmeng" type="n0:quantum13.3" />
               <xsd:element name="Pickqty" type="n0:quantum13.3" />
               <xsd:element name="Vrkme" type="n0:unit3" />
               <xsd:element name="LineCount" type="xsd:int" />
               <xsd:element name="Kunnr" type="tns:char10" />
               <xsd:element name="Block" type="tns:char2" />
               <xsd:element name="Ordertype" type="tns:char4" />
               <xsd:element name="Shippingcondition" type="tns:char20" />
            </xsd:sequence>
         </xsd:complexType>
         <xsd:complexType name="ZdfTOrderStatus">
            <xsd:sequence>
               <xsd:element name="item" type="tns:ZdfSOrderStatus" minOccurs="0" maxOccurs="unbounded" />
            </xsd:sequence>
         </xsd:complexType>
         <xsd:element name="ZOrderStatus">
            <xsd:complexType>
               <xsd:sequence>
                  <xsd:element name="ImInvoiceDaylimit" type="xsd:int" minOccurs="0" />
               </xsd:sequence>
            </xsd:complexType>
         </xsd:element>
         <xsd:element name="ZOrderStatusResponse">
            <xsd:complexType>
               <xsd:sequence>
                  <xsd:element name="ExOrders" type="tns:ZdfTOrderStatus" />
               </xsd:sequence>
            </xsd:complexType>
         </xsd:element>
      </xsd:schema>
   </wsdl:types>
   <wsdl:message name="ZOrderStatus">
      <wsdl:part name="parameters" element="tns:ZOrderStatus" />
   </wsdl:message>
   <wsdl:message name="ZOrderStatusResponse">
      <wsdl:part name="parameter" element="tns:ZOrderStatusResponse" />
   </wsdl:message>
   <wsdl:portType name="Z_DATA_FEEDS">
      <wsp:Policy>
         <wsp:PolicyReference URI="#IF__Z_DATA_FEEDS" />
      </wsp:Policy>
      <wsdl:operation name="ZOrderStatus">
         <wsp:Policy>
            <wsp:PolicyReference URI="#OP__ZOrderStatus" />
         </wsp:Policy>
         <wsdl:input message="tns:ZOrderStatus" />
         <wsdl:output message="tns:ZOrderStatusResponse" />
      </wsdl:operation>
   </wsdl:portType>
   <wsdl:binding name="Z_DATA_FEEDS" type="tns:Z_DATA_FEEDS">
      <wsp:Policy>
         <wsp:PolicyReference URI="#BN__Z_DATA_FEEDS" />
      </wsp:Policy>
      <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document" />
      <wsdl:operation name="ZOrderStatus">
         <soap:operation soapAction="urn:sap-com:document:sap:soap:functions:mc-style:Z_DATA_FEEDS:ZOrderStatusRequest" 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="Z_DATA_FEEDS_soap12" type="tns:Z_DATA_FEEDS">
      <wsp:Policy>
         <wsp:PolicyReference URI="#BN__Z_DATA_FEEDS_soap12" />
      </wsp:Policy>
      <wsoap12:binding transport="http://schemas.xmlsoap.org/soap/http" style="document" />
      <wsdl:operation name="ZOrderStatus">
         <wsoap12:operation soapAction="urn:sap-com:document:sap:soap:functions:mc-style:Z_DATA_FEEDS:ZOrderStatusRequest" style="document" />
         <wsdl:input>
            <wsoap12:body use="literal" />
         </wsdl:input>
         <wsdl:output>
            <wsoap12:body use="literal" />
         </wsdl:output>
      </wsdl:operation>
   </wsdl:binding>
   <wsdl:service name="Z_DATA_FEEDS">
      <wsdl:port name="Z_DATA_FEEDS" binding="tns:Z_DATA_FEEDS">
         <soap:address location="#### REMOVED ####" />
      </wsdl:port>
      <wsdl:port name="Z_DATA_FEEDS_soap12" binding="tns:Z_DATA_FEEDS_soap12">
         <wsoap12:address location="#### REMOVED ####" />
      </wsdl:port>
   </wsdl:service>
</wsdl:definitions>

关于在.NET 5.0 中使用此 SOAP 的方法有什么想法吗?我的意思是最坏的情况,我们将其移至 OData,但理想情况下希望稍后再解决。

我尝试使用 WCF 服务引用添加它,但出现以下错误:

The optional WSDL extension element 'Policy' from namespace 'http://schemas.xmlsoap.org/ws/2004/09/policy' was not handled.
XPath: //wsdl:definitions[@targetNamespace='urn:sap-com:document:sap:soap:functions:mc-style']/wsdl:portType[@name='Z_DATA_FEEDS']/wsdl:operation[@name='ZOrderStatus']
The optional WSDL extension element 'Policy' from namespace 'http://schemas.xmlsoap.org/ws/2004/09/policy' was not handled.
XPath: //wsdl:definitions[@targetNamespace='urn:sap-com:document:sap:soap:functions:mc-style']/wsdl:portType[@name='Z_DATA_FEEDS']

这在项目中添加了一个 System.ServiceModel 的程序集,但抱怨 .NET 版本因此被编译为不同的框架。

【问题讨论】:

【参考方案1】:

答案:

右键单击连接的服务,然后单击添加 单击 Microsoft WCF Web 服务参考提供程序 输入您的 URI 和任何选项,然后单击下一步/完成 删除System.ServiveModel,因为它与.NET 5.0 不兼容
Install Package System.ServiceModel.Http
Install-Package System.ServiceModel.Primitives

对于我的服务类型,它是基本身份验证:

var basicHttpBinding = new BasicHttpBinding();
basicHttpBinding.MaxBufferSize = int.MaxValue;
basicHttpBinding.ReaderQuotas = System.Xml.XmlDictionaryReaderQuotas.Max;
basicHttpBinding.MaxReceivedMessageSize = int.MaxValue;
basicHttpBinding.AllowCookies = true;
basicHttpBinding.OpenTimeout = TimeSpan.FromMinutes(15);
basicHttpBinding.ReceiveTimeout = TimeSpan.FromMinutes(15);
basicHttpBinding.SendTimeout = TimeSpan.FromMinutes(15);

basicHttpBinding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly;
basicHttpBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;

var endpoint = new EndpointAddress("## Enter your URL ##");

using (var client = new Analytics.ZWS_ANALYTICSClient(basicHttpBinding, endpoint))

    client.ClientCredentials.UserName.UserName = "## Your username ##"
    client.ClientCredentials.UserName.Password = "## Your password ##";

    // Call your web service method here

希望这能帮助遇到与我类似情况的人。

【讨论】:

以上是关于.NET 5.0 和 SOAP Web 服务的主要内容,如果未能解决你的问题,请参考以下文章

强制在来自 NET Web 服务代理类的 SOAP 请求中包含默认值属性属性

ASP.NET与SOAP协议使用记录

ASP.NET与SOAP协议使用记录

带有.net核心的控制台应用程序使用soap Web服务的问题

从 C# .NET Core(特别是 Workday)调用 Java Web 服务。如何在soap请求中获取xml属性

如何创建 PHP SOAP 客户端以在 SSL 下调用 WCF Web 服务?