WebService 服务可以返回任意类型
来源:互联网 发布:澳洲网络运营商 编辑:程序博客网 时间:2024/05/22 10:52
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]
这里是类方法
- WebService 服务可以返回任意类型
- WebService 服务返回类型设计
- Xfire webService 返回复杂类型
- webService怎么返回map类型
- C# 中WebService返回类型
- axis1.4发布返回复杂类型的webservice服务+代码访问+客户端访问
- axis1.4发布返回复杂类型的webservice服务+代码访问+客户端访问
- webService返回自定义类型的数据处理
- Objective-C id为什么可以存放任意类型的对象?
- null 为什么可以被转换成任意引用类型?
- 用PHP模拟服务器返回任意类型数据
- 基于gsoap开发WebService服务返回结构体数组
- [Java][Webservice]关于返回Resultset类型的问题
- 如何让WebService的返回类型不是XML
- xfire webService返回Map<String,List<E>>嵌套复杂类型
- C#WebService返回dynamic类型报错问题
- ATLServer WebService 如何返回任意内容的 SOAP 消息给客户 —— 改写atlsoap.h
- 让自定义的类型可以和任意的类型之间转换
- web项目中Log4j不输出到文件问题
- 《一线架构师》 - 书摘精要
- ASP.NET弹出对话框几种基本方法VB.NET
- Struts 2读书笔记-----Action访问Servlet API
- libevent项目分析(二) -- 例程分析
- WebService 服务可以返回任意类型
- Vim 复制粘贴探秘
- zoj 1610 Count the Colors
- XmlDocument/XmlTextWriter
- win7_fedora 双系统安装方法
- 数据库中间件技术的研究与应用
- 程序员技术练级攻略
- 倒序取字符串函数
- Java如何跳出多重循环