{"record":{"id":"404254fd5b53dd71","repo":"restsharp/RestSharp","slug":"cannot-access-a-disposed-object","errorCode":null,"errorMessage":"Cannot access a disposed object.","messagePattern":"Cannot access a disposed object\\.","errorType":"exception","errorClass":"ObjectDisposedException","httpStatus":null,"severity":"critical","filePath":"src/RestSharp/RestClient.Async.cs","lineNumber":102,"sourceCode":"\n            request.Interceptors = Options.Interceptors.ToList();\n            return;\n        }\n\n        if (Options.Interceptors != null) {\n            request.Interceptors.AddRange(Options.Interceptors);\n        }\n    }\n\n    async Task<HttpResponse> ExecuteRequestAsync(RestRequest request, CancellationToken cancellationToken) {\n        Ensure.NotNull(request, nameof(request));\n\n        // Make sure we are not disposed of when someone tries to call us!\n#if NET\n        ObjectDisposedException.ThrowIf(_disposed, this);\n#else\n        if (_disposed) {\n            throw new ObjectDisposedException(nameof(RestClient));\n        }\n#endif\n        CombineInterceptors(request);\n        await OnBeforeRequest(request, cancellationToken).ConfigureAwait(false);\n        request.ValidateParameters();\n        var authenticator = request.Authenticator ?? Options.Authenticator;\n\n        if (authenticator != null) {\n            await authenticator.Authenticate(this, request, cancellationToken).ConfigureAwait(false);\n        }\n\n        var contentToDispose = new List<RequestContent>();\n        var initialContent   = new RequestContent(this, request);\n        contentToDispose.Add(initialContent);\n\n        var httpMethod = AsHttpMethod(request.Method);\n        var url        = new Uri(this.BuildUriString(request));\n","sourceCodeStart":84,"sourceCodeEnd":120,"githubUrl":"https://github.com/restsharp/RestSharp/blob/6a5082169257438cd085f822f050d93256a8e499/src/RestSharp/RestClient.Async.cs#L84-L120","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Do not dispose the RestClient while requests are still in flight or may still be issued.","Treat RestClient as a long-lived singleton (it wraps HttpClient, which is designed for reuse).","Remove the using-block around RestClient and dispose it only at application shutdown.","If using a shared HttpClient, pass it to RestClient with disposeHttpClient: false so lifecycle is controlled externally."],"exampleFix":"// before\nusing var client = new RestClient(\"https://api.example.com\");\n// ... later, after dispose, in a background task:\nawait client.ExecuteAsync(req); // throws ObjectDisposedException\n\n// after\nvar client = new RestClient(\"https://api.example.com\"); // long-lived\nawait client.ExecuteAsync(req);","handlingStrategy":"try-catch","validationCode":"// Structural prevention: keep RestClient alive for the app lifetime.\n// If you must check: RestSharp does not expose IsDisposed publicly,\n// so track disposal in your own wrapper.\npublic bool IsDisposed { get; private set; }\nprotected override void Dispose(bool disposing) { IsDisposed = true; base.Dispose(disposing); }","typeGuard":null,"tryCatchPattern":"try {\n    return await client.ExecuteAsync(request, ct);\n}\ncatch (ObjectDisposedException) {\n    // client was disposed mid-flight; recreate or surface a lifecycle error\n    client = CreateClient();\n    return await client.ExecuteAsync(request, ct);\n}","preventionTips":["Treat RestClient as a singleton; do not wrap it in a using-block around requests.","Pass an externally-owned HttpClient with disposeHttpClient: false if you manage its lifetime.","Cancel in-flight work before disposing during shutdown."],"tags":["dispose","lifecycle","httpclient","object-disposed"],"backgroundTag":null,"analyzedSha":"6a5082169257438cd085f822f050d93256a8e499","analyzedAt":"2026-08-13T20:38:25.807Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}