C# 中WebService返回类型(string,int,bool,DataSet,class实体类)示例

 WebService 服务可以返回任何可序列化的对象.本文代码给出返回基本数据类型及实体类结构示例和调用代码示例.

WebService代码如下:
  1. using System;
  2. using System.Collections;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Diagnostics;
  6. using System.Web;
  7. using System.Web.Services;
  8. namespace StudentServer
  9. {
  10.  /// <summary>
  11.  /// 本类实现WebService服务
  12.  /// 提供对各种数据类型的返回例子
  13.  /// 包括:
  14.  ///  基本数据类型(string,ini,bool,long,float等)
  15.  ///  类结构型(class),必须是可序列化的类
  16.  ///  DataSet类型
  17.  /// </summary>
  18.  public class Demo : System.Web.Services.WebService
  19.  {
  20.   public Demo()
  21.   {
  22.    //CODEGEN: 该调用是 ASP.NET Web 服务设计器所必需的
  23.    InitializeComponent();
  24.   }
  25.   #region 组件设计器生成的代码
  26.   
  27.   //Web 服务设计器所必需的
  28.   private IContainer components = null;
  29.     
  30.   /// <summary>
  31.   /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  32.   /// 此方法的内容。
  33.   /// </summary>
  34.   private void InitializeComponent()
  35.   {
  36.   }
  37.   /// <summary>
  38.   /// 清理所有正在使用的资源。
  39.   /// </summary>
  40.   protected override void Dispose( bool disposing )
  41.   {
  42.    if(disposing && components != null)
  43.    {
  44.     components.Dispose();
  45.    }
  46.    base.Dispose(disposing);  
  47.   }
  48.   
  49.   #endregion
  50.   // WEB 服务示例
  51.   // HelloWorld() 示例服务返回字符串 Hello World
  52.   // 若要生成,请取消注释下列行,然后保存并生成项目
  53.   // 若要测试此 Web 服务,请按 F5 键
  54.   /// <summary>
  55.   /// 字符串型
  56.   /// </summary>
  57.   /// <returns>Hello World</returns>
  58.   [WebMethod]
  59.   public string HelloWorld()
  60.   {
  61.    return "Hello World";
  62.   }
  63.   /// <summary>
  64.   /// 整型
  65.   /// </summary>
  66.   /// <returns>Int</returns>
  67.   [WebMethod]
  68.   public int GetInt()
  69.   {
  70.    return 1234;
  71.   }
  72.   
  73.   /// <summary>
  74.   /// 布尔型
  75.   /// </summary>
  76.   /// <returns>Bool</returns>
  77.   [WebMethod]
  78.   public bool GetBool()
  79.   {
  80.    return true;
  81.   }
  82.   /// <summary>
  83.   /// 返回实体类
  84.   /// 必须是已序列化的类
  85.   /// </summary>
  86.   /// <returns>学生类</returns>
  87.   [WebMethod]
  88.   public Student GetStudent()
  89.   {
  90.    Student stu = new Student();
  91.    stu.Name = "张三";
  92.    stu.Age = 25;
  93.    stu.Sex = true;
  94.    return stu;
  95.   }
  96.   /// <summary>
  97.   /// 返回DataSet数据类型
  98.   /// </summary>
  99.   /// <returns>DataSet</returns>
  100.   [WebMethod]
  101.   public DataSet GetDataSet()
  102.   {
  103.    DataSet ds = new DataSet();
  104.    return ds;
  105.   }
  106.  }
  107.  #region 定义可序列化类
  108.  /*
  109.   * 为避免Framework1.1中返回实体类报错“请求格式无法识别。”
  110.   * 要在Web.Config文件中<system.web>添加以下内容:
  111.   * <webServices>
  112.    <protocols>
  113.    <add name="HttpPost" />
  114.    <add name="HttpGet" />
  115.    </protocols>
  116.   </webServices>
  117.   */
  118.  //指示下面的类可序列化
  119.  /// <summary>
  120.  /// 学生基本信息类
  121.  /// </summary>
  122.  [Serializable]
  123.  public class Student 
  124.  {
  125.   /// <summary>
  126.   /// 构造函数
  127.   /// </summary>
  128.   public Student()
  129.   {
  130.   }
  131.   private string name;
  132.   /// <summary>
  133.   /// 姓名
  134.   /// </summary>
  135.   public string Name
  136.   {
  137.    get
  138.    {
  139.     return name;
  140.    }
  141.    set
  142.    {
  143.     name=value;
  144.    }
  145.   }
  146.   private bool sex;
  147.   /// <summary>
  148.   /// 性别--布尔型变量真为女,假为男
  149.   /// </summary>
  150.   public bool Sex
  151.   {
  152.    get
  153.    {
  154.     return sex;
  155.    }
  156.    set
  157.    {
  158.     sex=value;
  159.    }
  160.   }
  161.   private int age;
  162.   /// <summary>
  163.   /// 年龄
  164.   /// </summary>
  165.   public int Age
  166.   {
  167.    get
  168.    {
  169.     return age;
  170.    }
  171.    set
  172.    {
  173.     age=value;
  174.    }
  175.   }
  176.  }
  177.  #endregion
  178. }

