职业IT人-IT人生活圈

 找回密码
 成为会员
搜索
查看: 1361|回复: 2

怎样创建.NET Web Service

[复制链接]
joe 发表于 2006-12-14 00:34 | 显示全部楼层 |阅读模式
为什么需要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调用的方法来调用远程对象。
     
    那么怎样使在位置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文件。
 楼主| joe 发表于 2006-12-14 00:34 | 显示全部楼层
怎样使用Web Service
   
    现在我们能够使用这个Web Service了,让我们输入一个值获得一个假的价格。
      点击Invoke按钮,将显示一个下面这样的新窗口和Xml文档。
      这显示了Web Service怎样发布信息,我们需要设计一个客户端来显示Xml文档,这个客户端应该是:
   
    1、一个Web 页
   
    2、控制台或Windows应用程序
   
    3、能和移动电话交互的WML或Wmlscript
   
    4、能在PDA上使用的Palm或Windows ce应用程序
   
    在后面我将解释建立客户端的过程
   
    可以通过http get方法直接调用Web Service,在这个例子中将不通过上面的web页和点击invoke按钮获得Xml文档,我们直接用http get方法调用Xml文档,那么语法应下:
   
  http://server/webServiceName.asm ... eter=parameterValue
   
    所以对我们这个例子而言,语句将是:
   
  http://localhost/work/aspx/Sampl ... curityInfo?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站点并对用户收费, 就象付费电视一样,用户只需要付一点费用,这在商业上是可行的。
 楼主| joe 发表于 2006-12-14 00:36 | 显示全部楼层
怎样创建.NET Web Service
附实例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=\"rice\" Xmlns:q4=\"http://www.w3.org/1999/XmlSchema\" type=\"q4:double\" />
  </all>
  </complexType>
  <element name=\"SecurityInfo\" type=\"s0:SecurityInfo\" />
  </schema>
  </serviceDescription>
您需要登录后才可以回帖 登录 | 成为会员

本版积分规则

QQ|手机版|小黑屋|网站帮助|职业IT人-IT人生活圈 ( 粤ICP备12053935号-1 )|网站地图
本站文章版权归原发布者及原出处所有。内容为作者个人观点,并不代表本站赞同其观点和对其真实性负责,本站只提供参考并不构成任何投资及应用建议。本站是信息平台,网站上部分文章为转载,并不用于任何商业目的,我们已经尽可能的对作者和来源进行了通告,但是能力有限或疏忽造成漏登,请及时联系我们,我们将根据著作权人的要求立即更正或者删除有关内容。

GMT+8, 2024-5-11 18:06 , Processed in 0.116571 second(s), 20 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表