Skip to content

Commit 5b82ef3

Browse files
authored
CSHARP-5725: C# driver stopped working with Unity since v3.2.0 (#1793)
1 parent eaa2eae commit 5b82ef3

File tree

1 file changed

+30
-20
lines changed

1 file changed

+30
-20
lines changed

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

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ private void ConfigureConnectedSocket(Socket socket)
166166
private void Connect(Socket socket, EndPoint endPoint, CancellationToken cancellationToken)
167167
{
168168
IAsyncResult connectOperation;
169-
169+
#if NET472
170170
if (endPoint is DnsEndPoint dnsEndPoint)
171171
{
172172
// mono doesn't support DnsEndPoint in its BeginConnect method.
@@ -176,6 +176,9 @@ private void Connect(Socket socket, EndPoint endPoint, CancellationToken cancell
176176
{
177177
connectOperation = socket.BeginConnect(endPoint, null, null);
178178
}
179+
#else
180+
connectOperation = socket.BeginConnect(endPoint, null, null);
181+
#endif
179182

180183
WaitHandle.WaitAny([connectOperation.AsyncWaitHandle, cancellationToken.WaitHandle], _settings.ConnectTimeout);
181184

@@ -203,31 +206,38 @@ private void Connect(Socket socket, EndPoint endPoint, CancellationToken cancell
203206

204207
private async Task ConnectAsync(Socket socket, EndPoint endPoint, CancellationToken cancellationToken)
205208
{
206-
var timeoutTask = Task.Delay(_settings.ConnectTimeout, cancellationToken);
207-
var connectTask = socket.ConnectAsync(endPoint);
208-
209-
await Task.WhenAny(connectTask, timeoutTask).ConfigureAwait(false);
210-
211-
if (!connectTask.IsCompleted)
209+
Task connectTask;
210+
#if NET472
211+
if (endPoint is DnsEndPoint dnsEndPoint)
212212
{
213-
try
214-
{
215-
socket.Dispose();
216-
// should await on the read task to avoid UnobservedTaskException
217-
await connectTask.ConfigureAwait(false);
218-
} catch { }
219-
220-
cancellationToken.ThrowIfCancellationRequested();
221-
throw new TimeoutException($"Timed out connecting to {endPoint}. Timeout was {_settings.ConnectTimeout}.");
213+
// mono doesn't support DnsEndPoint in its ConnectAsync method.
214+
connectTask = socket.ConnectAsync(dnsEndPoint.Host, dnsEndPoint.Port);
222215
}
223-
216+
else
217+
{
218+
connectTask = socket.ConnectAsync(endPoint);
219+
}
220+
#else
221+
connectTask = socket.ConnectAsync(endPoint);
222+
#endif
224223
try
225224
{
226-
await connectTask.ConfigureAwait(false);
225+
await connectTask.WaitAsync(_settings.ConnectTimeout, cancellationToken).ConfigureAwait(false);
227226
}
228-
catch
227+
catch (Exception ex)
229228
{
230-
try { socket.Dispose(); } catch { }
229+
try
230+
{
231+
socket.Dispose();
232+
connectTask.IgnoreExceptions();
233+
}
234+
catch { }
235+
236+
if (ex is TimeoutException)
237+
{
238+
throw new TimeoutException($"Timed out connecting to {endPoint}. Timeout was {_settings.ConnectTimeout}.");
239+
}
240+
231241
throw;
232242
}
233243
}

0 commit comments

Comments
 (0)