可靠会话的最佳做法Best Practices for Reliable Sessions

本主题讨论可靠会话的最佳实践。This topic discusses best practices for reliable sessions.

设置 MaxTransferWindowSizeSetting MaxTransferWindowSize

Windows Communication Foundation (WCF) 中的可靠会话使用传输窗口保存客户端和服务上的消息。Reliable sessions in Windows Communication Foundation (WCF) use a transfer window to hold messages on the client and service. 可配置属性 MaxTransferWindowSize 指示传输窗口可以保存多少条消息。The configurable property MaxTransferWindowSize indicates how many messages the transfer window can hold.

在发件人,这指示等待确认; 时传输窗口可以保存多少条消息在接收方,它指示要为服务缓冲的消息数。On the sender, this indicates how many messages the transfer window can hold while waiting for acknowledgements; on the receiver, it indicates how many messages to buffer for the service.

选择合适的大小会影响网络的效率和服务的最佳容量。Choosing the right size impacts the efficiency of the network and the optimal capacity of the service. 以下各节将详细介绍选择此属性和值的影响的值时要考虑。The following sections detail what to consider when choosing a value for this property and the impact of the value.

默认传输窗口大小为 8 条消息。The default transfer window size is eight messages.

有效地利用网络Efficient use of the network

在此上下文中,术语网络对应于所有内容用作客户端 (发送方) 和服务 (接收方) 之间的通信的基础。In this context, the term network corresponds to everything used as the basis of communication between a client (sender) and a service (receiver). 这包括传输连接和任何中间站点或在之间架起桥梁包括 SOAP 路由器或 HTTP 代理/防火墙。This includes the transport connections and any intermediary or bridges in between, including SOAP routers or HTTP proxies/firewalls.

有效使用网络可确保充分利用网络容量。Efficient use of the network ensures that network capacity is fully used. 可以每秒通过网络传输的数据量 (数据速率) 并将数据从发送方到接收方传输所花费的时间 (延迟) 如何有效地影响网络则可利用。Both the amount of data that can be transferred per second over the network (data rate) and the time it takes to transfer data from the sender to the receiver (latency) impact how effectively the network is utilized.

在发送方,属性 MaxTransferWindowSize 指示在等待确认消息时,其传输窗口可以保存多少条消息。On the sender, the property MaxTransferWindowSize indicates how many messages its transfer window can hold while waiting for acknowledgements. 如果网络延迟较高,并且为了确保及时响应发送者和网络有效利用,应增加传输窗口大小。If the network latency is high and in order to ensure a responsive sender and effective network utilization, you should increase the transfer window size.

例如即使发件人不断变化的数据速率,延迟时间会很高如果发送者和接收者之间存在多个中介或者数据都必须通过率有损中介或网络。For example even if the sender keeps up with data rate, latency could be high if several intermediaries exist between the sender and receiver or the data must pass through a lossy intermediary or network. 因此,发件人必须接受新消息在网络上发送之前,等待其传输窗口中消息的确认信息。Thus, the sender has to wait for acknowledgements for the messages in its transfer window before accepting new messages to send on the wire. 具有高延迟、 较少的有效的小缓冲区网络利用率。The smaller the buffer with high latency, the less effective the network utilization. 另一方面,过高的传输窗口大小可能会影响服务,因为该服务可能需要几乎与客户端发送的数据的速率很高。On the other hand, too high a transfer window size may impact the service because the service may need to catch up to the high rate of data sent by the client.

与容量运行服务Running the service to capacity

尽可能有效地使用网络,理想情况下还想要在最佳容量运行的服务。As much as the network is used efficiently, ideally you also want the service to run at optimal capacity. 接收方上的传输窗口大小属性指示接收方可以缓冲多少条消息。The transfer window size property on the receiver indicates how many messages the receiver can buffer. 此消息缓冲不仅帮助网络流控制,而且还可让服务满负荷运行。This message buffering helps not only the network flow control but also enables the service to run to full capacity. 例如,如果缓冲区是一条消息,消息到达的速度超过了该服务可以处理它们,然后在网络可能会丢弃一些消息,并可能浪费或闲置容量。For example if the buffer is one message and messages arrive faster than the service can process them, then the network might drop messages and capacity might be wasted or underutilized.

使用缓冲区可提高服务的可用性,因为它同时接收并处理以前接收的消息时缓冲消息。Using a buffer increases the availability of the service as it concurrently receives and buffers a message while processing the previously received messages.

我们建议你使用相同MaxTransferWindowSize发送方和接收方。We recommended that you use the same MaxTransferWindowSize on both the sender and receiver.

启用流控制Enabling flow control

