在上一篇中和大家复习了有关WCF的一些基础知识,这篇通过实例和大家分享如何开发一个获取,添加学生信息的WCF服务。
开发WCF服务的端点需要涉及下面几个任务:
开发服务契约:指定端点可用的WCF服务的操作。
开发绑定:绑定指点端点与外界通信的协议。
添加,删除,更新和配置端点:在配置文件中添加和绑定端点(当然也可以用编码的形式,但是不推荐。)
添加行为:一个行为就是一个组件,能增强服务,端点,和操作的运行时行为。
开发一个WCF服务契约
一个WCF服务契约是一个用元数据属性[ServiceContract]修饰的.NET接口或类。每个WCF服务可以有一个或多个契约,每个契约是一个操作集合。
首先我们定义一个.NET接口:IStuServiceContract,定义两个方法分别实现添加和获取学生信息的功能
void AddStudent(Student stu);stuCollection GetStudent();
用WCF服务模型的元数据属性ServiceContract标注接口IStuServiceContract,把接口设计为WCF契约。用OperationContract标注AddStudent,GetStudent
GetStudent()返回一个类型为stuCollection类型的集合。AddStudent()需要传入Student实体类作为参数。
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->namespaceWCFStudent
{
[ServiceContract]
publicinterfaceIStuServiceContract
{
[OperationContract]
voidAddStudent(Studentstu);
[OperationContract]
stuCollectionGetStudent();
}
[DataContract]
publicclassStudent
{
privatestring_stuName;
privatestring_stuSex;
privatestring_stuSchool;
[DataMember]
publicstringStuName
{
get{return_stuName;}
set{_stuName=value;}
}
[DataMember]
publicstringStuSex
{
get{return_stuSex;}
set{_stuSex=value;}
}
[DataMember]
publicstringStuSchool
{
get{return_stuSchool;}
set{_stuSchool=value;}
}
}
publicclassstuCollection:List<Student>
{
}
}
WCF服务和客户交换SOAP信息。在发送端必须把WCF服务和客户交互的数据串行化为XML并在接收端把XML反串行化。因此客户传递给AddStudent操作的Student对象也必须在发送到服务器之前串行化为XML。WCF默认使用的是一个XML串行化器DataContractSerializer,用它对WCF服务和客户交换的数据进行串行化和反串行化。
作为开发人员,我们必须要做的是用元数据属性DataContract标注WCF和其客户所交换的数据的类型。用元数据属性DataMember标注交换数据类型中要串行化的属性。(详细看上面的代码)
实现WCF服务契约
实在一个WCF服务契约就行写一个类一样容易,这里我们先创建一个处理Student的类。StudentManage
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->namespaceWCFStudent
{
publicstaticclassStudentManage
{
privatestaticDataTableTD_stu;
staticStudentManage()
{
TD_stu=newDataTable();
TD_stu.Columns.Add(newDataColumn("Name"));
TD_stu.Columns.Add(newDataColumn("Sex"));
TD_stu.Columns.Add(newDataColumn("School"));
}
publicstaticvoidAddStudent(stringname,stringsex,stringschool)
{
DataRowrow=TD_stu.NewRow();
row["Name"]=name;
row["Sex"]=sex;
row["School"]=school;
TD_stu.Rows.Add(row);
}
publicstaticIEnumerableGetStudent()
{
returnTD_stu.DefaultView;
}
}
}
接下来创建一个类WCFStudentText,实现接口IStuServiceContract
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->namespaceWCFStudent
{
publicclassWCFStudentText:IStuServiceContract
{
publicWCFStudentText()
{
//
//TODO:在此处添加构造函数逻辑
//
}
publicvoidAddStudent(Studentstu)
{
StudentManage.AddStudent(stu.StuName,stu.StuSex,stu.StuSchool);
}
publicstuCollectionGetStudent()
{
IEnumerablelist=StudentManage.GetStudent();
stuCollectionstucollect=newstuCollection();
Studentstu;
IEnumeratoriter=list.GetEnumerator();//通过GetEnumerator方法获得IEnumerator对象的引用
boolfirst=true;
PropertyDescriptorCollectionpds=null;
while(iter.MoveNext())//用IEnumerator对象对存储在IEnumerator集合中的Student信息进行迭代,每一个PropertyDescriptor都是一个学生的信息
{
if(first)
{
first=false;
pds=TypeDescriptor.GetProperties(iter.Current);
}
stu=newStudent();
stu.StuName=(string)pds["Name"].GetValue(iter.Current);
stu.StuSex=(string)pds["Sex"].GetValue(iter.Current);
stu.StuSchool=(string)pds["School"].GetValue(iter.Current);
stucollect.Add(stu);
}
returnstucollect;
}
}
}
用IEnumerator对象对存储在IEnumerator集合中的Student信息进行迭代,每一个PropertyDescriptor都是一个学生的信息。
驻留WCF服务
添加一个ADO.NET数据服务文件WCFStudentText.svc,并修改文件的内容为:
<%@ ServiceHost Service="WCFStudent.WCFStudentText"%>
最后我们要做的就是修改Web.config文件:
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><system.serviceModel>
<services>
<servicename="WCFStudent.WCFStudentText"behaviorConfiguration="ServiceBehavior">
<!--ServiceEndpoints-->
<endpointaddress=""binding="wsHttpBinding"contract="WCFStudent.IStuServiceContract">
<!--
部署时,应删除或替换下列标识元素,以反映
在其下运行部署服务的标识。删除之后,WCF将
自动推导相应标识。
-->
<identity>
<dnsvalue="localhost"/>
</identity>
</endpoint>
<endpointaddress="mex"binding="mexHttpBinding"contract="IMetadataExchange"/>
</service>
</services>
将WCF服务的名称设为WCFStudent.WCFStudentText,WCF服务端点(EndPoint)的服务契约设定为我们所编写的契约WCFStudent.IStuServiceContract
当然我们可以用VS2008直接创建WCF工程,将会给我们带来很多方便。
这样,一个WCF服务就完成了。