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
        欢迎转载,转载时请注明出处。谢谢合作。

  • 相关阅读:
    ElEmentUI选择器弹出框定位错乱问题解决(弹出框出现在左上角)
    Element中(Notification)通知组件字体修改(Vue项目中Element的Notification修改字体)
    解决谷歌浏览器设置font-family属性不起作用,(css中设置了font-family:没有用)css字体属性没用
    开发环境Vue访问后端接口教程(前后端分离开发,端口不同下跨域访问)
    nested exception is org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.报错解决
    ssm项目中中文字符乱码
    idea使用PlantUML画类图教程
    org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.TooManyResultsException: Expected one result (or null) to be returned by selectOne(), but found: 3报错解决
    安装anaconda python时只能安装到默认文件夹&& 安装提示文件夹以存在问题
    生产者消费者代码学习,Producer_Consuner
  • 原文地址:https://www.cnblogs.com/soundcode/p/2130996.html
Copyright © 2011-2022 走看看