微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

WebService 服务可以返回任意类型

 

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;
   }
}


//指示下面的类可序列化
/// <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>
        /// 调用学生信息
        /// </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();          }      } } 也可以用过 xmlinclude申明实体类 [XmlInclude(typeof(Student))] //一定要加上 xmlinclude这一句,实体类是可序列化的          [WebMethod] 这里是类方法

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