title | description | ms.date | ms.topic |
---|---|---|---|
Configuring Timeout Values on a Binding |
Learn how to manage timeout settings for WCF bindings to improve performance, usability, and security of your service. |
03/30/2017 |
how-to |
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. The following timeouts are available on WCF bindings:
-
OpenTimeout
-
CloseTimeout
-
SendTimeout
-
ReceiveTimeout
Each of the settings discussed in this topic are made on the binding itself, either in code or configuration. 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>
More information about these settings can be found in the documentation for the xref:System.ServiceModel.Channels.Binding class.
On the client side:
-
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.
-
OpenTimeout – used when opening channels when no explicit timeout value is specified.
-
CloseTimeout – used when closing channels when no explicit timeout value is specified.
-
ReceiveTimeout – is not used.
On the service side:
-
SendTimeout, OpenTimeout, CloseTimeout are the same as on the client.
-
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.