How to consume a wsdl file that doesn't exist on web in C#

蹲街弑〆低调 提交于 2019-12-24 10:36:39

问题


I am trying to impliment a service with SOAP messages to my visual studio project. They gave me some instructions and I saw on stackoverflow that I have to consume the wsdl and xsd file in visual studio so that it can create all the classes that I need. The problem is that when I try to add the wsdl file as service reference it says that the file doesnt exist. I search it myself using the URL provided and it really doesnt exists. Is there any way so that I can use it.

Below is the WSDL statement:
https://www1.gsis.gr/wsicispay/rmt_ws?wsdl
<definitions targetNamespace="http://remittance_ws/rmt_ws.wsdl" name="rmt_ws"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://remittance_ws/rmt_ws.wsdl"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/">
<types>
<xsd:schema>
<xsd:import namespace="http://remittance_ws/rmt_ws.wsdl"
schemaLocation="https://www1.gsis.gr/wsicispay/rmt_ws?xsd=1"/>
</xsd:schema>
</types>
<message name="remittanceXml">
<part name="parameters" element="tns:remittanceXml"/>
</message>
<message name="remittanceXmlResponse">
<part name="parameters" element="tns:remittanceXmlResponse"/>
</message>
<message name="UnsupportedEncodingException">
<part name="fault" element="tns:UnsupportedEncodingException"/>
</message>
<portType name="rmt_ws">
<operation name="remittanceXml">
<input message="tns:remittanceXml"/>
<output message="tns:remittanceXmlResponse"/>
<fault message="tns:UnsupportedEncodingException" name="UnsupportedEncodingException"/>
</operation>
</portType>
<binding name="rmt_wsBinding" type="tns:rmt_ws">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
<operation name="remittanceXml">
<soap:operation soapAction=""/>
<input>
<soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
<fault name="UnsupportedEncodingException">
<soap:fault name="UnsupportedEncodingException" use="literal"/>
</fault>
</operation>
</binding>
<service name="rmt_ws">
<port name="rmt_ws" binding="tns:rmt_wsBinding">
<soap:address location="https://www1.gsis.gr/wsicispay/rmt_ws"/>
</port>
</service>
</definitions>

and here is the xsd

 https://www1.gsis.gr/wsicispay/rmt_ws?xsd=1
<xs:schema version="1.0" targetNamespace="http://remittance_ws/rmt_ws.wsdl"
xmlns:tns="http://remittance_ws/rmt_ws.wsdl" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="UnsupportedEncodingException" type="tns:UnsupportedEncodingException"/>
<xs:element name="remittanceXml" type="tns:remittanceXml"/>
<xs:element name="remittanceXmlResponse" type="tns:remittanceXmlResponse"/>
<xs:complexType name="remittanceXml">
<xs:sequence>
<xs:element name="inputXML" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="remittanceXmlResponse">
<xs:sequence>
<xs:element name="return" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="UnsupportedEncodingException">
<xs:sequence>
<xs:element name="message" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
Δείγμα μηνύματος:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:rmt="http://remittance_ws/rmt_ws.wsdl">
<soapenv:Header/>
<soapenv:Body>
<rmt:remittanceXml>
<inputXML>&lt;remittance&gt;&lt;wsTin&gt;Tin&lt;/wsTin&gt;&lt;wsUser&gt;Username&lt;/wsUser&gt;&lt;wsPswd
&gt;Pswd&lt;/wsPswd&gt;&lt;contain&gt;&lt;bmrn&gt;16GRIMXXXX00001746&lt;/bmrn&gt;&lt;blrn&gt;TESTFOR1&
lt;/blrn&gt;&lt;bcnt&gt;1&lt;/bcnt&gt;&lt;bafm&gt;099099099&lt;/bafm&gt;&lt;/contain&gt;&lt;contain&gt;&lt;bmr
n&gt;16GRIMXXXX00001720&lt;/bmrn&gt;&lt;blrn&gt;TESTFOR2&lt;/blrn&gt;&lt;bcnt&gt;2&lt;/bcnt&gt;&lt;bafm&
gt;099099099&lt;/bafm&gt;&lt;/contain&gt;&lt;contain&gt;&lt;bmrn&gt;16GRIMXXXX00001738&lt;/bmrn&gt;&lt;bl
rn&gt;TESTFOR2&lt;/blrn&gt;&lt;bcnt&gt;2&lt;/bcnt&gt;&lt;bafm&gt;099099099&lt;/bafm&gt;&lt;/contain&gt;&lt;/
remittance&gt;</inputXML>
</rmt:remittanceXml>
</soapenv:Body>
</soapenv:Envelope>

