怎样创建.NET Web Service(2)

2016-01-29 19:34 7 1 收藏

怎样创建.NET Web Service(2),怎样创建.NET Web Service(2)

【 tulaoshi.com - ASP 】

      
    创建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://www.tulaoshi.com/n/20160129/1509691.html

延伸阅读
标签: 电脑入门
环境:win7、win2008 搜索cmd, 右键"run as admimistarator",打开cmd后运行命令: sc create NCPRetrieverService binPath= "D:ProductionApplicationNCPNotificationRetriever NotificationRetriever.exe"
.NET之ASP Web Form快速入门 编写第一个Web Form页面 ASP.NET的Web Form页面是纯文本文件,以.aspx为文件扩展名。 当客户端浏览器请求一个.aspx文件时,ASP.NET将解析并编译目标文件为.NET架构类,接着这个类动态处理进来的请求。注意:.aspx文件只在第一次访问时被编译,随后的再次请求将重用这个类实例。这与JSP的处理方式大致相同。呵...
标签: ASP
  移植方法的选择 将站点移植到Visual Basic .NET和.NET框架的第一步是看看有哪些方法可供选择。现在有三种方法可以使用: · 将站点和Visual Basic 6.0的组件移植到ASP .NET和Visual Basic .NET · 将站点移植到ASP .NET,再用COM+ interoperability与现存的Visual Basic 6.0组件通讯 · 不改变现存的站点,而...
XML Web Service 数据交换 客户端调用服务器端的 Web 服务并传递包含数据的 DataSet (ds): Private Sub Synchronize() Dim username As String = "JohnS" Dim blnSuccess As Boolean ' 使用 XML Web Service 进行同步 Cursor.Current = Cursors.WaitCursor Dim wsFeedback As New wsFeedback.feedback blnSuccess = wsFeedback.In...
.NET之ASP Web Application快速入门(2) Application的生存期 一个ASP.NET application创建于服务器第一次被请求时,在那之前不会有ASP.NET代码在执行。接受第一次请求后,一个HttpApplication实例池被创建,并激活了Application_OnStart事件。HttpApplication实例处理这个请求及随后发生的请求,直到最后的实例退出,接着就触发Applicati...

经验教程

678

收藏

26
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部