Web service

来源:互联网 发布:java 时间戳处理 编辑:程序博客网 时间:2024/06/05 09:02

最近在有接触web Service,了解到Web Service 中的方法不能重载,不能有输出参数。想返回多个值的话,方法返回对象。

对象model代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data ;

namespace MyWebService
{
    public class ReturnObject
    {
        private string _Msg;
        public string Msg
        {
            get { return _Msg; }
            set { _Msg = value; }
        }
        private DataTable  _dtRecord;
        public DataTable dtRecord
        {
            get { return _dtRecord; }
            set { _dtRecord = value; }
        }
        private  bool _isSuccess;
        public bool isSuccess
        {
            get { return _isSuccess; }
            set { _isSuccess = value; }
        }
        private int _rowCount;
        public int rowCount
        {
            get { return _rowCount; }
            set { _rowCount = value; }
        }
    }
}

service方法:


         [WebMethod(Description="返回对象")]
        public ReturnObject getUserIfo()
        {

            DataSet ds = new DataSet();
            ReturnObject rbReturn = new ReturnObject();
            try
            {

                string strsql = "Select   USERID,USERNAME,TELEPHONE  from USERS";
                ds = SqlHelper.GetDataSet(strsql);
                rbReturn.dtRecord = ds.Tables[0];
                rbReturn.rowCount = ds.Tables[0].Rows.Count;
                rbReturn.isSuccess = true;
            }
            catch (Exception ex)
            {
                rbReturn.Msg = ex.ToString();
                rbReturn.isSuccess = false;
                throw ex;
               
            }
         
            return rbReturn;
        }


        [WebMethod(Description="返回的对象序列化")]
        public string  getUserIfoSerialize()
        {

            DataSet ds = new DataSet();
            ReturnObject rbReturn = new ReturnObject();
            try
            {

                string strsql = "Select   *  from MyInfo";
                ds = SqlHelper.GetDataSet(strsql);
                rbReturn.dtRecord = ds.Tables[0];
                rbReturn.rowCount = ds.Tables[0].Rows.Count;
                rbReturn.isSuccess = true;
            }
            catch (Exception ex)
            {
                rbReturn.Msg = ex.ToString();
                rbReturn.isSuccess = false;
                throw ex;

            }
            string strResult = JsonConvert.SerializeObject(rbReturn, Formatting.Indented);
            return strResult;
        }


后台调用webService :前台button

   protected void Button1_Click(object sender, EventArgs e)
        {
            LocalService.Service1SoapClient web = new LocalService.Service1SoapClient();
            string str = web.getUserIfoSerialize();

        }



0 0
原创粉丝点击