ConnectionLeaseTimeout Property

ServicePoint.ConnectionLeaseTimeout Property


Gets or sets the number of milliseconds after which an active ServicePoint connection is closed.

Namespace:   System.Net
Assembly:  System (in System.dll)

public int ConnectionLeaseTimeout { get; set; }

Property Value

Type: System.Int32

A Int32 that specifies the number of milliseconds that an active ServicePoint connection remains open. The default is -1, which allows an active ServicePoint connection to stay connected indefinitely. Set this property to 0 to force ServicePoint connections to close after servicing a request.

Exception Condition

The value specified for a set operation is a negative number less than -1.

You can use this property to ensure that a ServicePoint object's active connections do not remain open indefinitely. This property is intended for scenarios where connections should be dropped and reestablished periodically, such as load balancing scenarios.

By default, when KeepAlive is true for a request, the MaxIdleTime property sets the time-out for closing ServicePoint connections due to inactivity. If the ServicePoint has active connections, MaxIdleTime has no effect and the connections remain open indefinitely.

When the ConnectionLeaseTimeout property is set to a value other than -1, and after the specified time elapses, an active ServicePoint connection is closed after servicing a request by setting KeepAlive to false in that request.

Setting this value affects all connections managed by the ServicePoint object.

The following code example sets the value of this property.

using System;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;

namespace Examples.System.Net
    public class ServicePointExample
        // Pass in the name of the Web page to retrieve.
        public static void Main (string[] args)
            string page;
            if (args == null || args.Length == 0 || args[0].Length == 0)
                page = "";
                page = args[0];
            // Create the request.
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create (page);
            // Get the service point that handles the request's socket connection.
            ServicePoint point = request.ServicePoint;
            // Set the receive buffer size on the underlying socket.
            point.ReceiveBufferSize = 2048;
            // Set the connection lease timeout to infinite.
            point.ConnectionLeaseTimeout = Timeout.Infinite;
            // Send the request.
            HttpWebResponse response = (HttpWebResponse)request.GetResponse ();
            Stream responseStream = response.GetResponseStream ();
            StreamReader s = new StreamReader (responseStream);
            // Display the response.
            Console.WriteLine (s.ReadToEnd ());

.NET Framework
Available since 2.0
Return to top
© 2015 Microsoft