ServicePoint.ConnectionLeaseTimeout Proprietà

Definizione

Ottiene o imposta il numero di millisecondi dopo il quale una connessione ServicePoint attiva viene chiusa.

public:
 property int ConnectionLeaseTimeout { int get(); void set(int value); };
public int ConnectionLeaseTimeout { get; set; }
member this.ConnectionLeaseTimeout : int with get, set
Public Property ConnectionLeaseTimeout As Integer

Valore della proprietà

Struttura Int32 che specifica il numero di millisecondi per il quale una connessione ServicePoint attiva resta aperta. Il valore predefinito è -1, che consente a una connessione ServicePoint attiva di restare aperta a tempo indefinito. Impostare questa proprietà su 0 per forzare la chiusura delle connessioni ServicePoint dopo la gestione di una richiesta.

Eccezioni

Il valore specificato per un'operazione di impostazione è un numero negativo minore di -1.

Esempio

Nell'esempio di codice seguente viene impostato il valore di questa proprietà.

#using <System.dll>

using namespace System;
using namespace System::Net;
using namespace System::Net::Sockets;
using namespace System::IO;
using namespace System::Threading;

namespace SystemNetExamples
{
    public ref class ServicePointExample
    {
    public:

        // Pass in the name of the Web page to retrieve.    
        static void PrintResponse(String^ page)
        {

            // Create the request.
            HttpWebRequest^ request;
            Uri^ uri;
            
 

            try
            {
                uri = gcnew Uri(page);
            }
            catch (UriFormatException^ ex) 
            {
                Console::WriteLine(ex->Message);
            }

            request = (HttpWebRequest^) WebRequest::Create(uri); 

            // 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^ streamReader = 
                gcnew StreamReader(responseStream);
            try
            {
                // Display the response.
                Console::WriteLine(streamReader->ReadToEnd());
                responseStream->Close();
                response->Close();
            }
            finally
            {
                streamReader->Close();
            }
        }
    };
}
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 = "http://www.contoso.com/default.html";
            }
            else
            {
                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());
            s.Close();
            responseStream.Close();
            response.Close();
        }
    }
}
Imports System.Net
Imports System.Net.Sockets
Imports System.IO
Imports System.Threading

Public Class ServicePointExample

    ' Pass in the name of the Web page to retrieve.
    Public Shared Sub Main(ByVal args() As String)
        Dim page As String
        If args Is Nothing OrElse args.Length = 0 OrElse args(0).Length = 0 Then
            page = "http://www.contoso.com/default.html"
        Else
            page = args(0)
        End If

        Dim request As HttpWebRequest = CType(WebRequest.Create(page), HttpWebRequest)
        ' Get the service point that handles the request's socket connection.
        Dim point As ServicePoint = 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.
        Dim response As HttpWebResponse = CType(request.GetResponse(), HttpWebResponse)
        Dim responseStream As Stream = response.GetResponseStream()
        Dim s As New StreamReader(responseStream)
        ' Display the response.
        Console.WriteLine(s.ReadToEnd())
        responseStream.Close()
        response.Close()
    End Sub
End Class

Commenti

È possibile usare questa proprietà per assicurarsi che le connessioni attive di un ServicePoint oggetto non rimangano aperte in modo indefinito. Questa proprietà è destinata agli scenari in cui le connessioni devono essere eliminate e ripristinate periodicamente, ad esempio scenari di bilanciamento del carico.

Per impostazione predefinita, quando KeepAlive è true per una richiesta, la MaxIdleTime proprietà imposta il timeout per chiudere ServicePoint le connessioni a causa dell'inattività. Se l'oggetto ServicePoint ha connessioni attive, MaxIdleTime non ha alcun effetto e le connessioni rimangono aperte in modo indefinito.

Quando la ConnectionLeaseTimeout proprietà è impostata su un valore diverso da -1 e dopo l'intervallo di tempo specificato, una connessione attiva ServicePoint viene chiusa dopo la manutenzione di una richiesta impostandola KeepAlivefalse su in tale richiesta.

L'impostazione di questo valore influisce su tutte le connessioni gestite dall'oggetto ServicePoint .

Si applica a

Vedi anche