开发环境:vs2008英文版(SP1) + IIS + Windows2003
整个解决方案有4个项目
01.WCF ---Class Libary项目,用于生成WCF所需的类/接口文件
02.BLL ---Class LIbary项目,演示用的业务逻辑层(仅做分层演示用,无实际意义)
03.WEB ---Web Application,WCF服务将发布在这个项目中(即本例是把WCF宿主在IIS里)
04.Client--Console Application,命令行程序,用于演示调用WCF的客户端程序
项目引用关系:
01.WCF ---独立项目,无引用
02.BLL ---引用WCF,即业务逻辑层,引用Wcf
03.web ---引用BLL,即Web UI层引用BLL
04.Client --独立项目,无引用
步骤:
1.打开vs2008,File-->new project-->Visual C#/Windows-->Class Libary,命名为01_WCF
2.WCF项目上右击,Add-->New Item-->WCF Service ,命名为CalculateService.cs,确认后,系统会同时生成一个ICalculateService.cs的接口文件
ICalculateService.cs的内容如下(本例中,仅写了二个示例方案,Add与Sub,用于实现数字的加减):
Code
1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Runtime.Serialization;
5
using System.ServiceModel;
6
using System.Text;
7
8
namespace WCF
9

{
10
// NOTE: If you change the interface name "ICalculateService" here, you must also update the reference to "ICalculateService" in App.config.
11
[ServiceContract]
12
public interface ICalculateService
13
{
14
[OperationContract]
15
double Add(double x, double y);
16
17
[OperationContract]
18
double Sub(double x, double y);
19
}
20
}
这里可以看出,除了类前面加了[ServiceContract],以及方法签名前加了[OperationContract],其它跟普通的接口文件完全一样。这部分也称为WCF的契约
再来看CalculateService.cs,即实现契约的部分
Code
1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Runtime.Serialization;
5
using System.ServiceModel;
6
using System.Text;
7
8
namespace WCF
9

{
10
// NOTE: If you change the class name "CalculateService" here, you must also update the reference to "CalculateService" in App.config.
11
public class CalculateService : ICalculateService
12
{
13
public double Add(double x, double y)
14
{
15
return x + y;
16
}
17
18
public double Sub(double x, double y)
19
{
20
return x - y;
21
}
22
}
23
}
这个类实现了刚才的ICalculateService接口,其它与普通类文件完全一样
build一下,如果没错的话,wcf这个项目就算完工了
3.解决方案上右击,add-->new Project-->class Libary 命名为BLL,即业务逻辑层,然后在BLL项目的References上右击-->add References-->Projects-->选择01_WCF项目,完成对项目WCF的引用
4.把BLL中默认的Class1.cs删除,新建一个Test.Cs,内容如下:
Code
1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Text;
5
6
namespace BLL
7

{
8
public class Test
9
{
10
public string HelloWorld()
11
{
12
return "Hello World!";
13
}
14
15
public double Add(double x, double y)
16
{
17
return new WCF.CalculateService().Add(x, y);
18
}
19
}
20
}
这里仅做了一个示例,写了一个Add方法,用来调用WCF.CalculateService中的Add方法,到目前为止,可以看出,这跟普通项目的引用,以及普通类的引用没有任何区别,Build一下,如果没有问题的话,BLL项目也告一段落了
5.解决方案右击,add-->new project-->Asp.net Web Applicatin或Asp.net 3.5 Extenstions Web Application都可以,命名为03_WEB,同样添加对BLL项目的引用
6.在WEB项目中,新建一个WCF目录,然后在该目录下Add-->new Item-->WCF Service,命名为CalculateService.svc,添加后,这里有一个关键步骤,把WCF目录下,除CalculateService.svc以外的文件都删除,然后双击CalculateService.svc,修改内容为
<%@ ServiceHost Language="C#" Debug="true" Service="WCF.CalculateService" %>
因为实际上WCF的主要内容已经在WCF项目中实现了,所以这里只要标明Service="WCF.CalculateService"就可以了
,顺便给出web.config的一段关键配置
Code
1

