C# 中WebService返回類型(string,int,bool,DataSet,class實體類)示例

  WebService 服務可以返回任何可序列化的對象.本文代碼給出返回基本數據類型及實體類結構示例和調用代碼示例.

WebService代碼如下:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Web;
using System.Web.Services;
namespace StudentServer
{
 /// <summary>
 /// 本類實現WebService服務
 /// 提供對各種數據類型的返回例子
 /// 包括:
 ///  基本數據類型(string,ini,bool,long,float等)
 ///  類結構型(class),必須是可序列化的類
 ///  DataSet類型
 /// </summary>
 public class Demo : System.Web.Services.WebService
 {
  public Demo()
  {
   //CODEGEN: 該調用是 ASP.NET Web 服務設計器所必需的
   InitializeComponent();
  }
  #region 組件設計器生成的代碼
  
  //Web 服務設計器所必需的
  private IContainer components = null;
    
  /// <summary>
  /// 設計器支持所需的方法 - 不要使用代碼編輯器修改
  /// 此方法的內容。
  /// </summary>
  private void InitializeComponent()
  {
  }
  /// <summary>
  /// 清理所有正在使用的資源。
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if(disposing && components != null)
   {
    components.Dispose();
   }
   base.Dispose(disposing);  
  }
  
  #endregion
  // WEB 服務示例
  // HelloWorld() 示例服務返回字符串 Hello World
  // 若要生成,請取消註釋下列行,然後保存並生成項目
  // 若要測試此 Web 服務,請按 F5 鍵
  /// <summary>
  /// 字符串型
  /// </summary>
  /// <returns>Hello World</returns>
  [WebMethod]
  public string HelloWorld()
  {
   return "Hello World";
  }
  /// <summary>
  /// 整型
  /// </summary>
  /// <returns>Int</returns>
  [WebMethod]
  public int GetInt()
  {
   return 1234;
  }
  
  /// <summary>
  /// 布爾型
  /// </summary>
  /// <returns>Bool</returns>
  [WebMethod]
  public bool GetBool()
  {
   return true;
  }
  /// <summary>
  /// 返回實體類
  /// 必須是已序列化的類
  /// </summary>
  /// <returns>學生類</returns>
  [WebMethod]
  public Student GetStudent()
  {
   Student stu = new Student();
   stu.Name = "張三";
   stu.Age = 25;
   stu.Sex = true;
   return stu;
  }
  /// <summary>
  /// 返回DataSet數據類型
  /// </summary>
  /// <returns>DataSet</returns>
  [WebMethod]
  public DataSet GetDataSet()
  {
   DataSet ds = new DataSet();
   return ds;
  }
 }
 #region 定義可序列化類
 /*
  * 爲避免Framework1.1中返回實體類報錯“請求格式無法識別。”
  * 要在Web.Config文件中<system.web>添加以下內容:
  * <webServices>
   <protocols>
   <add name="HttpPost" />
   <add name="HttpGet" />
   </protocols>
  </webServices>
  */
 //指示下面的類可序列化
 /// <summary>
 /// 學生基本信息類
 /// </summary>
 [Serializable]
 public class Student 
 {
  /// <summary>
  /// 構造函數
  /// </summary>
  public Student()
  {
  }
  private string name;
  /// <summary>
  /// 姓名
  /// </summary>
  public string Name
  {
   get
   {
    return name;
   }
   set
   {
    name=value;
   }
  }
  private bool sex;
  /// <summary>
  /// 性別--布爾型變量真爲女,假爲男
  /// </summary>
  public bool Sex
  {
   get
   {
    return sex;
   }
   set
   {
    sex=value;
   }
  }
  private int age;
  /// <summary>
  /// 年齡
  /// </summary>
  public int Age
  {
   get
   {
    return age;
   }
   set
   {
    age=value;
   }
  }
 }
 #endregion
}



