怎样创建.NET Web Service(2)
点击次数:30 次 发布日期:2008-11-27 00:06:15 作者:源代码网
|
源代码网推荐 创建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中看到。 源代码网供稿. |