2
<system.serviceModel>
3
<behaviors>
4
<serviceBehaviors>
5
<behavior name="WEB.DemoServiceBehavior">
6
<serviceMetadata httpGetEnabled="true"/>
7
<serviceDebug includeExceptionDetailInFaults="false"/>
8
</behavior>
9
</serviceBehaviors>
10
</behaviors>
11
<bindings>
12
<wsHttpBinding>
13
<binding name="WSHttpBinding_ICalculateService">
14
<security mode="None">
15
</security>
16
</binding>
17
</wsHttpBinding>
18
</bindings>
19
<services>
20
<service behaviorConfiguration="WEB.DemoServiceBehavior" name="WCF.CalculateService">
21
<endpoint address="wcf" binding="wsHttpBinding" contract="WCF.ICalculateService" bindingConfiguration="WSHttpBinding_ICalculateService" name="WCF.ICalculateService"/>
22
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
23
</service>
24
</services>
25
</system.serviceModel>
26
7.WCF在IIS里的配置
iis里新建一个站点,指向WEB项目所在的目录,端口在本例中设置为90,如果在浏览器直接浏览http://localhost:90/WCF/CalculateService.svc,表明IIS的环境没问题,如果不行,建议重新安装.net framework3.5(当然也有其它办法,就是增加svc后缀的映射,这个百度一下N多,就不重复了)
8.刚才的WEB项目里,应该还有一个Default.aspx的页面,这里我们简单示例一下调用BLL层代码(Default.aspx.cs内容)
Code
1
namespace WEB
2

{
3
public partial class _Default : System.Web.UI.Page
4
{
5
protected void Page_Load(object sender, EventArgs e)
6
{
7
//通过BLL层来调用WCF中的方法
8
BLL.Test _Test = new BLL.Test();
9
double z= _Test.Add(5, 10);
10
Response.Write(z.ToString());
11
}
12
}
13
}
编译浏览该页面,如果能显示15,表示ok了,Web项目完工
9.解决方案中,再添加一个Console Application,命名为04_Client,我们将在这个项目中,调用WEB中的WCF,注意要添加对System.ServiceModel的引用
10.关键步骤:浏览http://localhost:90/WCF/CalculateService.svc时,会发现页面上有一个提示:
若要测试此服务,需要创建一个客户端,并将其用于调用该服务。可以使用下列语法,从命令行中使用 svcutil.exe 工具来进行此操作:
svcutil.exe http://jimmycntvs:90/WCF/CalculateService.svc?wsdl (注:这里的JimmyCntvs就是我的机器名)
复制这一行命令,然后打开windows的开始菜单-->Microsoft Visual Studio 2008-->Visual Studio Tools-->Visual Studio 2008 Command Prompt进到vs2008的命令行
输入刚才的命令,并加一个参数/d:c:\123\ 即输出文件保存在c:\123目录中
svcutil.exe http://jimmycntvs:90/WCF/CalculateService.svc?wsdl /d:c:\123\
完成后,查看c:\123目录,会生成二个文件CalculateService.cs,output.config
把CalculateService.cs加到Client项目中,同时在Client项目中,增加一个App.Config,然后把output.Config的内容复制到App.Config中
这一步的目的在于利用svcutil.exe这个工具,生成客户端调用所需的代理类和配置文件
11.客户端代码的编写
Code
1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Text;
5
6
namespace ConsoleTest
7

{
8
class Program
9
{
10
static void Main(string[] args)
11
{
12
CalculateServiceClient _client = new CalculateServiceClient();
13
double x = 5, y = 10;
14
double z = _client.Add(x, y);
15
16
Console.WriteLine("{0} + {1} = {2}", x.ToString(), y.ToString(), z.ToString());
17
18
Console.ReadLine();
19
}
20
}
21
}
22
调用真的很简单吧,好了,总结一下:
本例中,先编写了一个简单的WCF服务,然后把它宿主在IIS中运行,然后用控制台程序来调用这个WCF.
当然WCF深入研究下去,远比这个复杂,这篇文章主要是为了消除大家对新技术的恐惧,快速上手WCF的使用,其实MS每次推出的新技术,听上去蛮吓人,用起来都很简单的.
(转载请注明来自"菩提树下的杨过")
点击此处下载源代码