类库只是类的集合,不能单独运行。

WCF服务程序项目是可以直接托管执行的,svc文件属于WCF服务的特定文件扩展名,IIS里有对应的处理程序。

WCF服务程序可以直接IIS里托管即可,WCF类库需要一个程序来引用并托管它。


1、创建WCF服务库项目

在解决方案中会自动生成了两个类文件“IService.cs”和“Service.cs”。

这两个类文件是两个WCF示例文件,对我们开发没有什么用处,现在我们删掉这两个文件。

Student.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Runtime.Serialization;

namespace WcfServiceLibrary1

{

/// DataContract数据约定,Person就是传递的消息中的内容。好比信中的文字。

/// 为了保证此类在WCF调用中能够被序列化,我们在Person类上面加入[DataContract]标签,在每个需要序列化的成员变量上加入[DataMember]标签。

/// 这两个标签在使用的进候需要导入using System.Runtime.Serialization命名空间。

[DataContract]

public class Student

{

[DataMember]

public string Id;

[DataMember]

public string Name;

[DataMember]

public int Age;

[DataMember]

public int Sex;

}

}

创建服务接口,声明对外发布的类和方法。

IStudentService.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.ServiceModel;

namespace WcfServiceLibrary1

{

/// <summary>

/// ServiceContract:服务约定,代表我们所能操作的接口集合,提供功能点。

/// 在IStudentService接口上面,我们定义了[ServiceContract]标签,此标签代表此接口及实现此接口的类都是对外发布的Service类,

/// 在每个需要对外发布的方法上都加上[OperationContract]标签,以使外部可以访问到此方法。

/// [ServiceContract]和[OperationContract]这两个标签需要导入using System.ServiceModel命名空间。

/// </summary>

[ServiceContract]

public interface IStudentService

{

/// <summary>

/// OperationContract 操作约定,定义每个操作的接口点方法。

/// </summary>

/// <param name="person">要添加的学生实体</param>

[OperationContract]

void AddStudent(Student person);

[OperationContract]

List<Student> GetAllStudents();

[OperationContract]

void RemoveStudent(string id);

}

}

实现我们上面声明的服务接口,实现对Student的添加、删除和检索的具体功能。

StudentService.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.ServiceModel;

namespace WcfServiceLibrary1

{

/// <summary>

/// 此类是对IStudentService接口的具体实现,在此类的上面我们声明了[ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)]标签,

/// 此标签代表这个类采用SingleTone(单类模式)来生成对象。

/// 使用[ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)]接口需要导入using System.ServiceModel;命名空间。

/// </summary>

[ServiceBehavior(InstanceContextMode= InstanceContextMode.Single)]

public class StudentService : IStudentService

{

List<Student> _Student = new List<Student>();

public void Addstudent(Student student)

{

student.Id = Guid.NewGuid().ToString();

_Student.Add(student);

}

public List<Student> GetAllstudents()

{

return_Student;

}

public void Removestudent(string id)

{

//拉姆达语句,谓词 p.Id == id

Studentstudent = _Student.Find(p => p.Id == id);

_Student.Remove(student);

}

}

}

编译一下

2、配置服务运行

到目前为至,我们建立好了WCF服务,那我们如何让WCFSVCHost(WCF服务主机)理解我们编写的服务类,并能够运行我们编写的服务呢。这需要我们在App.Config里面注册一下我们的WCF服务。

VS为我们提供了可视化的操作界面。
在Services项目中右击“App.Config”配置文件,在弹出的右键菜单中选择“编辑WCF配置”。

打开之后

在此界面中暴露两个对外的终结点(外部可以访问到的类或接口),其中下面一个是元数据终结点,用来向外提供服务信息的终结点。

而另一个(即上面的终结点),是向外公布我们编写的[ServiceContract]的类,但我们可以看到它的Contract还是我们在第一步中删掉的WcfServiceLibrary1.IService1这个终结点。

不仅如此,在右侧上面的黑字的服务中还依旧是我们在第一步中删除的WcfServiceLibrary1.Service1服务。这说明虽然在第一步中我们删除了那两个自动生成的类文件,但配置文件中仍没有删除这两个类文件的配置信息。

