.NET 5.0 和 SOAP Web 服务 [英] .NET 5.0 and SOAP Web services

查看:34
本文介绍了.NET 5.0 和 SOAP Web 服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

终于在我写这个问题的时候弄清楚了!以为我仍然会发布它,以帮助其他人.

Finally managed to figure it out whilst I was writing this question! Thought I'd still post it encase helped any others out.

问题:

我正在将我的 Windows 服务从 .NET Framework 4.x 升级到 .NET 5.0,它使用 Hangfire 自动执行 .NET 中的计划任务和其中一项任务正在与 SAP 内的网络服务交谈.

I'm upgrading my Windows Service from .NET Framework 4.x to .NET 5.0 which uses Hangfire to automate scheduled tasks in .NET and one of the tasks is talking to web service within SAP.

其中一个自动化作业使用 SAP I 的 SOAP 服务,但不能在 .NET 5.0.NET Core 3.1 中使用它,但我可以将它与 .NET Core 3.1 一起使用代码>.NET 框架

One of the automated jobs consumes SOAP service from SAP I but can't use it in .NET 5.0 or .NET Core 3.1 but I can use it with .NET Framework

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

There is quite a few bespoke SAP services we're consuming. However, this is the most simple one. Here is the WSDL (Did remove the binding URL and identity property):

<?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="dddd-dd-dd" />
            </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,但最好在以后解决这个问题.

Any ideas on way to consume this SOAP within .NET 5.0? I mean worst-case scenario we move it to OData but ideally like to tackle that at a later date.

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

I've tried to add it using the WCF Service reference which I get the following error:

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 版本因此被编译为不同的框架.

That added an assembly of System.ServiceModel to the project but complaining about .NET version so been compiled for the different framework.

推荐答案

答案:

  • 右键单击连接的服务,然后单击添加
  • 单击 Microsoft WCF Web 服务参考提供程序
  • 输入您的 URI 和任何选项,然后单击下一步/完成
  • 删除 System.ServiveModel,因为它与 .NET 5.0
  • 不兼容
  • Right click on the Connected Services and click Add
  • Click on Microsoft WCF Web Service Reference Provider
  • Put your URI in and whatever options and click next/finish
  • Remove the System.ServiveModel as it's not compatible with .NET 5.0
Install Package System.ServiceModel.Http
Install-Package System.ServiceModel.Primitives

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

For my type of service, it's basic authentication:

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
}

希望这能帮助和我有类似情况的人.

Hopefully, this will help someone out in a similar situation to me.

这篇关于.NET 5.0 和 SOAP Web 服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