WCF学习笔记(二)

动手了,WCF

开发WCF服务的终结点需要涉及下面几个任务

开发服务契约:指定终结点可用的WCF服务的操作。

开发绑定:绑定指点终结点与外界通信的协议。

添加,删除,更新和配置端点:在配置文件中添加和绑定终结点(当然也可以用编码的形式,但是不推荐。)

添加行为:一个行为就是一个组件,能增强服务,终结点,和操作的运行时行为。

定义契约

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

如下代码:

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

WCF服务和客户交换SOAP信息。在发送端必须把WCF服务和客户交互的数据串行化为XML并在接收端把XML反串行化。WCF默认使用的是一个XML串行化器DataContractSerializer,用它对WCF服务和客户交换的数据进行串行化和反串行化。

作为开发人员,我们必须要做的是用元数据属性DataContract标注WCF和其客户所交换的数据的类型。用元数据属性DataMember标注交换数据类型中要串行化的属性。(详细看上面的代码)

实现WCF服务契约

就是实现一个类,也就是业务逻辑,其本质是和WCF没有直接联系的,供WCF服务调用而已。

View Code
 1 namespace WCFStudent
2 {
3 public static class StudentManage
4 {
5 private static DataTable TD_stu;
6 static StudentManage()
7 {
8 TD_stu = new DataTable();
9 TD_stu.Columns.Add(new DataColumn("Name"));
10 TD_stu.Columns.Add(new DataColumn("Sex"));
11 TD_stu.Columns.Add(new DataColumn("School"));
12 }
13
14 public static void AddStudent(string name, string sex, string school)
15 {
16 DataRow row = TD_stu.NewRow();
17 row["Name"] = name;
18 row["Sex"] = sex;
19 row["School"] = school;
20 TD_stu.Rows.Add(row);
21 }
22
23 public static IEnumerable GetStudent()
24 {
25 return TD_stu.DefaultView;
26 }
27 }
28 }

下面要创建一个类,来实现接口(也就是前面我们定义的契约),没什么需要特别说明的。

View Code
 1 namespace WCFStudent
2 {
3 public class WCFStudentText:IStuServiceContract
4 {
5 public WCFStudentText()
6 {
7 //
8 //TODO: 在此处添加构造函数逻辑
9 //
10 }
11
12 public void AddStudent(Student stu)
13 {
14 StudentManage.AddStudent(stu.StuName, stu.StuSex, stu.StuSchool);
15 }
16
17 public stuCollection GetStudent()
18 {
19 IEnumerable list = StudentManage.GetStudent();
20 stuCollection stucollect = new stuCollection();
21 Student stu;
22 IEnumerator iter = list.GetEnumerator();//通过GetEnumerator方法获得IEnumerator对象的引用
23
24 bool first = true;
25 PropertyDescriptorCollection pds = null;
26 while (iter.MoveNext())//用IEnumerator对象对存储在IEnumerator集合中的Student信息进行迭代,每一个PropertyDescriptor都是一个学生的信息
27 {
28 if (first)
29 {
30 first = false;
31 pds = TypeDescriptor.GetProperties(iter.Current);
32 }
33
34 stu = new Student();
35 stu.StuName = (string)pds["Name"].GetValue(iter.Current);
36 stu.StuSex = (string)pds["Sex"].GetValue(iter.Current);
37 stu.StuSchool = (string)pds["School"].GetValue(iter.Current);
38 stucollect.Add(stu);
39 }
40
41 return stucollect;
42 }
43 }
44 }

服务寄宿

驻留WCF服务

添加一个ADO.NET数据服务文件WCFStudentText.svc,并修改文件的内容为:

<%@ ServiceHost  Service="WCFStudent.WCFStudentText"%>

修改Web.config文件

 1 <system.serviceModel>
2 <services>
3 <service name="WCFStudent.WCFStudentText" behaviorConfiguration="ServiceBehavior">
4 <!-- Service Endpoints -->
5 <endpoint address="" binding="wsHttpBinding" contract="WCFStudent.IStuServiceContract">
6 <!--
7 部署时,应删除或替换下列标识元素,以反映
8 在其下运行部署服务的标识。删除之后,WCF 将
9 自动推导相应标识。
10 -->
11 <identity>
12 <dns value="localhost"/>
13 </identity>
14 </endpoint>
15 <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
16 </service>
17 </services>


将WCF服务的名称设为WCFStudent.WCFStudentText,WCF服务终结点(EndPoint)的服务契约设定为我们所编写的契约WCFStudent.IStuServiceContract

当然我们可以用VS2008直接创建WCF工程,将会给我们带来很多方便。

这样,一个WCF服务就完成了。

原文地址:https://www.cnblogs.com/xyang/p/2340477.html