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


本文转自左正博客园博客,原文链接:http://www.cnblogs.com/soundcode/archive/2011/08/08/2130996.html,如需转载请自行联系原作者


目录
相关文章
|
前端开发
WCF更新服务引用报错的原因之一
WCF更新服务引用报错的原因之一
|
C# 数据安全/隐私保护
c#如何创建WCF服务到发布(SqlServer版已经验证)
c#如何创建WCF服务到发布(SqlServer版已经验证)
88 0
|
网络协议 网络架构 Windows
框架学习——WCF框架
框架学习——WCF框架
321 0
|
安全 数据库连接 数据库
WCF服务创建到发布(SqlServer版)
在本示例开始之前,让我们先来了解一下什么是wcf? wcf有哪些特点? wcf是一个面向服务编程的综合分层架构。该架构的项层为服务模型层。 使用户用最少的时间和精力建立自己的软件产品和外界通信的模型。它使得开发者能够建立一个跨平台的安全、可信赖、事务性的解决方案。且能与已有系统兼容写作。 简单概括就是:一组数据通信的应用程序开发接口。
149 0
|
C++
WCF基础教程(二)——解析iis8和iis8.5+VS2013发布wcf服务问题
WCF基础教程(二)——解析iis8和iis8.5+VS2013发布wcf服务问题
164 0
WCF基础教程(二)——解析iis8和iis8.5+VS2013发布wcf服务问题
WCF使用纯代码的方式进行服务寄宿
服务寄宿的目的是为了开启一个进程,为WCF服务提供一个运行的环境。通过为服务添加一个或者多个终结点,使之暴露给潜在的服务消费,服务消费者通过匹配的终结点对该服务进行调用,除去上面的两种寄宿方式,还可以以纯代码的方式实现服务的寄宿工作。
908 0
|
Windows
WCF服务寄宿到IIS
一.WCF简介: Windows Communication Foundation(WCF)是由微软开发的一系列支持数据通信的应用程序框架,可以翻译为Windows 通讯开发平台。整合了原有的windows通讯的 .net Remoting,WebService,Socket的机制,并融合有HTTP和FTP的相关技术。
1112 0
WCF服务自我寄宿
WCF服务的寄宿方式 WCF寄宿方式是一种非常灵活的操作,可以寄宿在各种进程之中,常见的寄宿有: IIS服务、Windows服务、Winform程序、控制台程序中进行寄宿,从而实现WCF服务的运行,为调用者方便、高效提供服务调用。
1054 0
|
网络架构
(纯代码)快速创建wcf rest 服务
因为有一个小工具需要和其它的业务对接数据,所以就试一下看能不能弄一个无需配置快速对接的方法出来,百(以)度(讹)过(传)后(讹),最后还是对照wcf配置对象调试出来了: 1.创建WebHttpBinding 2.
1028 0