webservice今日遇到的二个问题:DataTable + Namespace
时间:2011-04-23 来源:菩提树下的杨过
自从ms推出wcf以后,几乎没在web项目中用过webservice了,基本上都是以wcf host在iis中替换的。今天利用公司以前的web框架做一个新项目,里面大量使用了asmx,在开发过程中遇到了二个很少见的问题,记录如此,可能会帮助到遇到类似问题的同学们:
一、在WebService中避免用DataTable做为参数类型,强烈推荐用DataSet来替换DataTable,否则会死得很惨
场景如下:
一个解决方案分三层:一层是Model,一层是WebService,一层是WebSite
WebService层中提供基本的查询方法,但是调用方法时,必须提供登录后的验证票据信息.
Model层中定义了验证票据Ticket类
WebSite调用WebService层
结构图如下:
Model层中的Ticket类如下:
namespace Model { public class Ticket { public int UserID { set; get; } public string UserName { set; get; } public string Role { set; get; } public Ticket() { this.UserID = -1; this.UserName = ""; this.Role = ""; } } }WebService中的MyService.cs如下:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Data; using System.Web.Services; using Model; /// <summary> /// Summary description for MyService /// </summary> [WebService(Namespace = "http://yjmyzz.cnblogs.com/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. // [System.Web.Script.Services.ScriptService] public class MyService : System.Web.Services.WebService { public MyService() { //Uncomment the following line if using designed components //InitializeComponent(); } [WebMethod(Description = "根据用户验证票据获取指定信息")] public int Query(ref DataTable dt, ref Ticket t) { //string sql = "select * from Table where id =" + t.UserID; //根据验证票据查询相关信息,如果有数据,将放到参数dt中 return 1;//假定成功且有数据返回1,失败返回-1,成功但无数据返回0 } }WebSite中的调用代码如下:
using System; using System.Data; using YJM; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { MyService ws = new MyService(); //这里简化起见,直接生成一个票据 Ticket t = new Ticket() { Role = "普通会员", UserID = 1, UserName = "杨俊明" }; DataTable dt = new DataTable("myTableName"); ws.Query(ref dt, ref t); } }然后好玩的事情发生了,Website层中的Ticket实例,在传递到WebServie层中时,信息丢失了,变成了null ,有图有真相: 调用前的截图 进入WebService层的截图:
相关阅读 更多 +