c#创建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;
using System.Web.Services; 

  SecurityWebService 应该继承了Web Service类的功能,因此我们有必要加入下面这行代码

public class SecurityWebService : WebService 

  现在我们使用面向对象的编程技巧创建一个类,c#的类与c++和java非常相似,用C#建一个类件象去公园散步那样简单,而且不需要任何技巧。

  C#的基本数据类型设计的非常聪明,因此,如果我们返回"int," "float," 或者 "string" ,那么将自动将他们转变成标准Xml输出。不幸的是,在大多数例子中我们需要将获得的数据集合看成一个单一的实体(single entity)。现在我们举一个例子。

  我们的 SecurityWebService 股票报价系统需要用户输入股票代码,并且还将返回完整的公司名和现行股票价格,所以对一只股票而言我们有三个信息块。

  1、公司代码(string)

  2、公司名(string)

  3、价格(double)

  当我们提交股票时,我们需要提取所有三种数据,有几种方法来完成这项工作,最好的方法是将他们绑定到一种可被枚举的数据类型内,我们在c#中可用"struct"来完成,c#中的"struct"和c++中的结构很相似。

public struct SecurityInfo
{
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上(注意:甚至将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文档,这个客户端应该是:

  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.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>
 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值