为什么需要Web Service
在通过internet网购买商品后,你可能对配送方式感到迷惑不解。经常的情况是因配送问题找配送公司而消耗你的大量时间,对于配送公司而言这也不是一项增值服务。
为了解决这种问题,配送公司需要在不降低安全级别的情况下了解更多的递送
|
|
那么怎样使在位置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那样),文件的第一行是:
| <%@ WebService Language="C#" class="SecurityWebService" %> |
这个语句的含义是:告诉编译器运行Web Service模式,还有c#类名。我们还需要访问Web Service名字空间,这也是引用
using System.Web.Services;
SecurityWebService 应该继承了Web Service类的功能,因此我们有必要加入下面这行代码
| public class SecurityWebService : WebService |
现在我们使用面向对象的编程技巧创建一个类,c#的类与c++和
{
public string Code;
public string CompanyName;
public double Price;
}
我们可以通过模块创建Web Service,代码如下:
| <%@ 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页是由.NET framework生成的,我们没有创建这个页(这是由系统自动生成的,我们没有为他写任何一行代码,这附图是先前代码的副产品),准备使用的功能对单一的Web Service是相当合适的。
使用asp.net和config.web文件可以很轻松的改变该页。不过要注意那个SDL规范的链接(即使我们我们使用WSDL,.NET 版仍然引用了SDL,这个问题在下一个版本中有希望矫正),这是Web Service的一个描述文件目的是创建一个
点击Invoke按钮,将显示一个下面这样的新窗口和Xml文档。
这显示了Web Service怎样发布信息,我们需要设计一个客户端来显示Xml文档,这个客户端应该是:
1、一个Web 页
2、
所以对我们这个例子而言,语句将是:
| http://localhost/work/aspx/SampleService.asmx/GetSecurityInfo?Code=IBM |
这与点击invoke按钮
<dynamicDiscovery Xmlns="urn:schemas-
dynamicdiscovery:disco.2000-03-17">
</dynamicDiscovery>
配置Web Service
配置Web Service非常简单,与asp.net
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> |