您的位置:首页 > 其它

WCF ServiceHost的实例方式及ServiceHost实例的并发方式

2014-01-21 20:41 309 查看
一个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>

msdn上的解释:http://msdn.microsoft.com/en-us/library/system.servicemodel.description.servicethrottlingbehavior.maxconcurrentcalls(v=vs.100).aspx

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

http://stackoverflow.com/questions/7427100/can-i-call-multiple-operation-contracts-when-concurrencymode-is-single
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: