Paul
Paul

Reputation: 318

.NET 5.0 and SOAP Web services

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

The problem:

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.

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

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="\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>

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.

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']

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

Upvotes: 2

Views: 4948

Answers (1)

Paul
Paul

Reputation: 318

Answer:

  • 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.ServiceModel 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.

Upvotes: 3

Related Questions