调用WebService服务示例代码如下:

 

  1. using System;
  2. using System.Drawing;
  3. using System.Collections;
  4. using System.ComponentModel;
  5. using System.Windows.Forms;
  6. using StudentClient.localhost;
  7. namespace StudentClient
  8. {
  9.     /// <summary>
  10.     /// FrmDemo 的摘要说明。
  11.     /// </summary>
  12.     public class FrmDemo : System.Windows.Forms.Form
  13.     {
  14.         private System.Windows.Forms.Label label1;
  15.         private System.Windows.Forms.Label label2;
  16.         private System.Windows.Forms.Label label3;
  17.         private System.Windows.Forms.Label label4;
  18.         private System.Windows.Forms.TextBox txtName;
  19.         private System.Windows.Forms.Label label5;
  20.         private System.Windows.Forms.Button btnStu;
  21.         private System.Windows.Forms.Button btnInt;
  22.         private System.Windows.Forms.Button btnHello;
  23.         private System.Windows.Forms.Button btnDataSet;
  24.         private System.Windows.Forms.Button btnBool;
  25.         private System.Windows.Forms.TextBox txtSex;
  26.         private System.Windows.Forms.TextBox txtAge;
  27.         private System.Windows.Forms.TextBox txtOther;
  28.         /// <summary>
  29.         /// 必需的设计器变量。
  30.         /// </summary>
  31.         private System.ComponentModel.Container components = null;
  32.         public FrmDemo()
  33.         {
  34.             //
  35.             // Windows 窗体设计器支持所必需的
  36.             //
  37.             InitializeComponent();
  38.             //
  39.             // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
  40.             //
  41.         }
  42.         /// <summary>
  43.         /// 清理所有正在使用的资源。
  44.         /// </summary>
  45.         protected override void Dispose( bool disposing )
  46.         {
  47.             if( disposing )
  48.             {
  49.                 if(components != null)
  50.                 {
  51.                     components.Dispose();
  52.                 }
  53.             }
  54.             base.Dispose( disposing );
  55.         }
  56.         #region Windows 窗体设计器生成的代码
  57.         /// <summary>
  58.         /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  59.         /// 此方法的内容。
  60.         /// </summary>
  61.         private void InitializeComponent()
  62.         {
  63.             this.label1 = new System.Windows.Forms.Label();
  64.             this.label2 = new System.Windows.Forms.Label();
  65.             this.label3 = new System.Windows.Forms.Label();
  66.             this.label4 = new System.Windows.Forms.Label();
  67.             this.txtName = new System.Windows.Forms.TextBox();
  68.             this.txtSex = new System.Windows.Forms.TextBox();
  69.             this.txtAge = new System.Windows.Forms.TextBox();
  70.             this.txtOther = new System.Windows.Forms.TextBox();
  71.             this.label5 = new System.Windows.Forms.Label();
  72.             this.btnStu = new System.Windows.Forms.Button();
  73.             this.btnInt = new System.Windows.Forms.Button();
  74.             this.btnHello = new System.Windows.Forms.Button();
  75.             this.btnDataSet = new System.Windows.Forms.Button();
  76.             this.btnBool = new System.Windows.Forms.Button();
  77.             this.SuspendLayout();
  78.             // 
  79.             // label1
  80.             // 
  81.             this.label1.Location = new System.Drawing.Point(32, 40);
  82.             this.label1.Name = "label1";
  83.             this.label1.Size = new System.Drawing.Size(48, 16);
  84.             this.label1.TabIndex = 0;
  85.             this.label1.Text = "姓名:";
  86.             // 
  87.             // label2
  88.             // 
  89.             this.label2.Location = new System.Drawing.Point(32, 70);
  90.             this.label2.Name = "label2";
  91.             this.label2.Size = new System.Drawing.Size(48, 16);
  92.             this.label2.TabIndex = 1;
  93.             this.label2.Text = "性别:";
  94.             // 
  95.             // label3
  96.             // 
  97.             this.label3.Location = new System.Drawing.Point(136, 70);
  98.             this.label3.Name = "label3";
  99.             this.label3.Size = new System.Drawing.Size(48, 16);
  100.             this.label3.TabIndex = 2;
  101.             this.label3.Text = "年龄:";
  102.             // 
  103.             // label4
  104.             // 
  105.             this.label4.Font = new System.Drawing.Font("宋体", 14F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
  106.             this.label4.Location = new System.Drawing.Point(72, 8);
  107.             this.label4.Name = "label4";
  108.             this.label4.Size = new System.Drawing.Size(136, 24);
  109.             this.label4.TabIndex = 3;
  110.             this.label4.Text = "学生基本信息";
  111.             // 
  112.             // txtName
  113.             // 
  114.             this.txtName.Location = new System.Drawing.Point(80, 36);
  115.             this.txtName.Name = "txtName";
  116.             this.txtName.Size = new System.Drawing.Size(144, 21);
  117.             this.txtName.TabIndex = 4;
  118.             this.txtName.Text = "";
  119.             // 
  120.             // txtSex
  121.             // 
  122.             this.txtSex.Location = new System.Drawing.Point(80, 64);
  123.             this.txtSex.Name = "txtSex";
  124.             this.txtSex.Size = new System.Drawing.Size(48, 21);
  125.             this.txtSex.TabIndex = 5;
  126.             this.txtSex.Text = "";
  127.             // 
  128.             // txtAge
  129.             // 
  130.             this.txtAge.Location = new System.Drawing.Point(184, 64);
  131.             this.txtAge.Name = "txtAge";
  132.             this.txtAge.Size = new System.Drawing.Size(40, 21);
  133.             this.txtAge.TabIndex = 6;
  134.             this.txtAge.Text = "";
  135.             // 
  136.             // txtOther
  137.             // 
  138.             this.txtOther.Location = new System.Drawing.Point(80, 96);
  139.             this.txtOther.Name = "txtOther";
  140.             this.txtOther.Size = new System.Drawing.Size(128, 21);
  141.             this.txtOther.TabIndex = 7;
  142.             this.txtOther.Text = "";
  143.             // 
  144.             // label5
  145.             // 
  146.             this.label5.Location = new System.Drawing.Point(32, 100);
  147.             this.label5.Name = "label5";
  148.             this.label5.Size = new System.Drawing.Size(48, 16);
  149.             this.label5.TabIndex = 8;
  150.             this.label5.Text = "其它:";
  151.             // 
  152.             // btnStu
  153.             // 
  154.             this.btnStu.Location = new System.Drawing.Point(8, 128);
  155.             this.btnStu.Name = "btnStu";
  156.             this.btnStu.Size = new System.Drawing.Size(88, 24);
  157.             this.btnStu.TabIndex = 9;
  158.             this.btnStu.Text = "调用Student";
  159.             this.btnStu.Click += new System.EventHandler(this.btnStu_Click);
  160.             // 
  161.             // btnInt
  162.             // 
  163.             this.btnInt.Location = new System.Drawing.Point(200, 128);
  164.             this.btnInt.Name = "btnInt";
  165.             this.btnInt.Size = new System.Drawing.Size(32, 24);
  166.             this.btnInt.TabIndex = 10;
  167.             this.btnInt.Text = "Int";
  168.             this.btnInt.Click += new System.EventHandler(this.btnInt_Click);
  169.             // 
  170.             // btnHello
  171.             // 
  172.             this.btnHello.Location = new System.Drawing.Point(152, 128);
  173.             this.btnHello.Name = "btnHello";
  174.             this.btnHello.Size = new System.Drawing.Size(48, 24);
  175.             this.btnHello.TabIndex = 11;
  176.             this.btnHello.Text = "Hello";
  177.             this.btnHello.Click += new System.EventHandler(this.btnHello_Click);
  178.             // 
  179.             // btnDataSet
  180.             // 
  181.             this.btnDataSet.Location = new System.Drawing.Point(96, 128);
  182.             this.btnDataSet.Name = "btnDataSet";
  183.             this.btnDataSet.Size = new System.Drawing.Size(56, 24);
  184.             this.btnDataSet.TabIndex = 12;
  185.             this.btnDataSet.Text = "DataSet";
  186.             this.btnDataSet.Click += new System.EventHandler(this.btnDataSet_Click);
  187.             // 
  188.             // btnBool
  189.             // 
  190.             this.btnBool.Location = new System.Drawing.Point(232, 128);
  191.             this.btnBool.Name = "btnBool";
  192.             this.btnBool.Size = new System.Drawing.Size(40, 24);
  193.             this.btnBool.TabIndex = 13;
  194.             this.btnBool.Text = "Bool";
  195.             this.btnBool.Click += new System.EventHandler(this.btnBool_Click);
  196.             // 
  197.             // FrmDemo
  198.             // 
  199.             this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
  200.             this.ClientSize = new System.Drawing.Size(280, 158);
  201.             this.Controls.Add(this.btnBool);
  202.             this.Controls.Add(this.btnDataSet);
  203.             this.Controls.Add(this.btnHello);
  204.             this.Controls.Add(this.btnInt);
  205.             this.Controls.Add(this.btnStu);
  206.             this.Controls.Add(this.label5);
  207.             this.Controls.Add(this.txtOther);
  208.             this.Controls.Add(this.txtAge);
  209.             this.Controls.Add(this.txtSex);
  210.             this.Controls.Add(this.txtName);
  211.             this.Controls.Add(this.label4);
  212.             this.Controls.Add(this.label3);
  213.             this.Controls.Add(this.label2);
  214.             this.Controls.Add(this.label1);
  215.             this.Name = "FrmDemo";
  216.             this.Text = "FrmDemo";
  217.             this.ResumeLayout(false);
  218.         }
  219.         #endregion
  220.         /// <summary>
  221.         /// 调用学生信息
  222.         /// </summary>
  223.         /// <param name="sender"></param>
  224.         /// <param name="e"></param>
  225.         private void btnStu_Click(object sender, System.EventArgs e)
  226.         {
  227.             //实例化服务类
  228.             Demo dm = new Demo();
  229.             //调用返回实体类服务方法
  230.             Student stu = dm.GetStudent();
  231.             txtName.Text = stu.Name;
  232.             txtSex.Text = (stu.Sex==false?"女":"男");
  233.             txtAge.Text = stu.Age.ToString();
  234.             
  235.         }
  236.         /// <summary>
  237.         /// DataSet数据
  238.         /// </summary>
  239.         /// <param name="sender"></param>
  240.         /// <param name="e"></param>
  241.         private void btnDataSet_Click(object sender, System.EventArgs e)
  242.         {
  243.             //实例化服务类
  244.             Demo dm = new Demo();
  245.             txtOther.Text = dm.GetDataSet().Tables.Count.ToString();
  246.         }
  247.         /// <summary>
  248.         /// 返回字符串
  249.         /// </summary>
  250.         /// <param name="sender"></param>
  251.         /// <param name="e"></param>
  252.         private void btnHello_Click(object sender, System.EventArgs e)
  253.         {
  254.             //实例化服务类
  255.             Demo dm = new Demo();
  256.             txtOther.Text = dm.HelloWorld();
  257.         }
  258.         /// <summary>
  259.         /// 返回整型
  260.         /// </summary>
  261.         /// <param name="sender"></param>
  262.         /// <param name="e"></param>
  263.         private void btnInt_Click(object sender, System.EventArgs e)
  264.         {
  265.             //实例化服务类
  266.             Demo dm = new Demo();
  267.             txtOther.Text = dm.GetInt().ToString();
  268.         }
  269.         /// <summary>
  270.         /// 返回布尔型
  271.         /// </summary>
  272.         /// <param name="sender"></param>
  273.         /// <param name="e"></param>
  274.         private void btnBool_Click(object sender, System.EventArgs e)
  275.         {
  276.             //实例化服务类
  277.             Demo dm = new Demo();
  278.             txtOther.Text = dm.GetBool().ToString();
  279.         }
  280.     }
  281. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值