{"record":{"id":"fb232f4a7651bc59","repo":"dotnet/reactive","slug":"value-cannot-be-null-parameter-context-contextdisposable","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'context')","messagePattern":"Value cannot be null\\. \\(Parameter 'context'\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Rx.NET/Source/src/System.Reactive/Disposables/ContextDisposable.cs","lineNumber":25,"sourceCode":"\nnamespace System.Reactive.Disposables\n{\n    /// <summary>\n    /// Represents a disposable resource whose disposal invocation will be posted to the specified <seealso cref=\"SynchronizationContext\"/>.\n    /// </summary>\n    public sealed class ContextDisposable : ICancelable\n    {\n        private volatile IDisposable _disposable;\n\n        /// <summary>\n        /// Initializes a new instance of the <see cref=\"ContextDisposable\"/> class that uses the specified <see cref=\"SynchronizationContext\"/> on which to dispose the specified disposable resource.\n        /// </summary>\n        /// <param name=\"context\">Context to perform disposal on.</param>\n        /// <param name=\"disposable\">Disposable whose Dispose operation to run on the given synchronization context.</param>\n        /// <exception cref=\"ArgumentNullException\"><paramref name=\"context\"/> or <paramref name=\"disposable\"/> is null.</exception>\n        public ContextDisposable(SynchronizationContext context, IDisposable disposable)\n        {\n            Context = context ?? throw new ArgumentNullException(nameof(context));\n            _disposable = disposable ?? throw new ArgumentNullException(nameof(disposable));\n        }\n\n        /// <summary>\n        /// Gets the provided <see cref=\"SynchronizationContext\"/>.\n        /// </summary>\n        public SynchronizationContext Context { get; }\n\n        /// <summary>\n        /// Gets a value that indicates whether the object is disposed.\n        /// </summary>\n        public bool IsDisposed => _disposable == BooleanDisposable.True;\n\n        /// <summary>\n        /// Disposes the underlying disposable on the provided <see cref=\"SynchronizationContext\"/>.\n        /// </summary>\n        public void Dispose()\n        {","sourceCodeStart":7,"sourceCodeEnd":43,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Rx.NET/Source/src/System.Reactive/Disposables/ContextDisposable.cs#L7-L43","documentation":"The ContextDisposable constructor throws ArgumentNullException when the context argument is null. ContextDisposable captures a SynchronizationContext on which Dispose will later be posted, so a null context would make the disposal behavior undefined. The constructor uses the ?? throw idiom to fail fast.","triggerScenarios":"new ContextDisposable(null, someDisposable), typically when SynchronizationContext.Current is null (e.g. on a threadpool/console thread) and was passed through blindly.","commonSituations":"Capturing SynchronizationContext.Current in library code running without an installed context (console apps, ASP.NET Core, threadpool work items); UI code that lost its dispatcher context.","solutions":["Pass an explicit context (e.g. a captured UI SynchronizationContext) instead of relying on SynchronizationContext.Current.","Guard before constructing: var ctx = SynchronizationContext.Current ?? new SynchronizationContext();","If no context is needed, use a plain disposable (Disposable.Create) instead of ContextDisposable."],"exampleFix":"// before\nvar d = new ContextDisposable(SynchronizationContext.Current, resource);\n// after\nvar ctx = SynchronizationContext.Current ?? new SynchronizationContext();\nvar d = new ContextDisposable(ctx, resource);","handlingStrategy":"validation","validationCode":"var ctx = SynchronizationContext.Current ?? new SynchronizationContext();\nvar d = new ContextDisposable(ctx, resource);","typeGuard":"ContextDisposable TryCreate(SynchronizationContext ctx, IDisposable d) => ctx == null || d == null ? null : new ContextDisposable(ctx, d);","tryCatchPattern":"try { var d = new ContextDisposable(context, resource); } catch (ArgumentNullException ex) when (ex.ParamName == \"context\") { /* fall back to plain Disposable.Create */ }","preventionTips":["Never rely on SynchronizationContext.Current being non-null on threadpool/console threads","Capture the UI context explicitly at app startup","Use ContextDisposable only when a real sync context exists"],"tags":["argument-null","synchronization-context","constructor","rx"],"backgroundTag":"null-argument","analyzedSha":"94b5d5ab912789f5abe9a72138a25bbd716fe59c","analyzedAt":"2026-09-15T02:26:24.759Z","contentChangedAt":"2026-09-15T02:26:24.759Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}