Skip to content

refactor : use new ThrowIf overload of ObjectDisposedException #42613

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 2 additions & 8 deletions src/Components/Components/src/OwningComponentBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,7 @@ protected IServiceProvider ScopedServices
throw new InvalidOperationException("Services cannot be accessed before the component is initialized.");
}

if (IsDisposed)
{
throw new ObjectDisposedException(GetType().Name);
}
ObjectDisposedException.ThrowIf(IsDisposed, this);

_scope ??= ScopeFactory.CreateAsyncScope();
return _scope.Value.ServiceProvider;
Expand Down Expand Up @@ -85,10 +82,7 @@ protected TService Service
{
get
{
if (IsDisposed)
{
throw new ObjectDisposedException(GetType().Name);
}
ObjectDisposedException.ThrowIf(IsDisposed, this);

// We cache this because we don't know the lifetime. We have to assume that it could be transient.
_item ??= ScopedServices.GetRequiredService<TService>();
Expand Down
10 changes: 1 addition & 9 deletions src/Components/Shared/src/ArrayBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,7 @@ protected void GrowBuffer(int desiredCapacity)
// If someone tries to do something that would require non-zero storage then
// this is a use-after-free. Throwing here is an easy way to prevent that without
// introducing overhead to every method.
if (_disposed)
{
ThrowObjectDisposedException();
}
ObjectDisposedException.ThrowIf(_disposed, null);

var newCapacity = Math.Max(desiredCapacity, _minCapacity);
Debug.Assert(newCapacity > _items.Length);
Expand Down Expand Up @@ -214,9 +211,4 @@ private static void ThrowIndexOutOfBoundsException()
{
throw new ArgumentOutOfRangeException("index");
}

private static void ThrowObjectDisposedException()
{
throw new ObjectDisposedException(objectName: null);
}
}
5 changes: 1 addition & 4 deletions src/Hosting/TestHost/src/TestServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -223,10 +223,7 @@ Task IServer.StartAsync<TContext>(IHttpApplication<TContext> application, Cancel
{
_application = new ApplicationWrapper<TContext>(application, () =>
{
if (_disposed)
{
throw new ObjectDisposedException(GetType().FullName);
}
ObjectDisposedException.ThrowIf(_disposed, this);
});

return Task.CompletedTask;
Expand Down
23 changes: 7 additions & 16 deletions src/Hosting/TestHost/src/TestWebSocket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,7 @@ private void Close()

private void ThrowIfDisposed()
{
if (_state >= WebSocketState.Closed) // or Aborted
{
throw new ObjectDisposedException(typeof(TestWebSocket).FullName);
}
ObjectDisposedException.ThrowIf(_state >= WebSocketState.Closed, typeof(TestWebSocket)); // or Aborted
}

private void ThrowIfOutputClosed()
Expand Down Expand Up @@ -282,10 +279,8 @@ public virtual Task SendAsync(Message message, CancellationToken cancellationTok
{
lock (_messageQueue)
{
if (_senderClosed)
{
throw new ObjectDisposedException(typeof(TestWebSocket).FullName);
}
ObjectDisposedException.ThrowIf(_senderClosed, typeof(TestWebSocket));

if (_receiverClosed)
{
throw new IOException("The remote end closed the connection.", new ObjectDisposedException(typeof(TestWebSocket).FullName));
Expand Down Expand Up @@ -335,14 +330,10 @@ public void SetSenderClosed()

private void ThrowNoReceive()
{
if (_receiverClosed)
{
throw new ObjectDisposedException(typeof(TestWebSocket).FullName);
}
else // _senderClosed
{
throw new IOException("The remote end closed the connection.", new ObjectDisposedException(typeof(TestWebSocket).FullName));
}
ObjectDisposedException.ThrowIf(_receiverClosed, typeof(TestWebSocket));

// _senderClosed must be true.
throw new IOException("The remote end closed the connection.", new ObjectDisposedException(typeof(TestWebSocket).FullName));
}
}
}
5 changes: 1 addition & 4 deletions src/Http/Http/src/Internal/ReferenceReadStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,6 @@ protected override void Dispose(bool disposing)

private void ThrowIfDisposed()
{
if (_disposed)
{
throw new ObjectDisposedException(nameof(ReferenceReadStream));
}
ObjectDisposedException.ThrowIf(_disposed, nameof(ReferenceReadStream));
}
}
5 changes: 1 addition & 4 deletions src/Http/WebUtilities/src/BufferedReadStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -419,10 +419,7 @@ private static string DecodeLine(MemoryStream builder, bool foundCRLF)

private void CheckDisposed()
{
if (_disposed)
{
throw new ObjectDisposedException(nameof(BufferedReadStream));
}
ObjectDisposedException.ThrowIf(_disposed, nameof(BufferedReadStream));
}

private static void ValidateBuffer(byte[] buffer, int offset, int count)
Expand Down
5 changes: 1 addition & 4 deletions src/Http/WebUtilities/src/FileBufferingReadStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -500,9 +500,6 @@ public override async ValueTask DisposeAsync()

private void ThrowIfDisposed()
{
if (_disposed)
{
throw new ObjectDisposedException(nameof(FileBufferingReadStream));
}
ObjectDisposedException.ThrowIf(_disposed, nameof(FileBufferingReadStream));
}
}
10 changes: 2 additions & 8 deletions src/Http/WebUtilities/src/FileBufferingWriteStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -283,18 +283,12 @@ private void EnsureFileStream()

private void ThrowIfDisposed()
{
if (Disposed)
{
throw new ObjectDisposedException(nameof(FileBufferingWriteStream));
}
ObjectDisposedException.ThrowIf(Disposed, nameof(FileBufferingWriteStream));
}

private static void ThrowArgumentException(byte[] buffer, int offset, int count)
{
if (buffer == null)
{
throw new ArgumentNullException(nameof(buffer));
}
ArgumentNullException.ThrowIfNull(buffer);

if (offset < 0)
{
Expand Down
30 changes: 6 additions & 24 deletions src/Http/WebUtilities/src/HttpRequestStreamReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,7 @@ protected override void Dispose(bool disposing)
/// <inheritdoc />
public override int Peek()
{
if (_disposed)
{
throw new ObjectDisposedException(nameof(HttpRequestStreamReader));
}
ObjectDisposedException.ThrowIf(_disposed, nameof(HttpRequestStreamReader));

if (_charBufferIndex == _charsRead)
{
Expand All @@ -142,10 +139,7 @@ public override int Peek()
/// <inheritdoc />
public override int Read()
{
if (_disposed)
{
throw new ObjectDisposedException(nameof(HttpRequestStreamReader));
}
ObjectDisposedException.ThrowIf(_disposed, nameof(HttpRequestStreamReader));

if (_charBufferIndex == _charsRead)
{
Expand Down Expand Up @@ -188,10 +182,7 @@ public override int Read(Span<char> buffer)
throw new ArgumentNullException(nameof(buffer));
}

if (_disposed)
{
throw new ObjectDisposedException(nameof(HttpRequestStreamReader));
}
ObjectDisposedException.ThrowIf(_disposed, nameof(HttpRequestStreamReader));

var count = buffer.Length;
var charsRead = 0;
Expand Down Expand Up @@ -260,10 +251,7 @@ public override Task<int> ReadAsync(char[] buffer, int index, int count)
[SuppressMessage("ApiDesign", "RS0027:Public API with optional parameter(s) should have the most parameters amongst its public overloads.", Justification = "Required to maintain compatibility")]
public override async ValueTask<int> ReadAsync(Memory<char> buffer, CancellationToken cancellationToken = default)
{
if (_disposed)
{
throw new ObjectDisposedException(nameof(HttpRequestStreamReader));
}
ObjectDisposedException.ThrowIf(_disposed, nameof(HttpRequestStreamReader));

if (_charBufferIndex == _charsRead && await ReadIntoBufferAsync() == 0)
{
Expand Down Expand Up @@ -353,10 +341,7 @@ public override async ValueTask<int> ReadAsync(Memory<char> buffer, Cancellation
/// <inheritdoc />
public override async Task<string?> ReadLineAsync()
{
if (_disposed)
{
throw new ObjectDisposedException(nameof(HttpRequestStreamReader));
}
ObjectDisposedException.ThrowIf(_disposed, nameof(HttpRequestStreamReader));

StringBuilder? sb = null;
var consumeLineFeed = false;
Expand Down Expand Up @@ -391,10 +376,7 @@ public override async ValueTask<int> ReadAsync(Memory<char> buffer, Cancellation
/// <inheritdoc />
public override string? ReadLine()
{
if (_disposed)
{
throw new ObjectDisposedException(nameof(HttpRequestStreamReader));
}
ObjectDisposedException.ThrowIf(_disposed, nameof(HttpRequestStreamReader));

StringBuilder? sb = null;
var consumeLineFeed = false;
Expand Down
30 changes: 6 additions & 24 deletions src/Http/WebUtilities/src/HttpResponseStreamWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,7 @@ public HttpResponseStreamWriter(
/// <inheritdoc/>
public override void Write(char value)
{
if (_disposed)
{
throw new ObjectDisposedException(nameof(HttpResponseStreamWriter));
}
ObjectDisposedException.ThrowIf(_disposed, nameof(HttpResponseStreamWriter));

if (_charBufferCount == _charBufferSize)
{
Expand All @@ -125,10 +122,7 @@ public override void Write(char value)
/// <inheritdoc/>
public override void Write(char[] values, int index, int count)
{
if (_disposed)
{
throw new ObjectDisposedException(nameof(HttpResponseStreamWriter));
}
ObjectDisposedException.ThrowIf(_disposed, nameof(HttpResponseStreamWriter));

if (values == null)
{
Expand All @@ -149,10 +143,7 @@ public override void Write(char[] values, int index, int count)
/// <inheritdoc/>
public override void Write(ReadOnlySpan<char> value)
{
if (_disposed)
{
throw new ObjectDisposedException(nameof(HttpResponseStreamWriter));
}
ObjectDisposedException.ThrowIf(_disposed, nameof(HttpResponseStreamWriter));

var remaining = value.Length;
while (remaining > 0)
Expand All @@ -172,10 +163,7 @@ public override void Write(ReadOnlySpan<char> value)
/// <inheritdoc/>
public override void Write(string? value)
{
if (_disposed)
{
throw new ObjectDisposedException(nameof(HttpResponseStreamWriter));
}
ObjectDisposedException.ThrowIf(_disposed, nameof(HttpResponseStreamWriter));

if (value == null)
{
Expand All @@ -198,10 +186,7 @@ public override void Write(string? value)
/// <inheritdoc/>
public override void WriteLine(ReadOnlySpan<char> value)
{
if (_disposed)
{
throw new ObjectDisposedException(nameof(HttpResponseStreamWriter));
}
ObjectDisposedException.ThrowIf(_disposed, nameof(HttpResponseStreamWriter));

Write(value);
Write(NewLine);
Expand Down Expand Up @@ -523,10 +508,7 @@ private async Task WriteLineAsyncAwaited(string value)
/// <inheritdoc/>
public override void Flush()
{
if (_disposed)
{
throw new ObjectDisposedException(nameof(HttpResponseStreamWriter));
}
ObjectDisposedException.ThrowIf(_disposed, nameof(HttpResponseStreamWriter));

FlushInternal(flushEncoder: true);
}
Expand Down
5 changes: 1 addition & 4 deletions src/Http/WebUtilities/src/PagedByteBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,6 @@ private void ClearBuffers()

private void ThrowIfDisposed()
{
if (Disposed)
{
throw new ObjectDisposedException(nameof(PagedByteBuffer));
}
ObjectDisposedException.ThrowIf(Disposed, nameof(PagedByteBuffer));
}
}
5 changes: 1 addition & 4 deletions src/Identity/EntityFrameworkCore/src/RoleStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -346,10 +346,7 @@ protected virtual async Task SaveChanges(CancellationToken cancellationToken)
/// </summary>
protected void ThrowIfDisposed()
{
if (_disposed)
{
throw new ObjectDisposedException(GetType().Name);
}
ObjectDisposedException.ThrowIf(_disposed, this);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,6 @@ public void Dispose()

internal void ThrowIfDisposed()
{
if (Disposed)
{
throw new ObjectDisposedException(GetType().Name);
}
ObjectDisposedException.ThrowIf(Disposed, this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,6 @@ public async ValueTask DisposeAsync()
/// <inheritdoc />
protected void ThrowIfDisposed()
{
if (Disposed)
{
throw new ObjectDisposedException(GetType().Name);
}
ObjectDisposedException.ThrowIf(Disposed, this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,7 @@ public virtual ILogger CreateLogger(string name)

private void CheckDisposed()
{
if (_disposed)
{
throw new ObjectDisposedException(nameof(ListLoggerFactory));
}
ObjectDisposedException.ThrowIf(_disposed, nameof(ListLoggerFactory));
}

public void AddProvider(ILoggerProvider provider)
Expand Down
5 changes: 1 addition & 4 deletions src/Middleware/WebSockets/test/UnitTests/BufferStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -338,9 +338,6 @@ protected override void Dispose(bool disposing)

private void CheckDisposed()
{
if (_disposed)
{
throw new ObjectDisposedException(GetType().FullName);
}
ObjectDisposedException.ThrowIf(_disposed, this);
}
}
5 changes: 1 addition & 4 deletions src/Servers/HttpSys/src/HttpSysListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -418,9 +418,6 @@ internal unsafe void SendError(ulong requestId, int httpStatusCode, IList<string

private void CheckDisposed()
{
if (_state == State.Disposed)
{
throw new ObjectDisposedException(this.GetType().FullName);
}
ObjectDisposedException.ThrowIf(_state == State.Disposed, this);
}
}
5 changes: 1 addition & 4 deletions src/Servers/HttpSys/src/NativeInterop/RequestQueue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -201,10 +201,7 @@ public void Dispose()

private void CheckDisposed()
{
if (_disposed)
{
throw new ObjectDisposedException(this.GetType().FullName);
}
ObjectDisposedException.ThrowIf(_disposed, this);
}

private static partial class Log
Expand Down
Loading