vs2010 c#添加webservice服務並使用soapui測試

1.環境win7 64位,vs2010
2.新建一個asp.net web應用程序

3.右鍵項目添加一個web服務

4.程序本來就有一個helloworld方法,現在添加一個自定義的方法,,如下CarMoveRequest
          [WebMethod]
        public Car CarMoveRequest(string Name, int X, int Y )
        {
            Car car = new Car();
            car.Name = Name;
            car.X = 8;
            car.Y = 12;
            return car;
        }

其中Car是一個類,具體見後面
5.在vs上測試:按debug按鈕,執行道如下頁面


執行之後是如下結果


6.使用soapui測試
需要先生成wsdl文件才能導入給soapui,soapui會分析wsdl文件提取出消息,才能測試
在瀏覽器裏面輸入 http://localhost:55278/WebService1.asmx?wsdl,會得到wsdl的文件,右鍵查看源代碼然後複製內容保存爲wsdl文件即可

7.使用soapui打開ws.wsdl,此時需要保持webservce服務在線才能正常打開

8.解析ws.wsdl
 

<?xml version="1.0" encoding="utf-8"?>
<wsdl:definitions xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:tns="http://tempuri.org/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" targetNamespace="http://tempuri.org/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
  <wsdl:types>
    <s:schema elementFormDefault="qualified" targetNamespace="http://tempuri.org/">
      <s:element name="HelloWorld">
        <s:complexType />
      </s:element>
      <s:element name="HelloWorldResponse">
        <s:complexType>
          <s:sequence>
            <s:element minOccurs="0" maxOccurs="1" name="HelloWorldResult" type="s:string" />
          </s:sequence>
        </s:complexType>
      </s:element>
      <s:element name="CarMoveRequest">
        <s:complexType>
          <s:sequence>
            <s:element minOccurs="0" maxOccurs="1" name="Name" type="s:string" />
            <s:element minOccurs="1" maxOccurs="1" name="X" type="s:int" />
            <s:element minOccurs="1" maxOccurs="1" name="Y" type="s:int" />
          </s:sequence>
        </s:complexType>
      </s:element>
      <s:element name="CarMoveRequestResponse">
        <s:complexType>
          <s:sequence>
            <s:element minOccurs="0" maxOccurs="1" name="CarMoveRequestResult" type="tns:Car" />
          </s:sequence>
        </s:complexType>
      </s:element>
      <s:complexType name="Car">
        <s:sequence>
          <s:element minOccurs="0" maxOccurs="1" name="Name" type="s:string" />
          <s:element minOccurs="1" maxOccurs="1" name="X" type="s:int" />
          <s:element minOccurs="1" maxOccurs="1" name="Y" type="s:int" />
        </s:sequence>
      </s:complexType>
    </s:schema>
  </wsdl:types>
  <wsdl:message name="HelloWorldSoapIn">
    <wsdl:part name="parameters" element="tns:HelloWorld" />
  </wsdl:message>
  <wsdl:message name="HelloWorldSoapOut">
    <wsdl:part name="parameters" element="tns:HelloWorldResponse" />
  </wsdl:message>
  <wsdl:message name="CarMoveRequestSoapIn">
    <wsdl:part name="parameters" element="tns:CarMoveRequest" />
  </wsdl:message>
  <wsdl:message name="CarMoveRequestSoapOut">
    <wsdl:part name="parameters" element="tns:CarMoveRequestResponse" />
  </wsdl:message>
  <wsdl:portType name="WebService1Soap">
    <wsdl:operation name="HelloWorld">
      <wsdl:input message="tns:HelloWorldSoapIn" />
      <wsdl:output message="tns:HelloWorldSoapOut" />
    </wsdl:operation>
    <wsdl:operation name="CarMoveRequest">
      <wsdl:input message="tns:CarMoveRequestSoapIn" />
      <wsdl:output message="tns:CarMoveRequestSoapOut" />
    </wsdl:operation>
  </wsdl:portType>
  <wsdl:binding name="WebService1Soap" type="tns:WebService1Soap">
    <soap:binding transport="http://schemas.xmlsoap.org/soap/http" />
    <wsdl:operation name="HelloWorld">
      <soap:operation soapAction="http://tempuri.org/HelloWorld" style="document" />
      <wsdl:input>
        <soap:body use="literal" />
      </wsdl:input>
      <wsdl:output>
        <soap:body use="literal" />
      </wsdl:output>
    </wsdl:operation>
    <wsdl:operation name="CarMoveRequest">
      <soap:operation soapAction="http://tempuri.org/CarMoveRequest" 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="WebService1Soap12" type="tns:WebService1Soap">
    <soap12:binding transport="http://schemas.xmlsoap.org/soap/http" />
    <wsdl:operation name="HelloWorld">
      <soap12:operation soapAction="http://tempuri.org/HelloWorld" style="document" />
      <wsdl:input>
        <soap12:body use="literal" />
      </wsdl:input>
      <wsdl:output>
        <soap12:body use="literal" />
      </wsdl:output>
    </wsdl:operation>
    <wsdl:operation name="CarMoveRequest">
      <soap12:operation soapAction="http://tempuri.org/CarMoveRequest" style="document" />
      <wsdl:input>
        <soap12:body use="literal" />
      </wsdl:input>
      <wsdl:output>
        <soap12:body use="literal" />
      </wsdl:output>
    </wsdl:operation>
  </wsdl:binding>
  <wsdl:service name="WebService1">
    <wsdl:port name="WebService1Soap" binding="tns:WebService1Soap">
      <soap:address location="http://localhost:55278/WebService1.asmx" />
    </wsdl:port>
    <wsdl:port name="WebService1Soap12" binding="tns:WebService1Soap12">
      <soap12:address location="http://localhost:55278/WebService1.asmx" />
    </wsdl:port>
  </wsdl:service>
