学习 WCF (2)--开发WCF服务

mac2022-06-30  37

在上一篇中和大家复习了有关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实体类作为参数。

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<Student>    {    }}

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

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

实现WCF服务契约

实在一个WCF服务契约就行写一个类一样容易,这里我们先创建一个处理Student的类。StudentManage

namespace WCFStudent{    public static class StudentManage    {        private static DataTable TD_stu;        static StudentManage()        {            TD_stu = new DataTable();            TD_stu.Columns.Add(new DataColumn("Name"));            TD_stu.Columns.Add(new DataColumn("Sex"));            TD_stu.Columns.Add(new DataColumn("School"));        }        public static void AddStudent(string name, string sex, string school)        {            DataRow row = TD_stu.NewRow();            row["Name"] = name;            row["Sex"] = sex;            row["School"] = school;            TD_stu.Rows.Add(row);        }        public static IEnumerable GetStudent()        {            return TD_stu.DefaultView;        }    }}

接下来创建一个类WCFStudentText,实现接口IStuServiceContract

 

namespace WCFStudent{    public class WCFStudentText:IStuServiceContract    {        public WCFStudentText()        {        //        //TODO: 在此处添加构造函数逻辑        //        }        public void AddStudent(Student stu)        {            StudentManage.AddStudent(stu.StuName, stu.StuSex, stu.StuSchool);        }        public stuCollection GetStudent()        {            IEnumerable list = StudentManage.GetStudent();            stuCollection stucollect = new stuCollection();            Student stu;            IEnumerator iter = list.GetEnumerator();//通过GetEnumerator方法获得IEnumerator对象的引用            bool first = true;            PropertyDescriptorCollection pds = null;            while (iter.MoveNext())//用IEnumerator对象对存储在IEnumerator集合中的Student信息进行迭代,每一个PropertyDescriptor都是一个学生的信息            {                if (first)                {                    first = false;                    pds = TypeDescriptor.GetProperties(iter.Current);                }                stu = new Student();                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);            }            return stucollect;        }    }}

 用IEnumerator对象对存储在IEnumerator集合中的Student信息进行迭代,每一个PropertyDescriptor都是一个学生的信息。

驻留WCF服务

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

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

最后我们要做的就是修改Web.config文件:

 

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

 

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

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

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

转载于:https://www.cnblogs.com/deepwishly/archive/2010/08/27/2551207.html

相关资源:WCF全析-服务配置部署详解
最新回复(0)