Those are in a pdf file that they gave me.

EDIT: If I add it as a file from service references it creates the reference ePayments and no .cs file. When I add the reference it gives me only one service rmt_ws which has an operation remitanceXML and nothing else. I don't know if this is correct and how should I use it


回答1:


I remember, that I had a problem with authentication of a web service some years ago, but I didn't remember where I found this alternative solution:

  • Create a new console app in visual studio (I am not sure, if deleting the old service reference in the existing project works without manual editing project file etc.)
  • Select add service reference as before, but don't enter the path of a wsdl. Instead press the button "Advanced..." in the dialog and "Add Web reference..." in the next dialog.
  • In the new dialog enter the path of the wsdl file and press "Add reference"
  • Open the file "program.cs" in the project and add this class with namespace between the last "using..." line and "namespace...."

    namespace TestWeb.WebReference 
    {
    partial class rmt_ws : System.Web.Services.Protocols.SoapHttpClientProtocol {
        protected override System.Net.WebRequest GetWebRequest(Uri uri) 
        {
            System.Net.HttpWebRequest request;
    
            request = (System.Net.HttpWebRequest)base.GetWebRequest(uri);
    
            if (PreAuthenticate) {
                System.Net.NetworkCredential networkCredentials = Credentials.GetCredential(uri, "Basic");
                if (networkCredentials != null) {
                    System.Diagnostics.Debug.WriteLine("User: " + networkCredentials.UserName);
                    System.Diagnostics.Debug.WriteLine("PW: " + networkCredentials.Password);
                    byte[] credentialBuffer = new UTF8Encoding().GetBytes(networkCredentials.UserName + ":" + networkCredentials.Password);
                    request.Headers["Authorization"] = "Basic " + Convert.ToBase64String(credentialBuffer);
                }
                else {
                    throw new ApplicationException("No network credentials");
                }
            }
            return request;
        }
    }
    
  • Replace "TestWeb" (first line of inserted code) with the namespace of your project (normally the project name).

  • Replace the existing class "Program" with the following code:

    class Program {
        static void Main(string[] args) {
            // Fill with a valid xml request
            String inputXML = "";
            String answer = "";
            try {
                WebReference.rmt_ws _webService = new WebReference.rmt_ws();
                System.Net.CredentialCache myCredentials = new System.Net.CredentialCache();
                // Set correct user and password
                System.Net.NetworkCredential netCred = new System.Net.NetworkCredential("User", "Password");
                _webService.Credentials = netCred.GetCredential(new Uri(_webService.Url), "Basic");
                _webService.PreAuthenticate = true;
                answer = _webService.remittanceXml(inputXML);
            }
            catch (Exception ex) {
                Console.WriteLine(ex.Message);
            }
            Console.WriteLine(answer);
            Console.ReadLine();
        }
    }
    
  • Set inputXML with a valid xml for your request

  • Set the correct user and password

If this does not help you should really open a new question to find some one other who could help.




回答2:


I copied the xsd part into the wsdl and visual studio generates a service with one operation to send an input XML and return another XML. I hope, this helps.

rmt_ws.wsdl:

    <?xml version="1.0" encoding="utf-8"?>
    <definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://remittance_ws/rmt_ws.wsdl" name="rmt_ws" targetNamespace="http://remittance_ws/rmt_ws.wsdl" xmlns="http://schemas.xmlsoap.org/wsdl/">
        <types>
            <xs:schema targetNamespace="http://remittance_ws/rmt_ws.wsdl" version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">
      <xs:element name="UnsupportedEncodingException" type="tns:UnsupportedEncodingException" />
      <xs:element name="remittanceXml" type="tns:remittanceXml" />
      <xs:element name="remittanceXmlResponse" type="tns:remittanceXmlResponse" />
      <xs:complexType name="remittanceXml">
        <xs:sequence>
          <xs:element minOccurs="0" name="inputXML" type="xs:string" />
        </xs:sequence>
      </xs:complexType>
      <xs:complexType name="remittanceXmlResponse">
        <xs:sequence>
          <xs:element minOccurs="0" name="return" type="xs:string" />
        </xs:sequence>
      </xs:complexType>
      <xs:complexType name="UnsupportedEncodingException">
        <xs:sequence>
          <xs:element minOccurs="0" name="message" type="xs:string" />
        </xs:sequence>
      </xs:complexType>
    </xs:schema>
  </types>
  <message name="remittanceXml">
    <part name="parameters" element="tns:remittanceXml" />
  </message>
  <message name="remittanceXmlResponse">
    <part name="parameters" element="tns:remittanceXmlResponse" />
  </message>
  <message name="UnsupportedEncodingException">
    <part name="fault" element="tns:UnsupportedEncodingException" />
  </message>
  <portType name="rmt_ws">
    <operation name="remittanceXml">
      <input message="tns:remittanceXml" />
      <output message="tns:remittanceXmlResponse" />
      <fault name="UnsupportedEncodingException" message="tns:UnsupportedEncodingException" />
    </operation>
  </portType>
  <binding name="rmt_wsBinding" type="tns:rmt_ws">
    <soap:binding transport="http://schemas.xmlsoap.org/soap/http" />
    <operation name="remittanceXml">
      <soap:operation soapAction="" />
      <input>
        <soap:body use="literal" />
      </input>
      <output>
        <soap:body use="literal" />
      </output>
      <fault name="UnsupportedEncodingException">
        <soap:fault use="literal" name="UnsupportedEncodingException" namespace="" />
      </fault>
    </operation>
  </binding>
  <service name="rmt_ws">
    <port name="rmt_ws" binding="tns:rmt_wsBinding">
      <soap:address location="https://www1.gsis.gr/wsicispay/rmt_ws" />
    </port>
  </service>
</definitions>



回答3:


I contact again for the communication problem and they send me this xml

<?xml version="1.0" encoding="UTF-8"?>
<con:soapui-project id="6026c0c2-d6a6-487f-8551-c5f616d5efab" xmlns:con="http://eviware.com/soapui/config" runType="SEQUENTIAL" abortOnError="false" soapui-version="5.2.1" resourceRoot="" name="pre-prod" activeEnvironment="Default">
<con:settings/>
<con:interface id="dba85261-52fb-48a6-96c0-9fcedc1e8146" name="rmt_wsBinding" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" definition="https://www1.gsis.gr/wsicispay/rmt_ws?wsdl" anonymous="optional" soapVersion="1_1" bindingName="{http://remittance_ws/rmt_ws.wsdl}rmt_wsBinding" type="wsdl" xsi:type="con:WsdlInterface" wsaVersion="NONE">
<con:settings/>
<con:definitionCache type="TEXT" rootPart="https://www1.gsis.gr/wsicispay/rmt_ws?wsdl">
<con:part>
<con:url>https://www1.gsis.gr/wsicispay/rmt_ws?wsdl</con:url>
<con:content>
<![CDATA[
<!--Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is Oracle JAX-WS 2.1.5.-->
<!--Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is Oracle JAX-WS 2.1.5.-->
<definitions targetNamespace="http://remittance_ws/rmt_ws.wsdl" name="rmt_ws" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://remittance_ws/rmt_ws.wsdl" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/">
  <types>
    <xsd:schema>
      <xsd:import namespace="http://remittance_ws/rmt_ws.wsdl" schemaLocation="https://www1.gsis.gr/wsicispay/rmt_ws?xsd=1"/>
    </xsd:schema>
  </types>
  <message name="remittanceXml">
    <part name="parameters" element="tns:remittanceXml"/>
  </message>
  <message name="remittanceXmlResponse">
    <part name="parameters" element="tns:remittanceXmlResponse"/>
  </message>
  <message name="UnsupportedEncodingException">
    <part name="fault" element="tns:UnsupportedEncodingException"/>
  </message>
  <portType name="rmt_ws">
    <operation name="remittanceXml">
      <input message="tns:remittanceXml"/>
      <output message="tns:remittanceXmlResponse"/>
      <fault message="tns:UnsupportedEncodingException" name="UnsupportedEncodingException"/>
    </operation>
  </portType>
  <binding name="rmt_wsBinding" type="tns:rmt_ws">
    <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
    <operation name="remittanceXml">
      <soap:operation soapAction=""/>
      <input>
        <soap:body use="literal"/>
      </input>
      <output>
        <soap:body use="literal"/>
      </output>
      <fault name="UnsupportedEncodingException">
        <soap:fault name="UnsupportedEncodingException" use="literal"/>
      </fault>
    </operation>
  </binding>
  <service name="rmt_ws">
    <port name="rmt_ws" binding="tns:rmt_wsBinding">
      <soap:address location="https://www1.gsis.gr/wsicispay/rmt_ws"/>
    </port>
  </service>
</definitions>
]]>
</con:content>
<con:type>http://schemas.xmlsoap.org/wsdl/</con:type>
</con:part>
<con:part>
<con:url>https://www1.gsis.gr/wsicispay/rmt_ws?xsd=1</con:url>
<con:content>
<![CDATA[
<!--Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is Oracle JAX-WS 2.1.5.-->
<xs:schema version="1.0" targetNamespace="http://remittance_ws/rmt_ws.wsdl" xmlns:tns="http://remittance_ws/rmt_ws.wsdl" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="UnsupportedEncodingException" type="tns:UnsupportedEncodingException"/>
  <xs:element name="remittanceXml" type="tns:remittanceXml"/>
  <xs:element name="remittanceXmlResponse" type="tns:remittanceXmlResponse"/>
  <xs:complexType name="remittanceXml">
    <xs:sequence>
      <xs:element name="inputXML" type="xs:string" minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>
  <xs:complexType name="remittanceXmlResponse">
    <xs:sequence>
      <xs:element name="return" type="xs:string" minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>
  <xs:complexType name="UnsupportedEncodingException">
    <xs:sequence>
      <xs:element name="message" type="xs:string" minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>
</xs:schema>
]]>
</con:content>
<con:type>http://www.w3.org/2001/XMLSchema</con:type>
</con:part>
</con:definitionCache>
<con:endpoints>
<con:endpoint>https://www1.gsis.gr/wsicispay/rmt_ws</con:endpoint>
</con:endpoints>
<con:operation id="432ec623-b52f-49b6-bff5-ff220d2dc4e8" name="remittanceXml" anonymous="optional" type="Request-Response" sendsAttachments="false" receivesAttachments="false" inputName="" bindingOperationName="remittanceXml" action="" isOneWay="false">
<con:settings/>
<con:call id="84ad3fc8-b5a2-491b-b375-8fc6d8b5edb4" name="Request 1">
<con:settings>
<con:setting id="com.eviware.soapui.impl.wsdl.WsdlRequest@request-headers"><xml-fragment/></con:setting>
</con:settings>
<con:encoding>UTF-8</con:encoding>
<con:endpoint>https://www1.gsis.gr/wsicispay/rmt_ws</con:endpoint>
<con:request>
<![CDATA[]]>
</con:request>
<con:credentials>
<con:USERNAME>USERNAME</con:USERNAME>
<con:PASSWORD>PASSWORD</con:PASSWORD>
<con:username>USERNAME</con:username>
<con:password>PASSWORD</con:password>
<con:selectedAuthProfile>Basic</con:selectedAuthProfile>
<con:addedBasicAuthenticationTypes>Basic</con:addedBasicAuthenticationTypes>
<con:authType>Global HTTP Settings</con:authType>
</con:credentials>
<con:jmsConfig JMSDeliveryMode="PERSISTENT"/>
<con:jmsPropertyConfig/>
<con:wsaConfig action="http://remittance_ws/rmt_ws.wsdl/rmt_ws/remittanceXmlRequest" version="200508" mustUnderstand="NONE"/>
<con:wsrmConfig version="1.2"/>
</con:call>
</con:operation>
</con:interface>
<con:properties/>
<con:wssContainer/>
<con:oAuth2ProfileContainer/>
</con:soapui-project>

Can I use this file in visual studio ? Is this better for the previous?



来源:https://stackoverflow.com/questions/48135293/how-to-consume-a-wsdl-file-that-doesnt-exist-on-web-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!