</wsdl:definitions>

從最下面的wsdl:service節看起,定義了兩個ws服務,用兩個wsdl:port節來表示對,名字分別是WebService1Soap和WebService1Soap12,和soapui的右側解析出來的兩個ws服務的名字一致,並且指示了每個ws的連接地址和端口


再看binding,
binding 元素有兩個屬性 - name 屬性和 type 屬性。
name 屬性定義 binding 的名稱,而 type 屬性指向用於 binding 的端口,在這個例子中是 "glossaryTerms" 端口。
soap:binding 元素有兩個屬性 - style 屬性和 transport 屬性。
style 屬性可取值 "rpc" 或 "document"。在這個例子中我們使用 document。transport 屬性定義了要使用的 SOAP 協議。在這個例子中我們使用 HTTP。
operation 元素定義了每個端口提供的操作符。
對於每個操作,相應的 SOAP 行爲都需要被定義。同時您必須如何對輸入和輸出進行編碼。在這個例子中我們使用了 "literal"。



再看port,port裏面指定了操作operation,WSDL 定義了四種操作類型:
類型                      定義
One-way                此操作可接受消息,但不會返回響應。
Request-response    此操作可接受一個請求並會返回一個響應,是最常見的,本例即是
Solicit-response    此操作可發送一個請求,並會等待一個響應。
Notification           此操作可發送一條消息,但不會等待響應。

所以每個operation節裏面要麼含有一個消息(比如One-way類型的操作),要麼含有兩個消息(比如Request-response類型的操作)

本例中的操作CarMoveRequest是Request-response 類型的操作(operation的類型哪裏設置的?),所以operation節裏面包含兩個message。而這個operation節裏面的每個message,在上面都會有頂層wsdl:message節來詳細這個消息在實際傳輸xml中叫什麼名字----用這個頂層wsdl:message節裏面的element來指示

再看types,在types裏面的每一個element節都會與一個頂層的wsdl:message節的element節相對應,用來具體描述這條message參數


通過types就可以構造要發送的消息名和參數,和應迴應的的消息名和參數


從圖中,看出來基本一致,但是soapui發出的消息裏面都有一個tem:前綴,這是什麼東東?
原來這是命名空間,在wsdl文件中的開頭部分有定義的,用targetNamespace來指定。soapui解析出來這個targetNamespace,然後再發送消息的時候直接給掛在各個元素前面了,如下


9.使用socket調試工具模擬發送webservice幀,
可以把c#那個頁面的的示例代碼改改參數和長度值,直接發給webservice即可

也可以用soapui裏面的raw和xml的信息作爲請求信息一塊發給webservice (需要稍微修改一下soapui的POST後面的地址信息)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章