WCF架构入门-用VS2008构建WCF

    根据微软官方的解释,WCF(之前的版本名为“Indigo”)是使用托管代码建立和运行面向服务(Service Oriented)应用程序的统一框架。它使得开发者能够建立一个跨平台的安全、可信赖、事务性的解决方案,且能与已有系统兼容协作。WCF是微软分布式应用程序开发的集大成者,它整合了.Net平台下所有的和分布式系统有关的技术,例如.Net Remoting、ASMX、WSE和MSMQ。以通信(Communiation)范围而论,它可以跨进程、跨机器、跨子网、企业网乃至于 Internet;以宿主程序而论,可以以ASP.NET,EXE,WPF,Windows Forms,NT Service,COM+作为宿主(Host)。WCF可以支持的协议包括TCP,HTTP,跨进程以及自定义,安全模式则包括SAML, Kerberos,X509,用户/密码,自定义等多种标准与模式。也就是说,在WCF框架下,开发基于SOA的分布式系统变得容易了,微软将所有与此相关的技术要素都包含在内,掌握了WCF,就相当于掌握了叩开SOA大门的钥匙。

   本文就是要通过一个简单的例子,介绍WCF架构的搭建方法和步骤。本文使用的开发工具是VS2008。

  WCF架构包括三大部分:服务、宿主进程和客户端。其中服务和宿主进程属于服务端。

一、服务(Service)

  服务主要包括契约和服务的实现。

1.1 契约

  WCF的所有服务必须公开契约。契约是与平台无关的,描述服务功能的标准方式。主要有服务契约、数据契约、消息契约和错误契约。

  本文着重介绍一下服务契约的定义。我们所提供的服务是一个加法计算(Add)。首先我们定义契约。


​​view plain​​​
​​​copy to clipboard​​​
​​​print​​​
1. using System;
2.
3. using System.Collections.Generic;
4.
5. using System.Linq;
6.
7. using System.Text;
8.
9. using System.ServiceModel;
10.
11. namespace manual_WCF
12.
13. {
14.
15. "#339900">ServiceContract</font>]
16.
17. interface IMyProcess
18.
19. {
20.
21. "#339900">OperationContract</font>]
22.
23. int Add(int a, int b);
24.
25. }
26.
27. }

  我们可以将[ServiceContract]属性应用到接口或者类上,但是通常我们将其应用于接口上,而不是类上。

  另外,即使接口应用了[ServiceContract]属性,并不意味它的所有成员都是契约的一部分。我们必须使用[OperationContract]属性显式地标注哪些方法需要暴露为WCF契约的一部分。

1.2 服务的实现

  服务实现,其实就是对契约(接口)的实现。


​​view plain​​​
​​​copy to clipboard​​​
​​​print​​​
​​​?​​

1. <font color="#000000"><pre class="csharp" name="code"><strong>using System;
2.
3. using System.Collections.Generic;
4.
5. using System.Linq;
6.
7. using System.Text;
8.
9.
10.
11. namespace manual_WCF
12.
13. {
14.
15. class MyProcess:IMyProcess
16.
17. {
18.
19. public int Add(int a, int b)
20.
21. {
22.
23. "Received Add({0},{1}) Returnning:{2}",a,b,a+b);
24.
25. return a + b;
26.
27. }
28.
29. }
30.
31. }</strong></pre>
32. </font>


二、宿主进程(Host)

WCF服务不可能凭空存在。每个WCF服务必须托管(Hosting)在Windows进程中,该进程就称为宿主进程(Host Process)。宿主可以由IIS提供,也可以有WindowsForm程序或者Console提供,也可以由Windows服务提供。



​​view plain​​​
​​​copy to clipboard​​​
​​​print​​​

1. <font color="#000000"><strong>using System;
2.
3. using System.ServiceModel;
4.
5. using System.ServiceModel.Description;
6.
7. using System.Net;
8.
9.
10.
11. namespace manual_WCF
12.
13. {
14.
15. class Program
16.
17. {
18.
19. static void Main(string[] args)
20.
21. {
22.
23. new Uri("http://localhost:8001/");
24.
25.
26.
27. new ServiceHost(typeof(MyProcess), baseAddress);
28.
29.
30.
31. typeof(IMyProcess),new BasicHttpBinding(),"manualWCF");
32.
33.
34.
35. new ServiceMetadataBehavior();
36.
37. true;
38.
39. Host.Description.Behaviors.Add(smb);
40.
41.
42.
43. Host.Open();
44.
45.
46.
47.
48.
49. "The manual WCF is running at " + baseAddress.ToString()+"manualWCF");
50.
51.
52.
53. "Press <ENTER> to terminate");
54.
55. Console.ReadLine();
56.
57.
58.
59. Host.Close();
60.
61.
62.
63. }
64.
65. }
66.
67. }</strong></font>



​view plain​​​ ​​​copy to clipboard​​​ ​​​print​​​ ​​​?​



  1. <font color="#000000"> </font><font color="#3366ff"><strong>三、客户端(Client)</strong></font>


  若要调用WCF服务的操作,客户端需要首先导入服务契约到客户端本地描述(Native Representation)中。如果客户端使用WCF,调用操作的常见做法是使用代理。代理是一个CLR类,它公开了一个单独的CLR接口用以表示服务的契约。代理完全封装了服务的每一个方面:服务的位置、实现技术、运行时平台以及通信传输。

3.1 生成代理

​SvcUtil.exe​

svcutil.exe /language:c# ​​http://localhost:8001​

  运行以上命令会生成两个文件MyProcess.cs和output.config。将这两个文件导入到客户端工程中(将output.config改名为app.config)。

3.2 调用服务操作

  此时,就可以像使用本地方法一样使用WCF服务中的操作了


​​view plain​​​
​​​copy to clipboard​​​
​​​print​​​

1. <font color="#000000">using System;
2.
3. using System.Collections.Generic;
4.
5. using System.Linq;
6.
7. using System.Text;
8.
9.
10.
11. namespace Client_manual
12.
13. {
14.
15. class Program
16.
17. {
18.
19. static void Main(string[] args)
20.
21. {
22.
23. new MyProcessClient();
24.
25. Console.WriteLine(client.Add(3,5));
26.
27. Console.ReadLine();
28.
29. }
30.
31. }
32.
33. }</font>

WCF入门转_wcf