一個http請求到達WCF的流程圖
Computer1向IIS發http請求。IIS接到請求,解析url,找到Project1並實例對應的ServiceHost1,解析參數,處理請求。
ServiceHost的配置是在web.config中設置的。
下面介紹下ServiceHost的實例方式以及ServiceHost實例的併發方式。
1.ServiceHost的三種實例方式(InstanceContextMode屬性)
PerSession:默認值。以session形式在server端實例一個ServiceHost對象,在session有效時間內此ServiceHost對象處理同一client端發過來的所有請求。
PerCall: 接到一個請求就實例一個ServiceHost對象,該請求返回後,此ServiceHost對象銷燬。
Single:只實例一個ServiceHost對象,用於處理所有請求。
msdn上的解釋:http://msdn.microsoft.com/en-us/library/system.servicemodel.instancecontextmode(v=vs.100).aspx
ServiceHost實例的上限取決於每個ServiceHost的maxConcurrentCalls、maxConcurrentSessions和maxConcurrentInstances的設置。
maxConcurrentCalls:以perCall形式實例的ServiceHost對象的數量受限於此屬性。maxConcurrentCalls 的默認值爲cpu數量*16。
maxConcurrentSessions:以perSession形式實例的ServiceHost對象的數量受限於此屬性。maxConcurrentSessions的默認值爲cpu數量*100。
(注:此處的cpu數量,虛擬機裏的虛擬cpu同樣適用)
maxConcurrentInstances:每個實例的ServiceHost對象稱爲Instance。 maxConcurrentInstances = maxConcurrentCalls + maxConcurrentSessions。
這三個屬性可以在web.config的ServiceBehaviors標籤下爲每個ServiceHost設置。
<behavior name="xxx ">
<serviceThrottling maxConcurrentCalls="100" maxConcurrentSessions="100" maxConcurrentInstances="200" />
</behavior>
2.ServiceHost實例的三種併發方式(ConcurrencyMode 屬性)
Single:默認值。ServiceHost實例的對象是單線程的,串行處理請求。
Multiple:ServiceHost實例的對象是多線程的,並行處理請求。
Reentrant:見msdn解釋。
msdn上的解釋:http://msdn.microsoft.com/zh-cn/library/system.servicemodel.concurrencymode(v=vs.100).aspx
參考文章
http://www.codeproject.com/Articles/89858/WCF-Concurrency-Single-Multiple-and-Reentrant-and
http://www.codeproject.com/Articles/86007/3-ways-to-do-WCF-instance-management-Per-call-Per
(以上兩篇文章作者對ConcurrencyMode概念理解有誤,請留意)
http://msdn.microsoft.com/en-us/library/ms731193.aspx
http://stackoverflow.com/questions/6065379/understanding-wcf-servicebehaviorproperty-concurrencymode