如何提高WCF的执行效率
1个回答
展开全部
下面是我的各处配置: <bindings>
<customBinding>
<binding name="myMexTcpBinding">
<binaryMessageEncoding>
<readerQuotas maxDepth="32" maxStringContentLength="819200" maxArrayLength="1638400" maxBytesPerRead="409600" maxNameTableCharCount="1638400"/>
</binaryMessageEncoding>
<tcpTransport maxBufferPoolSize="524288000" maxReceivedMessageSize="65536000" maxBufferSize="65536000"/>
</binding>
<binding name="mybinding">
<binaryMessageEncoding>
<readerQuotas maxDepth="32" maxStringContentLength="819200" maxArrayLength="1638400" maxBytesPerRead="409600" maxNameTableCharCount="1638400"/>
</binaryMessageEncoding>
<tcpTransport maxBufferPoolSize="524288000" maxReceivedMessageSize="65536000" maxBufferSize="65536000"/>
</binding>
</customBinding>
</bindings>
<service behaviorConfiguration="MyBehavior" name="Services.BaseDataManage.BaseDataManage">
<endpoint address="" binding="customBinding" bindingConfiguration="mybinding" contract="Services.BaseDataManage.IBaseDataManage">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<endpoint address="mex" binding="customBinding" bindingConfiguration="myMexTcpBinding" contract="IMetadataExchange"/>
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:8802/BaseDataManage/"/>
</baseAddresses>
</host>
</service>
<behavior name="MyBehavior">
<serviceMetadata httpGetEnabled="false"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
<serviceTimeouts transactionTimeout="23:59:59"/>
<serviceThrottling maxConcurrentCalls="1000" maxConcurrentSessions="1000" maxConcurrentInstances="1000"/>
</behavior>
[ServiceContract(CallbackContract=typeof(IBaseDataCallBack))]
public partial interface IBaseDataManage
[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple, InstanceContextMode = InstanceContextMode.PerCall)]
public partial class BaseDataManage : IBaseDataManage
wcfbindingbehavior
性能不好的原因很多:
1、事务:事务是需要同步上下文和会话,因此发生阻塞,若达到会话连接最高值,之后的会话需排队甚至超时;解决:将支持事务与非事务的服务分开成不同的服务;
2、回调:回调需要采用独立线程或线程池执行回调操作,否则可能在回调时因掉线、长时间运算、超时等因素造成会话一直占用或长时间占用,尽可能在设计上让服务IsOneWay=true;
3、在服务器资源允许的情况下:采用InstanceContextMode.PerCall,UseSynchronizationContext=false,ConcurrencyMode = ConcurrencyMode.Multiple,这种能最大的并发应用。
4、吞吐量配置,我不知道配置文件如何配,我自己是写了一个ServiceThrottlingAttribute,继承Attribute和IServiceBehavior,最后配置最大连接数 MaxConcurrentSessions (WCF默认为10),上下文实例数 MaxConcurrentInstances,最大待处理消息数或回调数,MaxConcurrentCalls(WCF默认64),
5、以上仅是我配置过的,可能有些解释不正确,但通过这样的改动后,并发和性能都得到很大的提升,由于我这里是工作流服务,目前最高有3000-5000工作流在同时运行,并没有发生明显的阻塞。
<customBinding>
<binding name="myMexTcpBinding">
<binaryMessageEncoding>
<readerQuotas maxDepth="32" maxStringContentLength="819200" maxArrayLength="1638400" maxBytesPerRead="409600" maxNameTableCharCount="1638400"/>
</binaryMessageEncoding>
<tcpTransport maxBufferPoolSize="524288000" maxReceivedMessageSize="65536000" maxBufferSize="65536000"/>
</binding>
<binding name="mybinding">
<binaryMessageEncoding>
<readerQuotas maxDepth="32" maxStringContentLength="819200" maxArrayLength="1638400" maxBytesPerRead="409600" maxNameTableCharCount="1638400"/>
</binaryMessageEncoding>
<tcpTransport maxBufferPoolSize="524288000" maxReceivedMessageSize="65536000" maxBufferSize="65536000"/>
</binding>
</customBinding>
</bindings>
<service behaviorConfiguration="MyBehavior" name="Services.BaseDataManage.BaseDataManage">
<endpoint address="" binding="customBinding" bindingConfiguration="mybinding" contract="Services.BaseDataManage.IBaseDataManage">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<endpoint address="mex" binding="customBinding" bindingConfiguration="myMexTcpBinding" contract="IMetadataExchange"/>
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:8802/BaseDataManage/"/>
</baseAddresses>
</host>
</service>
<behavior name="MyBehavior">
<serviceMetadata httpGetEnabled="false"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
<serviceTimeouts transactionTimeout="23:59:59"/>
<serviceThrottling maxConcurrentCalls="1000" maxConcurrentSessions="1000" maxConcurrentInstances="1000"/>
</behavior>
[ServiceContract(CallbackContract=typeof(IBaseDataCallBack))]
public partial interface IBaseDataManage
[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple, InstanceContextMode = InstanceContextMode.PerCall)]
public partial class BaseDataManage : IBaseDataManage
wcfbindingbehavior
性能不好的原因很多:
1、事务:事务是需要同步上下文和会话,因此发生阻塞,若达到会话连接最高值,之后的会话需排队甚至超时;解决:将支持事务与非事务的服务分开成不同的服务;
2、回调:回调需要采用独立线程或线程池执行回调操作,否则可能在回调时因掉线、长时间运算、超时等因素造成会话一直占用或长时间占用,尽可能在设计上让服务IsOneWay=true;
3、在服务器资源允许的情况下:采用InstanceContextMode.PerCall,UseSynchronizationContext=false,ConcurrencyMode = ConcurrencyMode.Multiple,这种能最大的并发应用。
4、吞吐量配置,我不知道配置文件如何配,我自己是写了一个ServiceThrottlingAttribute,继承Attribute和IServiceBehavior,最后配置最大连接数 MaxConcurrentSessions (WCF默认为10),上下文实例数 MaxConcurrentInstances,最大待处理消息数或回调数,MaxConcurrentCalls(WCF默认64),
5、以上仅是我配置过的,可能有些解释不正确,但通过这样的改动后,并发和性能都得到很大的提升,由于我这里是工作流服务,目前最高有3000-5000工作流在同时运行,并没有发生明显的阻塞。
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询