Skip to content

Commit a6e5563

Browse files
committed
CSHARP-2435: Ignore PlatformNotSupportedException when configuring KeepAlive.
1 parent 7acc6cd commit a6e5563

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

src/MongoDB.Driver.Core/Core/Connections/TcpStreamFactory.cs

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -250,13 +250,30 @@ private Socket CreateSocket(EndPoint endPoint)
250250
}
251251

252252
var socket = new Socket(addressFamily, SocketType.Stream, ProtocolType.Tcp);
253-
var keepAliveValues = new KeepAliveValues
253+
254+
// not all platforms support IOControl
255+
try
254256
{
255-
OnOff = 1,
256-
KeepAliveTime = 300000, // 300 seconds in milliseconds
257-
KeepAliveInterval = 10000 // 10 seconds in milliseconds
258-
};
259-
socket.IOControl(IOControlCode.KeepAliveValues, keepAliveValues.ToBytes(), null);
257+
var keepAliveValues = new KeepAliveValues
258+
{
259+
OnOff = 1,
260+
KeepAliveTime = 300000, // 300 seconds in milliseconds
261+
KeepAliveInterval = 10000 // 10 seconds in milliseconds
262+
};
263+
socket.IOControl(IOControlCode.KeepAliveValues, keepAliveValues.ToBytes(), null);
264+
}
265+
catch (PlatformNotSupportedException)
266+
{
267+
// most platforms should support this call to SetSocketOption, but just in case call it in a try/catch also
268+
try
269+
{
270+
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
271+
}
272+
catch (PlatformNotSupportedException)
273+
{
274+
// ignore PlatformNotSupportedException
275+
}
276+
}
260277

261278
return socket;
262279
}

0 commit comments

Comments
 (0)