实例源码下载:http://dldx.csdn.net/fd.php?i=684557114395064&s=8c825c30165d19e15bd0626914fe0e1c
1.开发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实体类作为参数。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.Runtime.Serialization;
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
{
set { _stuSchool = value; }
get { return _stuSchool; }
}
}
public class stuCollection : List<Student>
{
}
}
WCF服务和客户交换SOAP信息。在发送端必须把WCF服务和客户交互的数据串行化为XML并在接收端把XML反串行化。因此客户传递给AddStudent操作的Student对象也必须在发送到服务器之前串行化为XML。WCF默认使用的是一个XML串行化器DataContractSerializer,用它对WCF服务和客户交换的数据进行串行化和反串行化。
作为开发人员,我们必须要做的是用元数据属性DataContract标注WCF和其客户所交换的数据的类型。用元数据属性DataMember标注交换数据类型中要串行化的属性。(详细看上面的代码)
实现WCF服务契约
实在一个WCF服务契约就行写一个类一样容易,这里我们先创建一个处理Student的类。StudentManage
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Collections;
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data;
using System.ComponentModel;
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文件:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<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>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
</configuration>
将WCF服务的名称设为WCFStudent.WCFStudentText,WCF服务端点(EndPoint)的服务契约设定为我们所编写的契约WCFStudent.IStuServiceContract
当然我们可以用VS2008直接创建WCF工程,将会给我们带来很多方便。
这样,一个WCF服务就完成了。
添加一个Web引用
使用svcutil.exe工具
编程方案
1.添加一个Web引用
这个和添加引用Web服务的方法基本一致,在添加引用的对话框中输入URL:http://localhost:39113/WCFServiceText/WCFStudentText.svc
为WCF起个名字,点击添加引用按钮将会完成如下的任务:
(1)从指定的URL为学生管理服务下载WSDL文件
(2)生成代理类WCFStudentText,它是服务器WCFStudentText的代理,实现了服务器契约IStuServiceContract。
(3)生成响应的配置设置
现在我们就可以用代理类WCFStudentText与学生信息管理服务进行通信了。在站点中添加一个页面,放入一个GridView和ObjectDataSource
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="wcfclient.Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" BackColor="White"
BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" CellPadding="4"
DataSourceID="ObjectDataSource1" ForeColor="Black" GridLines="Vertical">
<RowStyle BackColor="#F7F7DE" />
<FooterStyle BackColor="#CCCC99" />
<PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
<SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" TypeName="StuServiceContractClient" SelectMethod="GetStudent">
</asp:ObjectDataSource>
</div>
</form>
</body>
</html>
ObjectDataSourse的优点是不需要编写一行代码就可以调用代理类中的方法。这里应当注意的是TypeName,SelectMethod两个重要属性的写法,必须与代理类一致。
2.使用svcutil.exe工具
WCF带有一个工具svcutil.exe,它自动从指定的URL下载WSDL文档,为代理类生成并保存一个指定的文件中,用相应的配置设置生成相应的配置文件,执行下面命令:
svcutil.exe工具将自动完成下列工作:
(1)通过URL下载元数据文档(WSDL文档)。
(2)为代理类生成代码,并将代码保持到WCFStudentServiceClient.cs文件中。
(3)生成配置设置并将其保存到Web.config中。
检查svcutil.exe多运行的目录,就会看到文件WCFStudentServiceClient.cs和Web.config。文件中的代码这里就不考出来了,大家可以自己试一下。将这两个文件导入到一个站点中。
只需将ObjectDataSource的代码改为:
源码下载:http://dldx.csdn.net/fd.php?i=684557114395064&s=8c825c30165d19e15bd0626914fe0e1c