`
lovnet
  • 浏览: 6711732 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
文章分类
社区版块
存档分类
最新评论

Learn WCF (2)--开发WCF服务

阅读更多

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

Code
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->namespaceWCFStudent
{
[ServiceContract]
publicinterfaceIStuServiceContract
{

[OperationContract]
voidAddStudent(Studentstu);

[OperationContract]
stuCollectionGetStudent();

}

[DataContract]
publicclassStudent
{
privatestring_stuName;
privatestring_stuSex;
privatestring_stuSchool;

[DataMember]
publicstringStuName
{
get{return_stuName;}
set{_stuName=value;}
}

[DataMember]
publicstringStuSex
{
get{return_stuSex;}
set{_stuSex=value;}
}

[DataMember]
publicstringStuSchool
{
get{return_stuSchool;}
set{_stuSchool=value;}
}
}

publicclassstuCollection:List<Student>
{

}
}

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

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

实现WCF服务契约

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

Code
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->namespaceWCFStudent
{
publicstaticclassStudentManage
{
privatestaticDataTableTD_stu;
staticStudentManage()
{
TD_stu
=newDataTable();
TD_stu.Columns.Add(
newDataColumn("Name"));
TD_stu.Columns.Add(
newDataColumn("Sex"));
TD_stu.Columns.Add(
newDataColumn("School"));
}

publicstaticvoidAddStudent(stringname,stringsex,stringschool)
{
DataRowrow
=TD_stu.NewRow();
row[
"Name"]=name;
row[
"Sex"]=sex;
row[
"School"]=school;
TD_stu.Rows.Add(row);
}

publicstaticIEnumerableGetStudent()
{
returnTD_stu.DefaultView;
}
}
}

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

Code
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->namespaceWCFStudent
{
publicclassWCFStudentText:IStuServiceContract
{
publicWCFStudentText()
{
//
//TODO:在此处添加构造函数逻辑
//
}

publicvoidAddStudent(Studentstu)
{
StudentManage.AddStudent(stu.StuName,stu.StuSex,stu.StuSchool);
}

publicstuCollectionGetStudent()
{
IEnumerablelist
=StudentManage.GetStudent();
stuCollectionstucollect
=newstuCollection();
Studentstu;
IEnumeratoriter
=list.GetEnumerator();//通过GetEnumerator方法获得IEnumerator对象的引用

boolfirst=true;
PropertyDescriptorCollectionpds
=null;
while(iter.MoveNext())//用IEnumerator对象对存储在IEnumerator集合中的Student信息进行迭代,每一个PropertyDescriptor都是一个学生的信息
{
if(first)
{
first
=false;
pds
=TypeDescriptor.GetProperties(iter.Current);
}

stu
=newStudent();
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);
}

returnstucollect;
}
}
}

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

驻留WCF服务

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

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

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

Code
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><system.serviceModel>
<services>
<servicename="WCFStudent.WCFStudentText"behaviorConfiguration="ServiceBehavior">
<!--ServiceEndpoints-->
<endpointaddress=""binding="wsHttpBinding"contract="WCFStudent.IStuServiceContract">
<!--
部署时,应删除或替换下列标识元素,以反映
在其下运行部署服务的标识。删除之后,WCF将
自动推导相应标识。
-->
<identity>
<dnsvalue="localhost"/>
</identity>
</endpoint>
<endpointaddress="mex"binding="mexHttpBinding"contract="IMetadataExchange"/>
</service>
</services>

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

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

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

分享到:
评论

相关推荐

    Programming WCF Services (4th).pdf 2015 第4版 无水印

    this guide provides unique insight, rather than documentation, to help you learn the topics and skills you need for building maintainable, extensible, and reusable WCF-based applications. Authors ...

    WCF.Multi-Layer.Services.Development.with.Entity.Framework.4th.Edition

    Chapter 2. Hosting the HelloWorld WCF Service Chapter 3. Deploying the HelloWorld WCF Service Chapter 4. Debugging the HelloWorld WCF Service Chapter 5. Implementing a Three-layer WCF Service Chapter ...

    Programming WCF Services(THIRD EDITION)

    WCF-based applications, illustrating how to take advantage of built-in features such as service hosting, instance management, concurrency management, transactions, dis- connected queued calls, ...

    Programming.WCF.Services.1491944838

    Hailed as the definitive treatment of WCF, this book provides unique insight, rather than documentation, to help you learn the topics and skills you need for building WCF-based applications that are ...

    Programming WCF Services(O'Reilly,4ed,2015)

    this guide provides unique insight, rather than documentation, to help you learn the topics and skills you need for building maintainable, extensible, and reusable WCF-based applications. Authors ...

    Programming WCF Services, 3rd Edition

    Hailed as the definitive treatment of WCF, this book provides unique insight, rather than documentation, to help you learn the topics and skills you need for building WCF-based applications that are ...

    [WCF] WCF 服务编程 第1版 (英文版)

    [奥莱理] WCF 服务编程 第1版 (英文版) [奥莱理] Programming WCF Services 1st Edition (E-Book) ☆ 图书概要:☆ Written by Microsoft software legend Juval Lowy, Programming WCF Services is the ...

    Packt.WCF.4.5.Multi-Layer.Services.Development.with.Entity.Framework.Dec.2012

    Explore and learn different debugging options for a WCF service. Build a multi-layer real-world WCF service from scratch to understand every layer of a WCF service and apply it to your real work. ...

    Programming WCF Services 4

    Learn WCF’s architecture and essential building blocks, including key concepts such as reliability and transport sessions Use built-in features such as service contracts, instance and concurrency ...

    Windows Communication Foundation 4 Step by Step

    With this practical, learn-by-doing tutorial, you get the clear guidance and hands-on examples you need to begin creating Web services for robust Windows-based business applications. Discover how to...

    WCF服务编程 (第3版)

    Hailed as the definitive treatment of WCF, this book provides unique insight, rather than documentation, to help you learn the topics and skills you need for building WCF-based applications that are ...

    Programming.WCF.Services.3rd.Edition

    Developers and architects will learn not only the "how" of WCF programming, but also relevant design guidelines, best practices, and pitfalls. Original techniques and utilities provided by the author...

    WCF & WPF 聊天程序源码

    But one good thing about that is that I generally share what I learn right here, and this article is one of the hardest ones I've done IMHO. &lt;br&gt;This article is about how to create a peer-to-peer...

    Wrox.Professional.WCF.4

    Written by a team of Microsoft MVPs and WCF experts, this book explains how the pieces of WCF 4.0 build on each other to provide a comprehensive framework to support distributed enterprise ...

    Professional WCF 4: Windows Communication Foundation with .NET 4

    WCF is the technology in .NET that you use to build service - oriented applications, to exchange messages in various communication scenarios, and to run workfl ows built from service operations. With...

    Microsoft Windows Communication Foundation 4.0 Cookbook for Developing SOA Applications

    One big plus is that you will learn to extend the existing WCF framework to achieve advanced functionality. You will find a dedicated chapter for RESTful and AJAX-enabled service development. Moving ...

    Microsoft BizTalk Server 2010 Patterns

    will learn both polling and query approaches to working with the WCF-SQL Adapter and how to expose different services in different manners from BizTalk. Chapter 9, Expanding the Solution with Services...

Global site tag (gtag.js) - Google Analytics