restsharp/RestSharp · critical · ObjectDisposedException

Cannot access a disposed object.

Error message

Cannot access a disposed object.

What it means

ExecuteRequestAsync throws ObjectDisposedException if it is invoked after the RestClient has been disposed (_disposed flag is true). RestClient owns an HttpClient and other resources; using it after Dispose is illegal because the underlying handler and connections are gone.

Solutions

  1. Do not dispose the RestClient while requests are still in flight or may still be issued.
  2. Treat RestClient as a long-lived singleton (it wraps HttpClient, which is designed for reuse).
  3. Remove the using-block around RestClient and dispose it only at application shutdown.
  4. If using a shared HttpClient, pass it to RestClient with disposeHttpClient: false so lifecycle is controlled externally.

Example fix

// before
using var client = new RestClient("https://api.example.com");
// ... later, after dispose, in a background task:
await client.ExecuteAsync(req); // throws ObjectDisposedException

// after
var client = new RestClient("https://api.example.com"); // long-lived
await client.ExecuteAsync(req);
Defensive patterns

Strategy: try-catch

Validate before calling

// Structural prevention: keep RestClient alive for the app lifetime.
// If you must check: RestSharp does not expose IsDisposed publicly,
// so track disposal in your own wrapper.
public bool IsDisposed { get; private set; }
protected override void Dispose(bool disposing) { IsDisposed = true; base.Dispose(disposing); }

Try / catch

try {
    return await client.ExecuteAsync(request, ct);
}
catch (ObjectDisposedException) {
    // client was disposed mid-flight; recreate or surface a lifecycle error
    client = CreateClient();
    return await client.ExecuteAsync(request, ct);
}

Prevention

When it happens

Trigger: Calling any Execute/ExecuteAsync method on a RestClient instance after client.Dispose() has been called, or after the using-block that owns the client has exited.

Common situations: Wrapping RestClient in a using statement but holding a reference used by a background task or delegate that fires later; sharing a singleton client that gets disposed during shutdown; DI misconfiguration disposing a transient/scoped client prematurely.

Related errors


AI-assisted analysis of restsharp/RestSharp@6a50821692 (2026-08-13). Data as JSON: /api/errors/404254fd5b53dd71. Report an issue: GitHub.

Appendix: source

Thrown at src/RestSharp/RestClient.Async.cs:102

            request.Interceptors = Options.Interceptors.ToList();
            return;
        }

        if (Options.Interceptors != null) {
            request.Interceptors.AddRange(Options.Interceptors);
        }
    }

    async Task<HttpResponse> ExecuteRequestAsync(RestRequest request, CancellationToken cancellationToken) {
        Ensure.NotNull(request, nameof(request));

        // Make sure we are not disposed of when someone tries to call us!
#if NET
        ObjectDisposedException.ThrowIf(_disposed, this);
#else
        if (_disposed) {
            throw new ObjectDisposedException(nameof(RestClient));
        }
#endif
        CombineInterceptors(request);
        await OnBeforeRequest(request, cancellationToken).ConfigureAwait(false);
        request.ValidateParameters();
        var authenticator = request.Authenticator ?? Options.Authenticator;

        if (authenticator != null) {
            await authenticator.Authenticate(this, request, cancellationToken).ConfigureAwait(false);
        }

        var contentToDispose = new List<RequestContent>();
        var initialContent   = new RequestContent(this, request);
        contentToDispose.Add(initialContent);

        var httpMethod = AsHttpMethod(request.Method);
        var url        = new Uri(this.BuildUriString(request));

View on GitHub (pinned to 6a50821692)