流控制是一种机制,可确保发送方和接收方跟上节奏彼此,消息是使用和处理,即快速它们正在生成。Flow control is a mechanism that ensures that the sender and receiver keep pace with each other, that is, the messages are consumed and acted upon as fast as they're produced. 客户端和服务上的传输窗口大小可确保发送方和接收方在一个合理的同步窗口中。The transfer window size on the client and service ensures that the sender and receiver are within a reasonable window of synchronization.

我们强烈建议将属性设置FlowControlEnabledtrue当使用 WCF 客户端和 WCF 服务之间可靠会话。We highly recommended that you set the property FlowControlEnabled to true when you're using a reliable session between a WCF client and a WCF service.

设置 MaxPendingChannelsSetting MaxPendingChannels

在编写一个服务,使来自不同客户端的可靠会话通信,就可以有许多客户端创建在同一时间对服务的可靠会话。When writing a service that enables reliable session communication from different clients, it's possible to have many clients establish a reliable session to the service at the same time. 在这些情况下,服务的响应取决于 MaxPendingChannels 属性。The response of the service in these situations depends on the MaxPendingChannels property.

当发送方创建到接收方的可靠会话通道时,发送方和接收方之间的握手将建立可靠会话。When the sender creates a reliable session channel to a receiver, a handshake between them establishes a reliable session. 建立可靠会话之后,该通道会放入到挂起的通道队列中以供服务接受。After the reliable session is established, the channel is put in a pending channel queue for acceptance by the service. MaxPendingChannels 属性指示有多少个通道可以处于此状态。The MaxPendingChannels property indicates how many channels can be in this state.

很可能要在其中它不能接受更多通道状态的服务。It's possible for the service to be in a state where it can't accept more channels. 如果队列已满时,在尝试建立可靠会话被拒绝,并在客户端必须重试。If the queue is full, an attempt to establish a reliable session is rejected, and the client must retry.

还有可能更长的时间在队列中挂起的通道保留在队列中。It's also possible that the pending channels in the queue remain in the queue for a longer duration. 在此期间,可能出现可靠会话的非活动超时,从而导致通道转换到出错状态。In the meantime, an inactivity timeout on the reliable session may occur, causing the channel to transition to a faulted state.

在编写同时服务多个客户端的服务时,应设置一个值,适用于你的需求。When writing a service that services multiple clients simultaneously, you should set a value that's suitable for your needs. 设置过高的值MaxPendingChannels属性会影响您的工作集。Setting too high a value for the MaxPendingChannels property impacts your working set.

默认值为MaxPendingChannels是四个通道。The default value for MaxPendingChannels is four channels.

可靠会话和宿主Reliable sessions and hosting

Web 承载服务使用可靠会话时,您应记住以下重要注意事项:When web hosting a service that uses reliable sessions, you should keep the following important considerations in mind:

  • 可靠会话是有状态的,而状态在 AppDomain 中进行维护。Reliable sessions are stateful, and state is maintained in the AppDomain. 这意味着,属于可靠会话的一部分的所有消息必须在同一个 AppDomain 中进行处理。This means that all messages that are part of a reliable session must be processed in the same AppDomain. 场或园的大小大于一个节点的 web 场和网络园无法保证此约束。Web farms and web gardens where the size of the farm or garden is greater than one node can't guarantee this constraint.

  • 可靠会话使用双工 HTTP 通道 (例如,使用WsDualHttpBinding) 会要求多于默认值为两个 HTTP 连接每个客户端。Reliable sessions using dual HTTP channels (for example, using WsDualHttpBinding) can require more than the default of two HTTP connections per-client. 这意味着,双工可靠会话可能需要最多两个连接每一种方式,因为并发应用程序和协议消息可能会传输每一种方式在任意给定时间。This means a duplex reliable session can require up to two connections each way because concurrent application and protocol messages may be transferring each way at any given time. 某些情况下根据该服务的消息交换模式,这意味着就可能发生死锁 web 承载的服务使用双工 HTTP 和可靠会话。Under certain conditions depending on the message exchange pattern of the service, this means that it's possible to deadlock a web-hosted service using dual HTTP and reliable sessions. 若要增加每个客户端允许的 HTTP 连接数,将以下代码添加到相关的配置文件 (例如, web.config相关服务造成的):To increase the number of allowable HTTP connections per client, add the following to the relevant configuration file (for example, web.config of the service in question):

    <configuration>
      <system.net>
        <connectionManagement>
          <add name="*" maxconnection="4" />
        </connectionManagement>
      </system.net>
    </configuration>
    

    maxconnection属性是所需的连接数。The value of the maxconnection attribute is the number of connections needed. 在这种情况下,最小值应为四个连接。The minimum in this case should be four connections.