設定繫結上的逾時值Configuring Timeout Values on a Binding

WCF 繫結有一些逾時設定可供使用。There are a number of timeout settings available in WCF bindings. 正確設定這些逾時設定可以改善服務的效能,但也會對服務的可用性和安全性造成影響。Setting these timeout settings correctly can improve not only your service’s performance but also play a role in the usability and security of your service. 可在 WCF 繫結上使用的逾時如下:The following timeouts are available on WCF bindings:

  1. OpenTimeoutOpenTimeout

  2. CloseTimeoutCloseTimeout

  3. SendTimeoutSendTimeout

  4. ReceiveTimeoutReceiveTimeout

WCF 繫結逾時WCF Binding Timeouts

本主題中討論的每個設定,不論在程式碼還是組態中,都是對繫結本身進行設定。Each of the settings discussed in this topic are made on the binding itself, either in code or configuration. 下列程式碼示範如何在自我裝載服務的內容中,以程式設計方式對 WCF 繫結設定逾時。The following code shows how to programmatically set timeouts on a WCF binding in the context of a self-hosted service.

public static void Main()
{
    Uri baseAddress = new Uri("http://localhost/MyServer/MyService");

    try
    {
        ServiceHost serviceHost = new ServiceHost(typeof(CalculatorService));

        WSHttpBinding binding = new WSHttpBinding();
        binding.OpenTimeout = new TimeSpan(0, 10, 0);
        binding.CloseTimeout = new TimeSpan(0, 10, 0);
        binding.SendTimeout = new TimeSpan(0, 10, 0);
        binding.ReceiveTimeout = new TimeSpan(0, 10, 0);

        serviceHost.AddServiceEndpoint("ICalculator", binding, baseAddress);
        serviceHost.Open();

        // The service can now be accessed.
        Console.WriteLine("The service is ready.");
        Console.WriteLine("Press <ENTER> to terminate service.");
        Console.WriteLine();
        Console.ReadLine();
    }
    catch (CommunicationException ex)
    {
        // Handle exception ...
    }
}

下列範例示範如何在應用程式組態檔中設定繫結上的逾時。The following example shows how to configure timeouts on a binding in a configuration file.

<configuration>
  <system.serviceModel>
    <bindings>
      <wsHttpBinding>
        <binding openTimeout="00:10:00"
                 closeTimeout="00:10:00"
                 sendTimeout="00:10:00"
                 receiveTimeout="00:10:00">
        </binding>
      </wsHttpBinding>
    </bindings>
  </system.serviceModel>
</configuration>

這些設定的其他資訊可以在 Binding 類別的文件中找到。More information about these settings can be found in the documentation for the Binding class.

用戶端逾時Client-side Timeouts

在用戶端:On the client side:

  1. SendTimeout – 用來初始化 OperationTimeout,這會控制整個傳送訊息程序,包括接收要求/回覆服務作業的回覆訊息。SendTimeout – used to initialize the OperationTimeout, which governs the whole process of sending a message, including receiving a reply message for a request/reply service operation. 從合約回呼方法傳送回覆郵件時,也適用這個逾時。This timeout also applies when sending reply messages from a callback contract method.

  2. OpenTimeout –在未指定明確的超時值時,用來開啟通道。OpenTimeout – used when opening channels when no explicit timeout value is specified.

  3. CloseTimeout –在未指定明確的超時值時,用於關閉通道。CloseTimeout – used when closing channels when no explicit timeout value is specified.

  4. ReceiveTimeout –不會使用。ReceiveTimeout – is not used.

服務端超時Service-side Timeouts

在服務端:On the service side:

  1. SendTimeout、OpenTimeout、CloseTimeout 與用戶端上的相同。SendTimeout, OpenTimeout, CloseTimeout are the same as on the client.

  2. ReceiveTimeout – 由服務架構層用來初始化工作階段閒置逾時,這會控制工作階段在逾時前可以處於閒置狀態多久。ReceiveTimeout – used by the Service Framework Layer to initialize the session-idle timeout which controls how long a session can be idle before timing out.