WCF單例服務(wù)中可擴展性認(rèn)識
WCF開發(fā)工具的出現(xiàn),為開發(fā)人員帶來了非常大的好處。我們今天將會通過這篇文章中介紹的內(nèi)容為大家充分講解一下關(guān)于WCF單例服務(wù)的基本內(nèi)容,從而使朋友們可以加深對WCF工具的應(yīng)用。#t#
對并發(fā)服務(wù)調(diào)用請求的處理是WCF最基本要求,為了提供服務(wù)的響應(yīng)能力,WCF會在不同的線程中處理并發(fā)請求。在單例模式下,服務(wù)實例是唯一的,也就是說相同的服務(wù)實例會同時被多個線程并發(fā)地訪問。在默認(rèn)的情況下,多個線程以同步的方式訪問WCF單例服務(wù)對象,也就是說,在某個時刻,最多只會有一個線程在使用服務(wù)實例。如果一個服務(wù)操作需要1秒,那么在一分鐘內(nèi)最多只能處理60個服務(wù)調(diào)用請求。倘若客戶端采用默認(rèn)的超時時限(1分鐘),對于60個并發(fā)地服務(wù)調(diào)用請求,至少會有一個服務(wù)調(diào)用會失敗。這極大地降低了WCF服務(wù)的可擴展性、響應(yīng)能力和可用性。
為了讓讀者對WCF單例服務(wù)的低可擴展性有一個深刻的認(rèn)識,我寫了一個極端的案例。從這個案例演示中,讀者會清晰地認(rèn)識到提供一個相同的功能,采用單調(diào)模式和單例模式,對客戶端影響的差別有多大。本案例同樣沿用計算服務(wù)的例子,Add方法中通過使線程休眠5秒模擬一個耗時的服務(wù)操作,下面是服務(wù)的定義,采用單調(diào)實例上下文模式。
- [ServiceBehavior(InstanceContextModeInstanceContextMode =
InstanceContextMode.PerCall)]- public class CalculatorService : ICalculator
- {
- public double Add(double x, double y)
- {
- Thread.Sleep(5000);
- return x + y;
- }
- }
在客戶端,通過ThreadPool模擬5個并發(fā)的客戶端,在Add操作調(diào)用成功后輸出當(dāng)前的時間,從而檢驗服務(wù)的響應(yīng)能力。
- for (int i = 0; i <
- i++)
- {
- ThreadPool.QueueUserWorkItem(delegate
- {
- using (ChannelFactory<ICalculator> channelFactory =
new ChannelFactory<ICalculator>("calculatorservice"))- {
- ICalculator calculator = channelFactory.CreateChannel();
- Console.WriteLine("{3}: x + y = {2} when x = {0} and y = {1}",
1, 2, calculator.Add(1, 2), DateTime.Now); }- });
- }
從客戶端輸出結(jié)果我們可以看出,對于5個并發(fā)的服務(wù)調(diào)用均得到了及時的相應(yīng),這是我們希望看到的結(jié)果。
- 3/8/2009 08:03:17 : x + y = 3 when x = 1 and y = 2
- 3/8/2009 08:03:17 : x + y = 3 when x = 1 and y = 2
- 3/8/2009 08:03:17 : x + y = 3 when x = 1 and y = 2
- 3/8/2009 08:03:18 : x + y = 3 when x = 1 and y = 2
- 3/8/2009 08:03:18 : x + y = 3 when x = 1 and y = 2
但是,如果將實例上下文模式換成是InstanceContextMode.Single,情況就完全不一樣了。從最終的輸出結(jié)果可以看出,客戶端得到執(zhí)行結(jié)果的間隔為5s,由此可知服務(wù)操作在服務(wù)端是以同步的方式執(zhí)行的。
- [ServiceBehavior(InstanceContextModeInstanceContextMode =
InstanceContextMode.Single)]- public class CalculatorService : ICalculator, IDisposable
- {
- //省略實現(xiàn)
- }
輸出結(jié)果:
- 3/8/2009 08:03:25 : x + y = 3 when x = 1 and y = 2
- 3/8/2009 08:03:30 : x + y = 3 when x = 1 and y = 2
- 3/8/2009 08:03:35 : x + y = 3 when x = 1 and y = 2
- 3/8/2009 08:03:40 : x + y = 3 when x = 1 and y = 2
- 3/8/2009 08:03:45 : x + y = 3 when x = 1 and y = 2
WCF通過并發(fā)模式(Concurrency Mode)表示多線程訪問WCF單例服務(wù)對象的方式,而并發(fā)模式作為一種服務(wù)行為可以通過ServiceBehaviorAttribute特性進行設(shè)定。WCF通過ConcurrencyMode枚舉來表示不同形式的并發(fā)模式,三個枚舉值Single、Reentrant和Multiple分別表示單線程、重入和多線程三種并發(fā)模式。關(guān)于并發(fā)和并發(fā)模式,將在本書的下一卷予以詳細(xì)講解,在這里就不再作重復(fù)介紹了。ConcurrencyMode在ServiceBehaviorAttribute的定義如下:
- [AttributeUsage(AttributeTargets.Class)]
- public sealed class ServiceBehaviorAttribute :
Attribute, IServiceBehavior- {
- //其他成員
- public ConcurrencyMode ConcurrencyMode { get; set; }
- }
- public enum ConcurrencyMode
- {
- Single,
- Reentrant,
- Multiple
- }
ConcurrencyMode.Single是默認(rèn)采用的并發(fā)模式,這正是上面的例子中服務(wù)操作同步執(zhí)行的根本原因。為了讓服務(wù)操作異步地執(zhí)行,從未提供服務(wù)的響應(yīng)能力,我們只須要通過ServiceBehaviorAttribute將并發(fā)模式設(shè)為ConcurrencyMode.Multiple就可以了。
- [ServiceBehavior(InstanceContextModeInstanceContextMode =
InstanceContextMode.Single, ConcurrencyModeConcurrencyMode =
ConcurrencyMode.Multiple)]- public class CalculatorService : ICalculator, IDisposable
- {
- //省略實現(xiàn)
- }
輸出結(jié)果:
- 3/8/2009 08:05:05 : x + y = 3 when x = 1 and y = 2
- 3/8/2009 08:05:05 : x + y = 3 when x = 1 and y = 2
- 3/8/2009 08:05:05 : x + y = 3 when x = 1 and y = 2
- 3/8/2009 08:05:05 : x + y = 3 when x = 1 and y = 2
- 3/8/2009 08:05:06 : x + y = 3 when x = 1 and y = 2
如果將并發(fā)模式設(shè)為ConcurrencyMode.Multiple,意味著同一個服務(wù)實例在多個線程中被并發(fā)執(zhí)行。當(dāng)我們操作一些數(shù)據(jù)的時候,須要根據(jù)具體的情況考慮是否要采用一些加鎖機制來確保狀態(tài)的同步性。
WCF單例服務(wù)的基本概念就為大家介紹到這里。