护士站之web service
2012-03-24 21:33 just doo it 阅读(237) 评论(0) 收藏 举报护士站的客户端采用windows moblie,后台数据通过web service提供,在这次护士站的开发中,我负责的工作就是web service的开发。
首先介绍下什么叫web service,简单来说,web service相当于一种远程的函数调用。我们可以将后台处理好的数据,通过web service给客户端调用,这样的好处是可以实现跨平台、跨语言,做到代码重用,比如你用.net写的web service,也可以给用java写的客户端调用。web serivce传递数据时采用的是soap协议,数据在浏览器最终是渲染成xml的形式。
1、在编写护士站的web service时,给客户端返回的数据类型主要有两种:实体类类型、链表类型。
a、实体类类型:当客户端通过web serivce获取的数据类型为实体类类型时,必须要在web service服务端添加下面这些代码,客户端才能识别出这些实体类。
[XmlInclude(typeof(SignCollection))]
[XmlInclude(typeof(ExecutiveOrder))]
[XmlInclude(typeof(NurseInfo))]
[XmlInclude(typeof(Patient))]
[XmlInclude(typeof(SoapSecurity))]
[XmlInclude(typeof(test))]
[XmlInclude(typeof(Ward))]
[XmlInclude(typeof(SignRecord))]
b、链表类型:当客户端通过web service获取的数据类型为链表时,客户端会自动将web service服务端的链表类型转换为数组类型。
2、在客户端获取web service的某种数据类型时,必须在该数据类型的前面加上引用web service时的命名空间,比如我在护士站引用web service时填写的命名空间为NurseService,当我想获取病区实体类对象时,必须得像下面这样写
1 NurseService.Ward=new NurseService.Ward();
3、权限验证,有权限的用户才能获取到web service返回的数据,否则返回空的数据,使用Soap标头实现权限验证。
a、首先自定义一个继承SoapHeader的类
public class SoapSecurity:SoapHeader
{
public string UserName;
public string PassWord;
}
b、然后在web service中声明这个自定义类,并且写一个函数来操作该自定义类来完成验证。
1 public class NurseService : System.Web.Services.WebService
2 {
3 public SoapSecurity security=new SoapSecurity(); //Soap标头对象
8 public bool Validate()
9 {
10 bool flag = false;
11 try
12 {
13 NurseInfo ni = new NurseDao().IsLogin(security.UserName, security.PassWord); //判断是否有权限
14 if (ni.UserName!=null && ni.PassWord!=null) //返回的实体类不为空,表示验证正确
15 {
16 flag = true;
17 }
18 }
19 catch (Exception e)
20 {
21 log_debug.Debug(e.Message + "" + e.StackTrace); //记录异常日记
22 }
23 return flag;
24 }
25
26 }
27
c、在需要验证的WebMethod前面加上[SoapHeader("security", Direction = SoapHeaderDirection.In)]。······security为上面自定义类的实例
1 [WebMethod(Description = "根据护士Id获取该护士的所有病人")]
2 [SoapHeader("security", Direction = SoapHeaderDirection.In)]
3 public List<Patient> GetPatientByNurseId(string nurseId)
4 {
5 List<Patient> patients = new List<Patient>(); //声明链表patients保存返回的病人数据
6
7 try
8 {
9 if (Validate()) //有权限时才将数据绑定到链表patients中,否则返回一个空的链表
10 {
11 patients = new NurseDao().GetPatientByNurseId(nurseId); //保存返回的病人数据
12 }
13 }
14 catch (Exception e)
15 {
16 log_debug.Debug(e.Message + "" + e.StackTrace); //记录异常日记
17 }
18
19 return patients;
20 }
d、此时在客户端要想获得调用WebMethod的权限,必须要声明SoapSecurity对象,并且该对象的属性值要符合一定的条件
1 ns.SoapSecurityValue = new SoapSecurity();
2 ns.SoapSecurityValue.UserName = 登陆界面.userName;
3 ns.SoapSecurityValue.PassWord = 登陆界面.passWord;
浙公网安备 33010602011771号