Webservice服務建立、調用筆記

引言  html

  之前使用windows服務,因而學習並記錄下來:windows服務的建立、安裝、調試全過程及引起的後續學習。現現在須要用到webservice,對此感受到很困惑。通過幾天的學習、查閱資料,終於大體搞清如何運用,至於它的根本原理,暫時還不能徹底理解,之後有機會再詳細研究吧。web

 

定義:  編程

  webservice是一個平臺獨立的,低耦合,自包含的、基於可編程可編程的應用程序,可以使用開放的xml標準來描述、發佈、發現、協調和配置這些應用程序,用於開發分佈式的互操做的應用程序。(百度定義)windows

  Web Service技術, 能使得運行在不一樣機器上的不一樣應用無須藉助附加的、專門的第三方軟件或硬件, 就可相互交換數據或集成。依據Web Service規範實施的應用之間, 不管它們所使用的語言、 平臺或內部協議是什麼, 均可以相互交換數據。Web Service是自描述、 自包含的可用網絡模塊, 能夠執行具體的業務功能。Web Service也很容易部署, 由於它們基於一些常規的產業標準以及已有的一些技術,諸如的xml、HTTP。Web Service減小了應用接口的花費。Web Service爲整個企業甚至多個組織之間的業務流程的集成提供了一個通用機制。網絡

   我的理解:webservice是一個獨立的平臺,是用xml來描述的,能夠幫組不一樣平臺下的系統創建數據交互機制。好比:在一個很龐大的asp.net網站上建立一個webservice,公司內部的其餘部門開發的系統須要引用網站的一些數據,那麼在網站上面建立一個webservice,其餘系統直接添加web服務便可調用網站上的一個數據或者是方法等等。框架

 

建立一個簡單的webservice:asp.net

講述碰到的第一個小問題:本人使用的visual studio2010,在新建項目時找不到web服務?分佈式

直接選擇.net4.0,能夠選擇建立asp.net空模板,而後添加新項選擇web服務模板便可。此方法最簡單。還有:選擇.net2.0,,3.0,3.5,就能夠選擇webservice模板了,以後須要.net4.0新特性,在項目屬性窗口或者頁窗口的Build選項卡中選擇.net4.0便可。ide

新建一個空的asp.net項目,添加webservice頁面,編寫:函數

namespace FirstWebservice
{
    /// <summary>
    /// WebService1 的摘要說明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要容許使用 ASP.NET AJAX 從腳本中調用此 Web 服務,請取消對下行的註釋。
    // [System.Web.Script.Services.ScriptService]
    public class WebService1 : System.Web.Services.WebService
    {
        [WebMethod(Description = "求和的方法")]
        public double addition(double i, double j)
        {
            return i + j;
        }
        [WebMethod(Description = "求差的方法")]
        public double subtract(double i, double j)
        {
            return i - j;
        }
        [WebMethod(Description = "求積的方法")]
        public double multiplication(double i, double j)
        {
            return i * j;
        }
        [WebMethod(Description = "求商的方法")]
        public double division(double i, double j)
        {
            if (j != 0)
                return i / j;
            else
                return 0;
        }   
    }
}
View Code

至此建立了一個webservice,裏面包含加減乘除4個方法。以下圖顯示:

 

Asp.net調用webservice:

下面建立一個asp.net項目調用webservice:

首先引用webservice,將服務的網站網址加入服務引用便可。添加服務引用界面如圖:

 

編寫調用的方法:

