zoukankan      html  css  js  c++  java
  • 学习 WCF (2)开发WCF服务


    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服务就完成了。



        文章作者:高维鹏(Brian)
        文章出处:http://www.cnblogs.com/gaoweipeng
        欢迎转载,转载时请注明出处。谢谢合作。

  • 相关阅读:
    什么是接口测试?怎样做接口测试?
    python下批量执行多条py文件的方法
    Jmeter运行报错software caused connection abort:recv failed
    性能测试一般容易出现瓶颈点
    性能测试流程规范(较好文档)
    Jmeter代理录制获取登录参数_移动端设置代理
    Http请求与WebSocket请求区别(WebSocket协议简析)
    JSONObject方法提取响应数据中的值
    Jmeter学习资料、控件下载地址大全
    图解IntelliJ IDEA 13版本对Android SQLite数据库的支持
  • 原文地址:https://www.cnblogs.com/soundcode/p/2130996.html
Copyright © 2011-2022 走看看