天天看點

WCF入門轉

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>