protected void Button1_Click(object sender, EventArgs e)
        {
            string selectFlag = selectOper.Value;
            ServiceReference1.WebService1SoapClient web = new ServiceReference1.WebService1SoapClient();          

            if (selectFlag.Equals("+"))
            {
                Result.Text = (web.addition(double.Parse(Num1.Text), double.Parse(Num2.Text))).ToString();
            }
            else if (selectFlag.Equals("-"))
            {
                Result.Text = (web.subtract(double.Parse(Num1.Text), double.Parse(Num2.Text))).ToString();
            }
            else if (selectFlag.Equals("*"))
            {
                Result.Text = (web.multiplication(double.Parse(Num1.Text), double.Parse(Num2.Text))).ToString();
            }
            else if (selectFlag.Equals("/"))
            {
                Result.Text = (web.division(double.Parse(Num1.Text), double.Parse(Num2.Text))).ToString();
            } 
        }

在運行界面內,隨即選擇「-」號,最後獲得的運算以下圖:

 

 

Winform調用webservice:

既然上面在介紹webservice時說了其是誇平臺的,那麼建立一個winform系統調用webservice。一樣先添加webservice,

private void button1_Click(object sender, EventArgs e)
        {
            ServiceReference1.WebService1SoapClient web = new ServiceReference1.WebService1SoapClient();
            this.txt3.Text = (web.multiplication(double.Parse(this.txt1.Text), double.Parse(this.txt2.Text))).ToString();
        }

能夠看出winform的調用和asp.net調用基本是相同的。在此也就沒有一一驗證,只是簡單的選擇「*」做爲運算符,運行結果以下:

 

 

總結:

  上文簡單描述了webservice的基本認識,接着建立了一個至關簡單的webservice,而後建立一個asp.net項目和winform系統對它進行調用,全部一切基本上完成了webservice的使用過程。你們平時看到的更高級的webservice,或者能夠說是更爲複雜的webservice吧,這些都是在最簡單的webservice基礎上增長的一些很複雜的業務邏輯,抽絲剝繭後仍是基本的webservice應用。

 

補充:

webservice三要素:SOAP(simple object access protocol),WSDL(web service description language),UDDI(universal description discovery and integration).

  • Web Service實現業務訴求:Web Service是真正「辦事」的那個。
  • SOAP提供「請求」的規範:你想讓人家辦事,總得告訴人家你想幹什麼吧,SOAP就是定義這個「請求」的格式的,按照SOAP定義的「請求」格式「書寫」請求就能夠保證Web Service可以正確的解讀你想讓它幹什麼以及你爲它提供了什麼參數。在這個請求中,你須要描述的主要問題有:向哪一個Web Service發送請求,請求的參數類型、參數值、返回值類型。這些都「填寫」完畢,也就完成了符合SOAP規範的SOAP消息。
  • WSDL提供「能辦的事的說明」:我想幫你的忙,可是我要告訴你我都能幹什麼,以及幹這些事情須要的參數類型。

 

SOAP簡單對象訪問協議是在分散或分佈式的環境中傳遞信息的一個基於xml的協議。標準的soap協議格式以下:

<?xml version="1.0"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">

<soap:Header>
  <m:Trans xmlns:m="http://www.w3schools.com/transaction/"
  soap:mustUnderstand="1">234
  </m:Trans>
</soap:Header>

<soap:Body>
  <m:GetPrice xmlns:m="http://www.w3schools.com/prices">
    <m:Item>Apples</m:Item>
  </m:GetPrice>
</soap:Body>

</soap:Envelope>

下面解釋裏面的元素:

a) Envelope

SOAP的請求內容必須以Envelope作爲根節點。

xmlns:soap="http://www.w3.org/2001/12/soap-envelope",不能修改,不然會出錯。http://www.w3.org/2001/12/soap-envelope裏面有Envelope的schema的相關定義。有興趣的能夠去這個連接的內容。

soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding",這個指定了數據元素的類型。

b) Header

這個是可選的,若是須要添加Header元素,那麼它必須是Envelope的第一個元素。

Header的內容並無嚴格的限制,咱們能夠本身添加一些和應用程序相關的內容,可是客戶端必定要記得處理這些Header元素,能夠加上mustUnderstand強制進行處理。 

c) Body

這個就是請求的主題內容了,請求什麼函數,參數是什麼類型等等都在這裏面指定。 

