为何须要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> |