ServicePoint.ConnectionLeaseTimeout プロパティ
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
アクティブな ServicePoint 接続が閉じられる時間 (ミリ秒) を取得または設定します。
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
プロパティ値
アクティブなInt32接続が開いたままになるミリ秒数を指定するServicePoint。 既定値は -1 で、アクティブな ServicePoint 接続を無期限に接続できます。 要求を処理した後、 ServicePoint 接続を強制的に閉じるには、このプロパティを 0 に設定します。
例外
設定操作に指定された値は、-1 未満の負の数です。
例
次のコード例では、このプロパティの値を設定します。
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
注釈
Caution
WebRequest、 HttpWebRequest、 ServicePoint、 WebClient は廃止されており、新しい開発には使用しないでください。
HttpClient を代わりに使用します。
このプロパティを使用すると、 ServicePoint オブジェクトのアクティブな接続が無期限に開いたままでないことを確認できます。 このプロパティは、負荷分散シナリオなど、接続を削除して定期的に再確立する必要があるシナリオを対象としています。
既定では、 KeepAlive が要求に対して true されると、 MaxIdleTime プロパティは、非アクティブのため、 ServicePoint 接続を閉じるためのタイムアウトを設定します。
ServicePointにアクティブな接続がある場合、MaxIdleTimeは影響を受けず、接続は無期限に開いたままです。
ConnectionLeaseTimeout プロパティが -1 以外の値に設定され、指定した時間が経過すると、ServicePointをその要求のKeepAliveに設定して、要求を処理した後、アクティブなfalse接続が閉じられます。
この値を設定すると、 ServicePoint オブジェクトによって管理されるすべての接続に影響します。
Note
.NET 9 以降、このプロパティは SocketsHttpHandler.PooledConnectionLifetimeにマップされます。 ただし、ハンドラーは要求間で再利用されないため、意味のある影響はありません。