調用WebService服務示例代碼如下:

 

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using StudentClient.localhost;
namespace StudentClient
{
    /// <summary>
    /// FrmDemo 的摘要說明。
    /// </summary>
    public class FrmDemo : System.Windows.Forms.Form
    {
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.Label label4;
        private System.Windows.Forms.TextBox txtName;
        private System.Windows.Forms.Label label5;
        private System.Windows.Forms.Button btnStu;
        private System.Windows.Forms.Button btnInt;
        private System.Windows.Forms.Button btnHello;
        private System.Windows.Forms.Button btnDataSet;
        private System.Windows.Forms.Button btnBool;
        private System.Windows.Forms.TextBox txtSex;
        private System.Windows.Forms.TextBox txtAge;
        private System.Windows.Forms.TextBox txtOther;
        /// <summary>
        /// 必需的設計器變量。
        /// </summary>
        private System.ComponentModel.Container components = null;
        public FrmDemo()
        {
            //
            // Windows 窗體設計器支持所必需的
            //
            InitializeComponent();
            //
            // TODO: 在 InitializeComponent 調用後添加任何構造函數代碼
            //
        }
        /// <summary>
        /// 清理所有正在使用的資源。
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if(components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }
        #region Windows 窗體設計器生成的代碼
        /// <summary>
        /// 設計器支持所需的方法 - 不要使用代碼編輯器修改
        /// 此方法的內容。
        /// </summary>
        private void InitializeComponent()
        {
            this.label1 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.label3 = new System.Windows.Forms.Label();
            this.label4 = new System.Windows.Forms.Label();
            this.txtName = new System.Windows.Forms.TextBox();
            this.txtSex = new System.Windows.Forms.TextBox();
            this.txtAge = new System.Windows.Forms.TextBox();
            this.txtOther = new System.Windows.Forms.TextBox();
            this.label5 = new System.Windows.Forms.Label();
            this.btnStu = new System.Windows.Forms.Button();
            this.btnInt = new System.Windows.Forms.Button();
            this.btnHello = new System.Windows.Forms.Button();
            this.btnDataSet = new System.Windows.Forms.Button();
            this.btnBool = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // label1
            // 
            this.label1.Location = new System.Drawing.Point(32, 40);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(48, 16);
            this.label1.TabIndex = 0;
            this.label1.Text = "姓名:";
            // 
            // label2
            // 
            this.label2.Location = new System.Drawing.Point(32, 70);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(48, 16);
            this.label2.TabIndex = 1;
            this.label2.Text = "性別:";
            // 
            // label3
            // 
            this.label3.Location = new System.Drawing.Point(136, 70);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(48, 16);
            this.label3.TabIndex = 2;
            this.label3.Text = "年齡:";
            // 
            // label4
            // 
            this.label4.Font = new System.Drawing.Font("宋體", 14F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
            this.label4.Location = new System.Drawing.Point(72, 8);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(136, 24);
            this.label4.TabIndex = 3;
            this.label4.Text = "學生基本信息";
            // 
            // txtName
            // 
            this.txtName.Location = new System.Drawing.Point(80, 36);
            this.txtName.Name = "txtName";
            this.txtName.Size = new System.Drawing.Size(144, 21);
            this.txtName.TabIndex = 4;
            this.txtName.Text = "";
            // 
            // txtSex
            // 
            this.txtSex.Location = new System.Drawing.Point(80, 64);
            this.txtSex.Name = "txtSex";
            this.txtSex.Size = new System.Drawing.Size(48, 21);
            this.txtSex.TabIndex = 5;
            this.txtSex.Text = "";
            // 
            // txtAge
            // 
            this.txtAge.Location = new System.Drawing.Point(184, 64);
            this.txtAge.Name = "txtAge";
            this.txtAge.Size = new System.Drawing.Size(40, 21);
            this.txtAge.TabIndex = 6;
            this.txtAge.Text = "";
            // 
            // txtOther
            // 
            this.txtOther.Location = new System.Drawing.Point(80, 96);
            this.txtOther.Name = "txtOther";
            this.txtOther.Size = new System.Drawing.Size(128, 21);
            this.txtOther.TabIndex = 7;
            this.txtOther.Text = "";
            // 
            // label5
            // 
            this.label5.Location = new System.Drawing.Point(32, 100);
            this.label5.Name = "label5";
            this.label5.Size = new System.Drawing.Size(48, 16);
            this.label5.TabIndex = 8;
            this.label5.Text = "其它:";
            // 
            // btnStu
            // 
            this.btnStu.Location = new System.Drawing.Point(8, 128);
            this.btnStu.Name = "btnStu";
            this.btnStu.Size = new System.Drawing.Size(88, 24);
            this.btnStu.TabIndex = 9;
            this.btnStu.Text = "調用Student";
            this.btnStu.Click += new System.EventHandler(this.btnStu_Click);
            // 
            // btnInt
            // 
            this.btnInt.Location = new System.Drawing.Point(200, 128);
            this.btnInt.Name = "btnInt";
            this.btnInt.Size = new System.Drawing.Size(32, 24);
            this.btnInt.TabIndex = 10;
            this.btnInt.Text = "Int";
            this.btnInt.Click += new System.EventHandler(this.btnInt_Click);
            // 
            // btnHello
            // 
            this.btnHello.Location = new System.Drawing.Point(152, 128);
            this.btnHello.Name = "btnHello";
            this.btnHello.Size = new System.Drawing.Size(48, 24);
            this.btnHello.TabIndex = 11;
            this.btnHello.Text = "Hello";
            this.btnHello.Click += new System.EventHandler(this.btnHello_Click);
            // 
            // btnDataSet
            // 
            this.btnDataSet.Location = new System.Drawing.Point(96, 128);
            this.btnDataSet.Name = "btnDataSet";
            this.btnDataSet.Size = new System.Drawing.Size(56, 24);
            this.btnDataSet.TabIndex = 12;
            this.btnDataSet.Text = "DataSet";
            this.btnDataSet.Click += new System.EventHandler(this.btnDataSet_Click);
            // 
            // btnBool
            // 
            this.btnBool.Location = new System.Drawing.Point(232, 128);
            this.btnBool.Name = "btnBool";
            this.btnBool.Size = new System.Drawing.Size(40, 24);
            this.btnBool.TabIndex = 13;
            this.btnBool.Text = "Bool";
            this.btnBool.Click += new System.EventHandler(this.btnBool_Click);
            // 
            // FrmDemo
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
            this.ClientSize = new System.Drawing.Size(280, 158);
            this.Controls.Add(this.btnBool);
            this.Controls.Add(this.btnDataSet);
            this.Controls.Add(this.btnHello);
            this.Controls.Add(this.btnInt);
            this.Controls.Add(this.btnStu);
            this.Controls.Add(this.label5);
            this.Controls.Add(this.txtOther);
            this.Controls.Add(this.txtAge);
            this.Controls.Add(this.txtSex);
            this.Controls.Add(this.txtName);
            this.Controls.Add(this.label4);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label1);
            this.Name = "FrmDemo";
            this.Text = "FrmDemo";
            this.ResumeLayout(false);
        }
        #endregion
        /// <summary>
        /// 調用學生信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnStu_Click(object sender, System.EventArgs e)
        {
            //實例化服務類
            Demo dm = new Demo();
            //調用返回實體類服務方法
            Student stu = dm.GetStudent();
            txtName.Text = stu.Name;
            txtSex.Text = (stu.Sex==false?"女":"男");
            txtAge.Text = stu.Age.ToString();
            
        }
        /// <summary>
        /// DataSet數據
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDataSet_Click(object sender, System.EventArgs e)
        {
            //實例化服務類
            Demo dm = new Demo();
            txtOther.Text = dm.GetDataSet().Tables.Count.ToString();
        }
        /// <summary>
        /// 返回字符串
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnHello_Click(object sender, System.EventArgs e)
        {
            //實例化服務類
            Demo dm = new Demo();
            txtOther.Text = dm.HelloWorld();
        }
        /// <summary>
        /// 返回整型
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnInt_Click(object sender, System.EventArgs e)
        {
            //實例化服務類
            Demo dm = new Demo();
            txtOther.Text = dm.GetInt().ToString();
        }
        /// <summary>
        /// 返回布爾型
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnBool_Click(object sender, System.EventArgs e)
        {
            //實例化服務類
            Demo dm = new Demo();
            txtOther.Text = dm.GetBool().ToString();
        }
    }
}

窗體界面如下圖:


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章