diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/HttpContent.cs b/src/libraries/System.Net.Http/src/System/Net/Http/HttpContent.cs index 2649dcb3de74e3..e7636d297eaff4 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/HttpContent.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/HttpContent.cs @@ -7,6 +7,7 @@ using System.Globalization; using System.IO; using System.Net.Http.Headers; +using System.Runtime.ExceptionServices; using System.Text; using System.Threading; using System.Threading.Tasks; @@ -695,7 +696,7 @@ internal static Exception WrapStreamCopyException(Exception e) { Debug.Assert(StreamCopyExceptionNeedsWrapping(e)); HttpRequestError error = e is HttpIOException ioEx ? ioEx.HttpRequestError : HttpRequestError.Unknown; - return new HttpRequestException(error, SR.net_http_content_stream_copy_error, e); + return ExceptionDispatchInfo.SetCurrentStackTrace(new HttpRequestException(error, SR.net_http_content_stream_copy_error, e)); } private static int GetPreambleLength(ReadOnlySpan data, Encoding encoding) @@ -767,7 +768,7 @@ private static async Task WaitAndReturnAsync(Task wait private static HttpRequestException CreateOverCapacityException(long maxBufferSize) { - return new HttpRequestException(HttpRequestError.ConfigurationLimitExceeded, SR.Format(CultureInfo.InvariantCulture, SR.net_http_content_buffersize_exceeded, maxBufferSize)); + return (HttpRequestException)ExceptionDispatchInfo.SetCurrentStackTrace(new HttpRequestException(HttpRequestError.ConfigurationLimitExceeded, SR.Format(CultureInfo.InvariantCulture, SR.net_http_content_buffersize_exceeded, maxBufferSize))); } /// diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/ContentLengthWriteStream.cs b/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/ContentLengthWriteStream.cs index 077fc8c6d5d3cb..071caa567b1383 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/ContentLengthWriteStream.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/ContentLengthWriteStream.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. using System.Diagnostics; +using System.Runtime.ExceptionServices; using System.Threading; using System.Threading.Tasks; @@ -39,7 +40,7 @@ public override ValueTask WriteAsync(ReadOnlyMemory buffer, CancellationTo if (BytesWritten > _contentLength) { - return ValueTask.FromException(new HttpRequestException(SR.net_http_content_write_larger_than_content_length)); + return ValueTask.FromException(ExceptionDispatchInfo.SetCurrentStackTrace(new HttpRequestException(SR.net_http_content_write_larger_than_content_length))); } HttpConnection connection = GetConnectionOrThrow(); @@ -51,7 +52,7 @@ public override Task FinishAsync(bool async) { if (BytesWritten != _contentLength) { - return Task.FromException(new HttpRequestException(SR.Format(SR.net_http_request_content_length_mismatch, BytesWritten, _contentLength))); + return Task.FromException(ExceptionDispatchInfo.SetCurrentStackTrace(new HttpRequestException(SR.Format(SR.net_http_request_content_length_mismatch, BytesWritten, _contentLength)))); } _connection = null; diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/Http2Connection.cs b/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/Http2Connection.cs index 9b48f6790bda7f..5acf3d76e513cd 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/Http2Connection.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/Http2Connection.cs @@ -9,6 +9,7 @@ using System.Net.Http.Headers; using System.Net.Http.HPack; using System.Runtime.CompilerServices; +using System.Runtime.ExceptionServices; using System.Text; using System.Threading; using System.Threading.Channels; @@ -1197,7 +1198,7 @@ private Task PerformWriteAsync(int writeBytes, T state, Func, // As such, it should not matter that we were not able to actually send the frame. // But just in case, throw ObjectDisposedException. Asynchronous callers will ignore the failure. Debug.Assert(_shutdown && _streamsInUse == 0); - return Task.FromException(new ObjectDisposedException(nameof(Http2Connection))); + return Task.FromException(ExceptionDispatchInfo.SetCurrentStackTrace(new ObjectDisposedException(nameof(Http2Connection)))); } return writeEntry.Task; @@ -2173,7 +2174,7 @@ private static void ThrowRetry(string message, Exception? innerException = null) throw new HttpRequestException((innerException as HttpIOException)?.HttpRequestError ?? HttpRequestError.Unknown, message, innerException, RequestRetryType.RetryOnConnectionFailure); private static Exception GetRequestAbortedException(Exception? innerException = null) => - innerException as HttpIOException ?? new IOException(SR.net_http_request_aborted, innerException); + innerException as HttpIOException ?? ExceptionDispatchInfo.SetCurrentStackTrace(new IOException(SR.net_http_request_aborted, innerException)); [DoesNotReturn] private static void ThrowRequestAborted(Exception? innerException = null) => diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/Http2Stream.cs b/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/Http2Stream.cs index 4b1f4ccb3ee695..c39b070c15bb28 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/Http2Stream.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/Http2Stream.cs @@ -1503,7 +1503,7 @@ public Http2ReadStream(Http2Stream http2Stream) : base(http2Stream, closeRespons public override void Write(ReadOnlySpan buffer) => throw new NotSupportedException(SR.net_http_content_readonly_stream); - public override ValueTask WriteAsync(ReadOnlyMemory destination, CancellationToken cancellationToken) => ValueTask.FromException(new NotSupportedException(SR.net_http_content_readonly_stream)); + public override ValueTask WriteAsync(ReadOnlyMemory destination, CancellationToken cancellationToken) => ValueTask.FromException(ExceptionDispatchInfo.SetCurrentStackTrace(new NotSupportedException(SR.net_http_content_readonly_stream))); } private sealed class Http2WriteStream : Http2ReadWriteStream @@ -1522,11 +1522,11 @@ public Http2WriteStream(Http2Stream http2Stream, long contentLength) : base(http public override int Read(Span buffer) => throw new NotSupportedException(SR.net_http_content_writeonly_stream); - public override ValueTask ReadAsync(Memory buffer, CancellationToken cancellationToken) => ValueTask.FromException(new NotSupportedException(SR.net_http_content_writeonly_stream)); + public override ValueTask ReadAsync(Memory buffer, CancellationToken cancellationToken) => ValueTask.FromException(ExceptionDispatchInfo.SetCurrentStackTrace(new NotSupportedException(SR.net_http_content_writeonly_stream))); public override void CopyTo(Stream destination, int bufferSize) => throw new NotSupportedException(SR.net_http_content_writeonly_stream); - public override Task CopyToAsync(Stream destination, int bufferSize, CancellationToken cancellationToken) => Task.FromException(new NotSupportedException(SR.net_http_content_writeonly_stream)); + public override Task CopyToAsync(Stream destination, int bufferSize, CancellationToken cancellationToken) => Task.FromException(ExceptionDispatchInfo.SetCurrentStackTrace(new NotSupportedException(SR.net_http_content_writeonly_stream))); public override ValueTask WriteAsync(ReadOnlyMemory buffer, CancellationToken cancellationToken) { @@ -1534,7 +1534,7 @@ public override ValueTask WriteAsync(ReadOnlyMemory buffer, CancellationTo if ((ulong)BytesWritten > (ulong)ContentLength) // If ContentLength == -1, this will always be false { - return ValueTask.FromException(new HttpRequestException(SR.net_http_content_write_larger_than_content_length)); + return ValueTask.FromException(ExceptionDispatchInfo.SetCurrentStackTrace(new HttpRequestException(SR.net_http_content_write_larger_than_content_length))); } return base.WriteAsync(buffer, cancellationToken); @@ -1643,7 +1643,7 @@ public override ValueTask WriteAsync(ReadOnlyMemory buffer, CancellationTo if (http2Stream == null) { - return ValueTask.FromException(new ObjectDisposedException(nameof(Http2WriteStream))); + return ValueTask.FromException(ExceptionDispatchInfo.SetCurrentStackTrace(new ObjectDisposedException(nameof(Http2WriteStream)))); } return http2Stream.SendDataAsync(buffer, cancellationToken); diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/Http3RequestStream.cs b/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/Http3RequestStream.cs index f5087936c59724..a9718781324c8e 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/Http3RequestStream.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/Http3RequestStream.cs @@ -1524,7 +1524,7 @@ public override ValueTask ReadAsync(Memory buffer, CancellationToken if (stream is null) { - return ValueTask.FromException(new ObjectDisposedException(nameof(Http3RequestStream))); + return ValueTask.FromException(ExceptionDispatchInfo.SetCurrentStackTrace(new ObjectDisposedException(nameof(Http3RequestStream)))); } Debug.Assert(_response != null); @@ -1577,7 +1577,7 @@ public override ValueTask WriteAsync(ReadOnlyMemory buffer, CancellationTo if (stream is null) { - return ValueTask.FromException(new ObjectDisposedException(nameof(Http3WriteStream))); + return ValueTask.FromException(ExceptionDispatchInfo.SetCurrentStackTrace(new ObjectDisposedException(nameof(Http3WriteStream)))); } return stream.WriteRequestContentAsync(buffer, cancellationToken); @@ -1589,7 +1589,7 @@ public override Task FlushAsync(CancellationToken cancellationToken) if (stream is null) { - return Task.FromException(new ObjectDisposedException(nameof(Http3WriteStream))); + return Task.FromException(ExceptionDispatchInfo.SetCurrentStackTrace(new ObjectDisposedException(nameof(Http3WriteStream)))); } return stream.FlushSendBufferAsync(endStream: false, cancellationToken).AsTask(); diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/SocketsHttpHandler.cs b/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/SocketsHttpHandler.cs index 19f4197ccd26d6..762d0c0a2a2f3e 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/SocketsHttpHandler.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/SocketsHttpHandler.cs @@ -8,6 +8,7 @@ using System.IO; using System.Net.Http.Metrics; using System.Net.Security; +using System.Runtime.ExceptionServices; using System.Runtime.Versioning; using System.Text; using System.Threading; @@ -634,7 +635,7 @@ async Task CreateHandlerAndSendAsync(HttpRequestMessage req { if (request.Version != HttpVersion.Version10 && request.Version != HttpVersion.Version11 && request.Version != HttpVersion.Version20 && request.Version != HttpVersion.Version30) { - return new NotSupportedException(SR.net_http_unsupported_version); + return ExceptionDispatchInfo.SetCurrentStackTrace(new NotSupportedException(SR.net_http_unsupported_version)); } // Add headers to define content transfer, if not present @@ -642,8 +643,8 @@ async Task CreateHandlerAndSendAsync(HttpRequestMessage req { if (request.Content == null) { - return new HttpRequestException(SR.net_http_client_execution_error, - new InvalidOperationException(SR.net_http_chunked_not_allowed_with_empty_content)); + return ExceptionDispatchInfo.SetCurrentStackTrace(new HttpRequestException(SR.net_http_client_execution_error, + ExceptionDispatchInfo.SetCurrentStackTrace(new InvalidOperationException(SR.net_http_chunked_not_allowed_with_empty_content)))); } // Since the user explicitly set TransferEncodingChunked to true, we need to remove @@ -661,7 +662,7 @@ async Task CreateHandlerAndSendAsync(HttpRequestMessage req // HTTP 1.0 does not support chunking if (request.Headers.TransferEncodingChunked == true) { - return new NotSupportedException(SR.net_http_unsupported_chunking); + return ExceptionDispatchInfo.SetCurrentStackTrace(new NotSupportedException(SR.net_http_unsupported_chunking)); } // HTTP 1.0 does not support Expect: 100-continue; just disable it. @@ -674,12 +675,12 @@ async Task CreateHandlerAndSendAsync(HttpRequestMessage req Uri? requestUri = request.RequestUri; if (requestUri is null || !requestUri.IsAbsoluteUri) { - return new InvalidOperationException(SR.net_http_client_invalid_requesturi); + return ExceptionDispatchInfo.SetCurrentStackTrace(new InvalidOperationException(SR.net_http_client_invalid_requesturi)); } if (!HttpUtilities.IsSupportedScheme(requestUri.Scheme)) { - return new NotSupportedException(SR.Format(SR.net_http_unsupported_requesturi_scheme, requestUri.Scheme)); + return ExceptionDispatchInfo.SetCurrentStackTrace(new NotSupportedException(SR.Format(SR.net_http_unsupported_requesturi_scheme, requestUri.Scheme))); } return null; diff --git a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.Tasks.cs b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.Tasks.cs index fb08996f02f3af..232c8e24381184 100644 --- a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.Tasks.cs +++ b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.Tasks.cs @@ -731,7 +731,7 @@ public ValueTask SendFileAsync(string? fileName, ReadOnlyMemory preBuffer, if (!IsConnectionOriented) { - var ex = new NotSupportedException(SR.net_notconnected); + var ex = ExceptionDispatchInfo.SetCurrentStackTrace(new NotSupportedException(SR.net_notconnected)); return ValueTask.FromException(ex); } diff --git a/src/libraries/System.Net.WebSockets/src/System/Net/WebSockets/ManagedWebSocket.cs b/src/libraries/System.Net.WebSockets/src/System/Net/WebSockets/ManagedWebSocket.cs index 25bcdf78fae09b..4e3a261e1ecb77 100644 --- a/src/libraries/System.Net.WebSockets/src/System/Net/WebSockets/ManagedWebSocket.cs +++ b/src/libraries/System.Net.WebSockets/src/System/Net/WebSockets/ManagedWebSocket.cs @@ -534,7 +534,7 @@ private ValueTask SendFrameLockAcquiredNonCancelableAsync(MessageOpcode opcode, return ValueTask.FromException( exc is OperationCanceledException ? exc : _state == WebSocketState.Aborted ? CreateOperationCanceledException(exc) : - new WebSocketException(WebSocketError.ConnectionClosedPrematurely, exc)); + ExceptionDispatchInfo.SetCurrentStackTrace(new WebSocketException(WebSocketError.ConnectionClosedPrematurely, exc))); } finally { @@ -1714,10 +1714,10 @@ private void ThrowIfOperationInProgress(bool operationCompleted, [CallerMemberNa /// Creates an OperationCanceledException instance, using a default message and the specified inner exception and token. private static OperationCanceledException CreateOperationCanceledException(Exception innerException, CancellationToken cancellationToken = default(CancellationToken)) { - return new OperationCanceledException( + return (OperationCanceledException)ExceptionDispatchInfo.SetCurrentStackTrace(new OperationCanceledException( new OperationCanceledException().Message, innerException, - cancellationToken); + cancellationToken)); } private void ThrowIfDisposed() => ThrowIfInvalidState(validStates: ManagedWebSocketStates.All); diff --git a/src/libraries/System.Net.WebSockets/src/System/Net/WebSockets/WebSocketStream.cs b/src/libraries/System.Net.WebSockets/src/System/Net/WebSockets/WebSocketStream.cs index 2f350d4846a6d8..cd262f5bbbc89d 100644 --- a/src/libraries/System.Net.WebSockets/src/System/Net/WebSockets/WebSocketStream.cs +++ b/src/libraries/System.Net.WebSockets/src/System/Net/WebSockets/WebSocketStream.cs @@ -4,6 +4,7 @@ using System.Diagnostics; using System.Diagnostics.CodeAnalysis; using System.IO; +using System.Runtime.ExceptionServices; using System.Threading; using System.Threading.Tasks; @@ -130,7 +131,7 @@ public override Task ReadAsync(byte[] buffer, int offset, int count, Cancel /// public override ValueTask ReadAsync(Memory buffer, CancellationToken cancellationToken = default) => - ValueTask.FromException(new NotSupportedException()); + ValueTask.FromException(ExceptionDispatchInfo.SetCurrentStackTrace(new NotSupportedException())); /// public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state) => @@ -150,7 +151,7 @@ public override Task WriteAsync(byte[] buffer, int offset, int count, Cancellati /// public override ValueTask WriteAsync(ReadOnlyMemory buffer, CancellationToken cancellationToken = default) => - ValueTask.FromException(new NotSupportedException()); + ValueTask.FromException(ExceptionDispatchInfo.SetCurrentStackTrace(new NotSupportedException())); /// public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state) => @@ -195,12 +196,12 @@ public override ValueTask WriteAsync(ReadOnlyMemory buffer, CancellationTo { if (_disposed) { - return ValueTask.FromException(new ObjectDisposedException(GetType().FullName)); + return ValueTask.FromException(ExceptionDispatchInfo.SetCurrentStackTrace(new ObjectDisposedException(GetType().FullName))); } if (!CanWrite) { - return ValueTask.FromException(new NotSupportedException(SR.NotWriteableStream)); + return ValueTask.FromException(ExceptionDispatchInfo.SetCurrentStackTrace(new NotSupportedException(SR.NotWriteableStream))); } if (cancellationToken.IsCancellationRequested) @@ -297,12 +298,12 @@ public override ValueTask WriteAsync(ReadOnlyMemory buffer, CancellationTo { if (_disposed) { - return ValueTask.FromException(new ObjectDisposedException(GetType().FullName)); + return ValueTask.FromException(ExceptionDispatchInfo.SetCurrentStackTrace(new ObjectDisposedException(GetType().FullName))); } if (!CanWrite) { - return ValueTask.FromException(new NotSupportedException(SR.NotWriteableStream)); + return ValueTask.FromException(ExceptionDispatchInfo.SetCurrentStackTrace(new NotSupportedException(SR.NotWriteableStream))); } if (cancellationToken.IsCancellationRequested)