Skip to content

Commit 871981f

Browse files
authored
Fix some exception handling (#5704)
Preserves the original stacktrace when AsyncResult is throwing stored exception Fix order of null checks so that SocketConnection actually throws stored exception when failing to connect
1 parent 783e979 commit 871981f

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

src/System.ServiceModel.NetTcp/src/System/ServiceModel/Channels/SocketConnection.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -969,17 +969,17 @@ public async ValueTask<IConnection> ConnectAsync(Uri uri, TimeSpan timeout)
969969
}
970970
}
971971

972-
if (socketConnection == null)
972+
if (lastException != null)
973973
{
974974
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
975-
new EndpointNotFoundException(SR.Format(SR.NoIPEndpointsFoundForHost, uri.Host)));
975+
SocketConnectionInitiator.ConvertConnectException(lastException, uri,
976+
timeoutHelper.ElapsedTime(), lastException));
976977
}
977978

978-
if (lastException != null)
979+
if (socketConnection == null)
979980
{
980981
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
981-
SocketConnectionInitiator.ConvertConnectException(lastException, uri,
982-
timeoutHelper.ElapsedTime(), lastException));
982+
new EndpointNotFoundException(SR.Format(SR.NoIPEndpointsFoundForHost, uri.Host)));
983983
}
984984

985985
return socketConnection;

src/System.ServiceModel.Primitives/src/Internals/System/Runtime/AsyncResult.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// See the LICENSE file in the project root for more information.
44

55

6+
using System.Runtime.ExceptionServices;
67
using System.Threading;
78

89
namespace System.Runtime
@@ -348,7 +349,7 @@ protected static TAsyncResult End<TAsyncResult>(IAsyncResult result)
348349

349350
if (asyncResult._exception != null)
350351
{
351-
throw Fx.Exception.AsError(asyncResult._exception);
352+
ExceptionDispatchInfo.Capture(Fx.Exception.AsError(asyncResult._exception)).Throw();
352353
}
353354

354355
return asyncResult;

0 commit comments

Comments
 (0)