爲何須要Web Service
在經過internet網購買商品後,你可能對配送方式感到疑惑不解。常常的狀況是因配送問題找配送公司而消耗你的大量時間,對於配送公司而言這也不是一項增值服務。
爲了解決這種問題,配送公司須要在不下降安全級別的狀況下了解更多的遞送信息,然而安全公司設計的安全系統卻很是複雜。那麼咱們能不能只使用80端口(web服務器端口)而且只經過web服務器提供信息呢?因此,咱們創建了一個全新的web應用程序以便從核心商業應用程序中得到數據。配送公司將爲些東西付money,全部的公司都但願可以將注意力集中在覈心商業應用上。
什麼是Web Service?
Web Service是一種構建應用程序的普通模型,並能在全部支持internet網通信的操做系統上 實施。Web Service令基於組件的開發和web的結合達到最佳,基於組件的對象模型,象: Distributed Component Object Model (DCOM), Remote Method Invocation (RMI), 和 Internet Inter-Orb Protocol (IIOP) 都已經發布很長時間了,不幸的是這些模型都依賴於特殊對象模型協議。Web Service利用soap和Xml對這些模型在通信方面做了進一步的擴展以消除特殊對象模型的障礙。
Web Service主要利用http和soap協議使商業數據在web傳輸, saop經過http調用商業對象執行遠程功能調用,web用戶可以使用soap和http經過web調用的方法來調用遠程對象。java
那麼怎樣使在位置a的用戶明白位置b的Web Service的意思呢?這個問題能夠經過和一個一致的共同標準來回答。描述性服務語言(Service Description Language (SDL)),soap訂約語言(SOAP Contract Language (SCL) )和網絡訪問規範語言(Network Accessible Specification Language (NASSL) )都是爲這個目的創建的類似語言,然而IBM和微軟都贊成Web Service Description Language (WSDL)做爲Web Service 的標準語言。
Web Service部件的結構由Web Service Description Language.描述,wsdl1.1是一份Xml文檔,描述了Web Service的屬性和接口。新的規範能夠在msdn.microsoft.com/Xml/general/wsdl.asp瞭解到。
當前的任務
最好的學習方法是建立一個Web Service,咱們以一個股票報價系統爲例,納斯達克和澳大利亞股票交易系統都是很是有名的例子,他們都提供了一個接口,用於輸入公司代碼和接受最終成交的股票價格。
咱們複製一個相同的功能的Web Service。
咱們的Web Service的輸入參數是股票代碼,Web Service經過調用中間層商業邏輯函數得到股票價格,商業邏輯函數保持以最小的部分集中在Web Service上。
Web Service開發工具
實現這個應用程序的核心部件將是微軟 .net framework sdk,不過他如今仍是一個試用版,你能夠在微軟站點下載,個人配置是:操做系統 windows 2000 server,pIII300,300mb內存。
建立Web Service的首選集成開發環境(IDE)是visual studio.net, 然而,你能夠用任何一種文本編輯器(wordpad,notepad,visual studio6.0)輕易建立一個Web Service文件。
建立Web Service
我將用c#建立一個Web Service 叫SecurityWebService。一個Web Service文件的擴展名是:.asmx(就象asp.net的文件擴展名.aspx那樣),文件的第一行是:
c++
<%@ WebService Language="C#" class="SecurityWebService" %> |
這個語句的含義是:告訴編譯器運行Web Service模式,還有c#類名。咱們還須要訪問Web Service名字空間,這也是引用系統名字空間的一次好實踐。
web
using System; using System.Web.Services; |
SecurityWebService 應該繼承了Web Service類的功能,所以咱們有必要加入下面這行代碼
編程
public class SecurityWebService : WebService |
如今咱們使用面向對象的編程技巧建立一個類,c#的類與c++和java很是類似,用C#建一個類件象去公園散步那樣簡單,並且不須要任何技巧。
C#的基本數據類型設計的很是聰明,所以,若是咱們返回"int," "float," 或者 "string" ,那麼將自動將他們轉變成標準Xml輸出。不幸的是,在大多數例子中咱們須要將得到的數據集合當作一個單一的實體(single entity)。如今咱們舉一個例子。
咱們的 SecurityWebService 股票報價系統須要用戶輸入股票代碼,而且還將返回完整的公司名和現行股票價格,因此對一隻股票而言咱們有三個信息塊。
一、公司代碼(string)
二、公司名(string)
三、價格(double)
當咱們提交股票時,咱們須要提取全部三種數據,有幾種方法來完成這項工做,最好的方法是將他們綁定到一種可被枚舉的數據類型內,咱們在c#中可用"struct"來完成,c#中的"struct"和c++中的結構很類似。
c#
public struct SecurityInfo { public string Code; public string CompanyName; public double Price; } |
咱們能夠經過模塊建立Web Service,代碼以下:
windows
<%@ WebService Language="C#" class="SecurityWebService" %> using System; using System.Web.Services; public struct SecurityInfo { public string Code; public string CompanyName; public double Price; } public class SecurityWebService : WebService { private SecurityInfo Security; public SecurityWebService() { Security.Code = ""; Security.CompanyName = ""; Security.Price = 0; } private void AssignValues(string Code) { // This is where you use your business components. // Method calls on Business components are used to populate the data. // For demonstration purposes, I will add a string to the Code and // use a random number generator to create the price feed. Security.Code = Code; Security.CompanyName = Code + " Pty Ltd"; Random RandomNumber = new System.Random(); Security.Price = double.Parse(new System.Random(RandomNumber.Next(1,10)).NextDouble().Format("##.##",null)); } [WebMethod(Description="This method call will get the company name and the price for a given security code.",EnableSession=false)] public SecurityInfo GetSecurityInfo(string Code) { AssignValues(Code); SecurityInfo SecurityDetails = new SecurityInfo(); SecurityDetails.Code = Security.Code; SecurityDetails.CompanyName = Security.CompanyName; SecurityDetails.Price = Security.Price; return SecurityDetails; } } |
記住全部用戶都能經過http訪問Web Service,也許你會談到代碼中的機密商業數據和不但願其餘人知道的數據,怎樣保守數據機密。解決方法是保護商業邏輯功能塊,只容許訪問表示層,在 c#中能夠經過使用關鍵字"[Web Method]"來達到這個目的,咱們看看下面的代碼:
瀏覽器
[WebMethod(Description="This......",EnableSession=false)] public SecurityInfo GetSecurityInfo(string Code) |
這個函數顯示給公衆,description標記用於描述Web Service的功能,因爲咱們不能存儲任何會話數據,咱們就將消除會話狀態。
安全
private void AssignValues(string Code) |
這個商業邏輯函數不被公衆所知,咱們不但願敏感的商業信息被公佈在web上(注意:甚至將private改成public,公衆仍然看不見,爲何呢?,這是因爲沒有使用[Web Method]關鍵字。)
咱們能夠在這個函數中利用商業邏輯得到最新的股票報價,爲了這個目的,我在代碼中添加了文本框以便輸入公司名稱,價格由一個隨機函數產生。
咱們把這個文件以SampleService.asmx保存在IIS目錄下。我將他保存在虛擬目錄"/work/aspx"下,在WEB瀏覽器中的類似以下圖:服務器
這個WEB頁是由.NET framework生成的,咱們沒有建立這個頁(這是由系統自動生成的,咱們沒有爲他寫任何一行代碼,這附圖是先前代碼的副產品),準備使用的功能對單一的Web Service是至關合適的。
使用asp.net和config.web文件能夠很輕鬆的改變該頁。不過要注意那個SDL規範的連接(即便咱們咱們使用WSDL,.NET 版仍然引用了SDL,這個問題在下一個版本中有但願矯正),這是Web Service的一個描述文件目的是建立一個代理對象,這基本上給出Web Service的一個大體介紹,若是你對這些都比較熟悉,你能夠只看"Web-only"方法,SDL規範對全部私有函數和屬性都未描述, SecurityWebService 類的SDL規範在例程A中看到。
怎樣使用Web Service
如今咱們可以使用這個Web Service了,讓咱們輸入一個值得到一個假的價格。網絡
點擊Invoke按鈕,將顯示一個下面這樣的新窗口和Xml文檔。
這顯示了Web Service怎樣發佈信息,咱們須要設計一個客戶端來顯示Xml文檔,這個客戶端應該是:
一、一個Web 頁
二、控制檯或Windows應用程序
三、能和移動電話交互的WML或Wmlscript
四、能在PDA上使用的Palm或Windows ce應用程序
在後面我將解釋創建客戶端的過程
能夠經過http get方法直接調用Web Service,在這個例子中將不經過上面的web頁和點擊invoke按鈕得到Xml文檔,咱們直接用http get方法調用Xml文檔,那麼語法應下:
http://server/webServiceName.asmx/functionName?parameter=parameterValue |
因此對咱們這個例子而言,語句將是:
http://localhost/work/aspx/SampleService.asmx/GetSecurityInfo?Code=IBM |
這與點擊invoke按鈕效果同樣,將產生一樣的結果。
如今咱們知道怎樣建立並使用一個Web Service,但咱們的工做還只完成了一半。怎樣使客戶端發現Web Service呢?在internet網上經過什麼途徑搜索Web Service呢?是否經過象雅虎搜索引擎那樣的搜索引擎呢?爲了解決這些問題咱們須要爲Web Service建立一個"discovery" 文件。
建立"discovery" 文件
發現Web Service是詢問並定位Web Service描述的過程,是訪問Web Service的預備過程,客戶端經過發現Web Service的過程得到Web Service的存在,大小,怎樣和他交互,"discovery" 文件是一個擴展名爲 :.disco的Xml文檔。沒必要強制性地要求爲每一個Web Service建立一個"discovery" 文件,下面是本文例子的"discovery" 文件實例:
<?Xml version="1.0" ?> <dynamicDiscovery Xmlns="urn:schemas- dynamicdiscovery:disco.2000-03-17"> </dynamicDiscovery> |
配置Web Service
配置Web Service很是簡單,與asp.net應用文件類似,將.asmx和.disco文件複製到相應的目錄下就好了。
Web Service的未來
Web Service的未來是很是光明的,如今不單是微軟在發展Web Service技術,IBM和SUN也致力於發展Web Service,SOAP toolkits已經能夠在Apache 和 Java Web servers上使用,不過我相信對於Web Service還須要作一點工做,尤爲是Web Service發現過程,她實在是太原始了。
Web Service將在WEB上映入一些新的觀念,有一點我相信是付費瀏覽,就象付費電視同樣,咱們創建WEB站點並對用戶收費, 就象付費電視同樣,用戶只須要付一點費用,這在商業上是可行的。
附實例A
<?Xml version="1.0" ?> <serviceDescription Xmlns:s0="http://tempuri.org/" name="SecurityWebService" targetNamespace="http://tempuri.org/" Xmlns="urn:schemas-Xmlsoap-org:sdl.2000-01-25"><soap Xmlns="urn:schemas-Xmlsoap-org:soap-sdl-2000-01-25"><service><addresses><address uri="http://localhost/work/aspx/SampleService.asmx" /> </addresses><requestResponse name="GetSecurityInfo" soapAction="http://tempuri.org/GetSecurityInfo"><request ref="s0:GetSecurityInfo" /> <response ref="s0:GetSecurityInfoResult" /> <info>This method call will get the company name and the price for a given security code.</info> </requestResponse></service></soap><httppost Xmlns="urn:schemas-Xmlsoap-org:post-sdl-2000-01-25"><service><requestResponse name="GetSecurityInfo" href="http://localhost/work/aspx/SampleService.asmx/GetSecurityInfo"><request><form><input name="Code" /> </form></request><response><mimeXml ref="s0:SecurityInfo" /> </response><info>This method call will get the company name and the price for a given security code.</info> </requestResponse></service></httppost><httpget Xmlns="urn:schemas-Xmlsoap-org:get-sdl-2000-01-25"><service><requestResponse name="GetSecurityInfo" href="http://localhost/work/aspx/SampleService.asmx/GetSecurityInfo"><request><param name="Code" /> </request><response><mimeXml ref="s0:SecurityInfo" /> </response><info>This method call will get the company name and the price for a given security code.</info> </requestResponse></service></httpget><schema targetNamespace="http://tempuri.org/" attributeFormDefault="qualified" elementFormDefault="qualified" Xmlns="http://www.w3.org/1999/XmlSchema"><element name="GetSecurityInfo"><complexType><all><element name="Code" Xmlns:q1="http://www.w3.org/1999/XmlSchema" type="q1:string" nullable="true" /> </all></complexType></element><element name="GetSecurityInfoResult"><complexType><all><element name="result" type="s0:SecurityInfo" /> </all></complexType></element><complexType name="SecurityInfo"><all><element name="Code" Xmlns:q2="http://www.w3.org/1999/XmlSchema" type="q2:string" nullable="true" /> <element name="CompanyName" Xmlns:q3="http://www.w3.org/1999/XmlSchema" type="q3:string" nullable="true" /> <element name="Price" Xmlns:q4="http://www.w3.org/1999/XmlSchema" type="q4:double" /> </all></complexType><element name="SecurityInfo" type="s0:SecurityInfo" /> </schema></serviceDescription> |