下面我们把它们改变一下。

单击左侧的“服务”-“WcfServiceLibrary1.Service1”在右侧的Name,弹出“服务类型浏览器”对话框,在此类型中我们找到此WCF服务项目编译出来的WcfServiceLibrary1.dll文件,双击它就可以出现此服务中的对外公布的服务,点击选中它单击确定。

这样我们就可以把对外公司的服务改变为我们刚编写的服务了。
然后,我们展开左侧“服务”->“WcfServiceLibrary1.StudentService”->“终结点”,单击第一个“空名称”,从右边的“终结点属性”中的Contract中我们可以看到,这里的Contract仍然用的是WcfServiceLibrary1.IService1。

我们按照上面的做法,找到此WCF服务项目编译出来的WcfServiceLibrary1.dll,双击它找到里面对应的ServiceContract点击确定就可以了。

在高级目录树中,为服务行为配置命名,名字你自己决定。

在服务中选中,刚才的行为配置。

重点一定要记着保存,点击菜单“文件”-“保存”就可以把我们对App.Config的修改保存回配置文件了。

<?xml version="1.0" encoding="utf-8" ?>

<configuration>

<system.web>

<compilation debug="true" />

</system.web>

<!-- 部署服务库项目时,必须将配置文件的内容添加到

主机的 app.config文件中。System.Configuration不支持库的配置文件。-->

<system.serviceModel>

<services>

<service behaviorConfiguration="Service.Myservice"name="WcfServiceLibrary1.StudentService">

<endpoint address="" binding="wsHttpBinding" contract="WcfServiceLibrary1.IStudentService">

<identity>

<dns value="localhost"/>

</identity>

</endpoint>

<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>

<host>

<baseAddresses>

<add baseAddress="http://localhost:8732/Design_Time_Addresses/WcfServiceLibrary1/Service1/"/>

</baseAddresses>

</host>

</service>

</services>

<behaviors>

<serviceBehaviors>

<behavior name="Service.Myservice">

<serviceMetadata httpGetEnabled="true" />

<serviceDebug includeExceptionDetailInFaults="false" />

</behavior>

</serviceBehaviors>

</behaviors>

</system.serviceModel>

3、测试WCF

在VisualStudio中为我们提供了测试WCF的工具,按F5启动WCF会出现两个东西
一个是在右下角的托盘图标中会出现WCFSVCHost(WCF服务主机),它为我们在开发时候提供了一个运行WCF的服务器,用来为测试客户端提供WCF服务。

另一个是“WCF测试客户端”

“测试客户端”从WcfSVCHost中取得WCF服务的元数据,解析为左侧的“服务结构树”,从这里面我们可以看到此WCF服务为我们提供了一个服务契约“IStudentService”,此服务契约中对外提供了三个可调用的方法。

点击AddStudent()方法输入参数点击Invoke调用

点击GetAllStudents(),在响应中我们看到了返回的结果。

在本例中我们看到,WCF作为面向对象和面向服务的桥梁,提供了非常方便的工具,无论是开发,配置还是测试,为我们可以快速的上手并提供面向服务的应用。你可以把WCF类库当作普通类库去做,但他提供了更强大的面向服务的特性。

WCF的理论学习复杂程度远大于其的使用难度,而如果你是一名初学者,千万不要先陷入其复杂的理论学习中,花费很多的时间,而且看得晕头转向,最好先去实践,先去用,这样再去看WCF的深入概念和技术才会在大脑里面形成更好理解的印象和对应。

附:

开发客户端来托管WCF服务库

新建控制台应用程序(winform也可)

添加引用

Program.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.ServiceModel;

using WcfServiceLibrary1;

using System.Runtime.InteropServices;

namespace ConsoleApplication1

{

class Program

{

static void Main(string[]args)

{

using(ServiceHost hostser = new ServiceHost(typeof(StudentService)))

{

Console.WriteLine("Service服务启动 ......");

hostser.Open();

Console.ReadLine();

}

}

}

}

然后项目右键-添加-新建项

把WCF服务库的配置文件覆盖到这个配置文件。

运行,

在浏览器打开配置文件的服务地址,就能看到效果了。