该 ServiceThrottlingBehavior 类公开可用于限制在应用程序级别创建实例或会话数的属性。 使用此行为,可以微调 Windows Communication Foundation (WCF) 应用程序的性能。
控制服务实例和并发调用
使用 MaxConcurrentCalls 属性来指定跨 ServiceHost 类主动处理的最大消息数,并使用 MaxConcurrentInstances 属性来指定服务中 InstanceContext 对象的最大数目。
由于这些属性的设置通常是在经过实际负载下运行应用程序的现实体验后进行的,因此通常在应用程序配置文件中使用 > 元素来指定这些属性的设置。
下面的代码示例演示如何使用 ServiceThrottlingBehavior 应用程序配置文件中的类,该配置文件将 MaxConcurrentSessions属性 MaxConcurrentCalls和 MaxConcurrentInstances 属性设置为 1 作为一个简单示例。 实际体验确定任何特定应用程序的最佳设置。
<configuration>
<appSettings>
<!-- use appSetting to configure base address provided by host -->
<add key="baseAddress" value="http://localhost:8080/ServiceMetadata" />
</appSettings>
<system.serviceModel>
<services>
<service
name="Microsoft.WCF.Documentation.SampleService"
behaviorConfiguration="Throttled" >
<host>
<baseAddresses>
<add baseAddress="http://localhost:8080/SampleService"/>
</baseAddresses>
</host>
<endpoint
address=""
binding="wsHttpBinding"
contract="Microsoft.WCF.Documentation.ISampleService"
/>
<endpoint
address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange"
/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="Throttled">
<serviceThrottling
maxConcurrentCalls="1"
maxConcurrentSessions="1"
maxConcurrentInstances="1"
/>
<serviceMetadata
httpGetEnabled="true"
httpGetUrl=""
/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
确切的运行时行为取决于ConcurrencyMode和InstanceContextMode属性的值,这些值分别控制一次操作中可以执行的消息数量,以及服务的生存期相对于传入通道会话的关系。
有关详细信息,请参阅 MaxConcurrentCalls和 MaxConcurrentInstances。