From 371f4429bf6ede377fb54d1082ae116a649104f7 Mon Sep 17 00:00:00 2001 From: Matt Connew Date: Fri, 9 Mar 2018 21:31:29 -0800 Subject: [PATCH] Add new exception handling logic for SocketsHttpHandler differences --- .../src/System.Private.ServiceModel.csproj | 2 +- .../Channels/HttpChannelHelpers.cs | 21 ++++++++++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/System.Private.ServiceModel/src/System.Private.ServiceModel.csproj b/src/System.Private.ServiceModel/src/System.Private.ServiceModel.csproj index 4acfcad2be4..ff34e7fae60 100644 --- a/src/System.Private.ServiceModel/src/System.Private.ServiceModel.csproj +++ b/src/System.Private.ServiceModel/src/System.Private.ServiceModel.csproj @@ -104,4 +104,4 @@ - + \ No newline at end of file diff --git a/src/System.Private.ServiceModel/src/System/ServiceModel/Channels/HttpChannelHelpers.cs b/src/System.Private.ServiceModel/src/System/ServiceModel/Channels/HttpChannelHelpers.cs index 2ba244c34ad..15993367357 100644 --- a/src/System.Private.ServiceModel/src/System/ServiceModel/Channels/HttpChannelHelpers.cs +++ b/src/System.Private.ServiceModel/src/System/ServiceModel/Channels/HttpChannelHelpers.cs @@ -2,14 +2,15 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. - using System.Diagnostics.Contracts; using System.IO; using System.Net; using System.Net.Http; using System.Net.Security; +using System.Net.Sockets; using System.Runtime; using System.Runtime.CompilerServices; +using System.Security.Authentication; using System.Security.Principal; using System.ServiceModel.Security; using System.ServiceModel.Security.Tokens; @@ -132,6 +133,24 @@ public static Exception ConvertHttpRequestException(HttpRequestException excepti Contract.Assert(exception.InnerException != null, "InnerException must be set to be able to convert"); uint hresult = (uint)exception.InnerException.HResult; + var innerSocketException = exception.InnerException as SocketException; + if (innerSocketException != null) + { + var socketErrorCode = innerSocketException.SocketErrorCode; + switch (socketErrorCode) + { + case SocketError.HostNotFound: + return new EndpointNotFoundException(SR.Format(SR.EndpointNotFound, request.RequestUri.AbsoluteUri), exception); + default: + break; + } + } + + if (exception.InnerException is AuthenticationException) + { + return new SecurityNegotiationException(SR.Format(SR.TrustFailure, request.RequestUri.Authority), exception); + } + switch (hresult) { // .Net Native HttpClientHandler sometimes reports an incorrect handle state when a connection is aborted, so we treat it as a connection reset error