WCF服务契约开发实践

开发 开发工具
如果想要开发一个WCF服务契约,其操作步骤是比较简单的。我们可以通过本文介绍的内容对此进行一个详细的认识,并充分掌握其中应用技巧。

WCF是由微软公司开发的一款.NET Framework 3.5的重要组成部件,它的影音方式很多,有很多重要的功能值得我们去深入研究。比如今天为大家介绍的WCF服务契约就是其中一个比较重要的应用知识。

一个WCF服务契约是一个用元数据属性[ServiceContract]修饰的.NET接口或类。每个WCF服务可以有一个或多个契约,每个契约是一个操作集合。

首先我们定义一个.NET接口:IStuServiceContract,定义两个方法分别实现添加和获取学生信息的功能

  1. void AddStudent(Student stu);stuCollection GetStudent(); 

用WCF服务契约模型的元数据属性ServiceContract标注接口IStuServiceContract,把接口设计为WCF契约。用OperationContract标注AddStudent,GetStudent

GetStudent()返回一个类型为stuCollection类型的集合。AddStudent()需要传入Student实体类作为参数。

  1. namespace WCFStudent  
  2. {  
  3. [ServiceContract]  
  4. public interface IStuServiceContract  
  5. {  
  6. [OperationContract]  
  7. void AddStudent(Student stu);  
  8. [OperationContract]  
  9. stuCollection GetStudent();  
  10. }  
  11. [DataContract]  
  12. public class Student  
  13. {  
  14. private string _stuName;  
  15. private string _stuSex;  
  16. private string _stuSchool;  
  17. [DataMember]  
  18. public string StuName  
  19. {  
  20. get { return _stuName; }  
  21. set { _stuName = value; }  
  22. }  
  23. [DataMember]  
  24. public string StuSex  
  25. {  
  26. get { return _stuSex; }  
  27. set { _stuSex = value; }  
  28. }  
  29. [DataMember]  
  30. public string StuSchool  
  31. {  
  32. get { return _stuSchool; }  
  33. set { _stuSchool = value; }  
  34. }  
  35. }  
  36. public class stuCollection : List<Student> 
  37. {  
  38. }  

WCF服务契约和客户交换SOAP信息。在发送端必须把WCF服务和客户交互的数据串行化为XML并在接收端把XML反串行化。因此客户传递给AddStudent操作的Student对象也必须在发送到服务器之前串行化为XML。WCF默认使用的是一个XML串行化器DataContractSerializer,用它对WCF服务和客户交换的数据进行串行化和反串行化。

【编辑推荐】

  1. WCF异常处理特点体现
  2. WCF异步调用实际应用技巧分析
  3. WCF DateSet应用技巧详解
  4. WCF获取客户端IP应用经验分享
  5. WCF元数据应用方法介绍
责任编辑:曹凯 来源: 博客园
相关推荐

2010-02-24 16:58:14

WCF Session

2009-12-22 09:11:31

WCF双向通信

2010-03-01 16:04:31

WCF服务契约

2009-11-09 09:23:10

WCF数据契约

2010-02-22 15:27:05

WCF数据契约

2009-11-06 15:02:47

WCF契约查询

2009-12-22 11:29:27

WCF自定义集合类型

2010-02-23 13:46:37

WCF数据契约

2009-11-09 14:46:09

WCF集合契约等价

2023-02-16 11:58:51

契约开发测试​​

2009-11-05 16:27:51

WCF数据契约

2009-12-21 14:05:18

WCF契约

2009-12-21 10:00:46

WCF基础开发

2009-11-09 14:15:17

WCF集合类型

2010-03-01 18:11:40

WCF数据契约变更

2010-02-23 17:44:22

WCF数据契约

2010-02-23 13:54:43

WCF非定制数据契约集

2010-03-01 15:12:53

WCF回调契约

2010-02-24 17:36:33

WCF集合数据契约

2009-11-06 09:39:40

WCF契约
点赞
收藏

51CTO技术栈公众号