新聞中心
WCF是由微軟公司開發(fā)的一款.NET Framework 3.5的重要組成部件,它的影音方式很多,有很多重要的功能值得我們?nèi)ド钊胙芯?。比如今天為大家介紹的WCF服務(wù)契約就是其中一個比較重要的應(yīng)用知識。

一個WCF服務(wù)契約是一個用元數(shù)據(jù)屬性[ServiceContract]修飾的.NET接口或類。每個WCF服務(wù)可以有一個或多個契約,每個契約是一個操作集合。
首先我們定義一個.NET接口:IStuServiceContract,定義兩個方法分別實現(xiàn)添加和獲取學(xué)生信息的功能
- void AddStudent(Student stu);stuCollection GetStudent();
用WCF服務(wù)契約模型的元數(shù)據(jù)屬性ServiceContract標(biāo)注接口IStuServiceContract,把接口設(shè)計為WCF契約。用OperationContract標(biāo)注AddStudent,GetStudent
GetStudent()返回一個類型為stuCollection類型的集合。AddStudent()需要傳入Student實體類作為參數(shù)。
- namespace WCFStudent
- {
- [ServiceContract]
- public interface IStuServiceContract
- {
- [OperationContract]
- void AddStudent(Student stu);
- [OperationContract]
- stuCollection GetStudent();
- }
- [DataContract]
- public class Student
- {
- private string _stuName;
- private string _stuSex;
- private string _stuSchool;
- [DataMember]
- public string StuName
- {
- get { return _stuName; }
- set { _stuName = value; }
- }
- [DataMember]
- public string StuSex
- {
- get { return _stuSex; }
- set { _stuSex = value; }
- }
- [DataMember]
- public string StuSchool
- {
- get { return _stuSchool; }
- set { _stuSchool = value; }
- }
- }
- public class stuCollection : List
- {
- }
- }
WCF服務(wù)契約和客戶交換SOAP信息。在發(fā)送端必須把WCF服務(wù)和客戶交互的數(shù)據(jù)串行化為XML并在接收端把XML反串行化。因此客戶傳遞給AddStudent操作的Student對象也必須在發(fā)送到服務(wù)器之前串行化為XML。WCF默認(rèn)使用的是一個XML串行化器DataContractSerializer,用它對WCF服務(wù)和客戶交換的數(shù)據(jù)進(jìn)行串行化和反串行化。
網(wǎng)頁標(biāo)題:WCF服務(wù)契約開發(fā)實踐
地址分享:http://www.5511xx.com/article/dhgcjio.html


咨詢
建站咨詢