用標籤表示一個函數,而後用子元素表示它的參數。

在調用中沒有指定參數和返回類型,這裏不須要指定,由於提供服務的一方本身已經規定好了數據類型,在調用時指定數據類型沒有任何意義。

 

WSDL是用來描述WebService的,它用XML的格式描述了WebService有哪些方法、參數類型、訪問路徑等等。咱們要使用一個WebService確定首先要獲取它的WSDL,在VS中添加一個Web 引用時,這些工做由開發環境幫咱們作了,開發環境根據WSDL文檔給Web Service生成了相應的代理類供咱們使用。

實例:

public class Service : System.Web.Services.WebService
{
    public Service () {
    }

    [WebMethod]
    public DateTime HelloWorld(int i)
    {
        return DateTime.Now;
    }
}

WSDL:

Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> 1 <?xml version="1.0" encoding="utf-8"?>
<wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" 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: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:sequence>
            <s:element minOccurs="1" maxOccurs="1" name="i" type="s:int" />
          </s:sequence>
        </s:complexType>
      </s:element>
      <s:element name="HelloWorldResponse">
        <s:complexType>
          <s:sequence>
            <s:element minOccurs="1" maxOccurs="1" name="HelloWorldResult" type="s:dateTime" />
          </s:sequence>
        </s:complexType>
      </s:element>
    </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:portType name="ServiceSoap">
    <wsdl:operation name="HelloWorld">
      <wsdl:input message="tns:HelloWorldSoapIn" />
      <wsdl:output message="tns:HelloWorldSoapOut" />
    </wsdl:operation>
  </wsdl:portType>
  <wsdl:binding name="ServiceSoap" type="tns:ServiceSoap">
    <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:binding>
  <wsdl:binding name="ServiceSoap12" type="tns:ServiceSoap">
    <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:binding>
  <wsdl:service name="Service">
    <wsdl:port name="ServiceSoap" binding="tns:ServiceSoap">
      <soap:address location="http://localhost:2206/WebSite1/Service.asmx" />
    </wsdl:port>
    <wsdl:port name="ServiceSoap12" binding="tns:ServiceSoap12">
      <soap12:address location="http://localhost:2206/WebSite1/Service.asmx" />
    </wsdl:port>
  </wsdl:service>
</wsdl:definitions>
View Code

一個WSDL文檔由四部分組成:

一、types

  指定了WebService用到的全部數據類型,上面用到了兩種數據類型,int和datetime

二、message

  指明一個操做所用到的數據類型。

  HelloWorldSoapIn是指HelloWorld的輸入操做用到的數據類型,HelloWorldSoapOut是指HelloWorld的輸出操做用到的數據類型。兩者的element元素指出了與types中對應到的具體類型。

三、portType

  指出了這個WebService全部支持的操做,就是說有哪些方法可供調用。

  這裏支持一個HelloWorld調用,它的輸入和輸出對應到HelloWorldSoapIn和HelloWorldSoapOut這個兩個數據類型。

四、binding

  soap12:binding元素的transport指明傳輸協議,這裏是http協議。

  operation 指明要暴露給外界調用的操做。

  use屬性指定輸入輸出的編碼方式,這裏沒有指定編碼。

五、services

  指定服務的一些信息,主要是指定服務的訪問路徑。

 

UDDI(統一描述發現和集成) 提供一種發佈和查找服務描述的方法。UDDI 數據實體提供對定義業務和服務信息的支持。WSDL 中定義的服務描述信息是UDDI註冊中心信息的補充。客戶端經過UDDI的標準和機制來搜尋須要的web服務,綁定找到的web服務並使用它提供的服務。另外,框架爲企業進一步發展電子商務提供了接口,創建在UDDI基礎上的Web services能夠完成這一功能。 

 

 

引用:

SOAP和WSDL的一些必要知識

SOAP消息機制簡介

相關文章
相關標籤/搜索