{"record":{"id":"cbd42584c95f5873","repo":"dotnet/reactive","slug":"value-cannot-be-null-parameter-resourcefactory","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'resourceFactory')","messagePattern":"Value cannot be null\\. \\(Parameter 'resourceFactory'\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Ix.NET/Source/System.Interactive/System/Linq/Operators/Using.cs","lineNumber":23,"sourceCode":"using System.Collections.Generic;\n\nnamespace System.Linq\n{\n    public static partial class EnumerableEx\n    {\n        /// <summary>\n        /// Generates a sequence that's dependent on a resource object whose lifetime is determined by the sequence usage\n        /// duration.\n        /// </summary>\n        /// <typeparam name=\"TSource\">Source element type.</typeparam>\n        /// <typeparam name=\"TResource\">Resource type.</typeparam>\n        /// <param name=\"resourceFactory\">Resource factory function.</param>\n        /// <param name=\"enumerableFactory\">Enumerable factory function, having access to the obtained resource.</param>\n        /// <returns>Sequence whose use controls the lifetime of the associated obtained resource.</returns>\n        public static IEnumerable<TSource> Using<TSource, TResource>(Func<TResource> resourceFactory, Func<TResource, IEnumerable<TSource>> enumerableFactory) where TResource : IDisposable\n        {\n            if (resourceFactory == null)\n                throw new ArgumentNullException(nameof(resourceFactory));\n            if (enumerableFactory == null)\n                throw new ArgumentNullException(nameof(enumerableFactory));\n\n            return UsingCore(resourceFactory, enumerableFactory);\n        }\n\n        private static IEnumerable<TSource> UsingCore<TSource, TResource>(Func<TResource> resourceFactory, Func<TResource, IEnumerable<TSource>> enumerableFactory) where TResource : IDisposable\n        {\n            using var res = resourceFactory();\n\n            foreach (var item in enumerableFactory(res))\n            {\n                yield return item;\n            }\n        }\n    }\n}\n","sourceCodeStart":5,"sourceCodeEnd":41,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Ix.NET/Source/System.Interactive/System/Linq/Operators/Using.cs#L5-L41","documentation":"Using ties a disposable resource's lifetime to the enumeration of a sequence: resourceFactory obtains the resource and it is disposed when enumeration completes. The factory delegate itself is validated eagerly, so passing a null resourceFactory throws ArgumentNullException naming `resourceFactory`.","triggerScenarios":"Calling `Using<TSource, IDisposable>(null, r => ...)` — e.g. forwarding a nullable delegate field, or a misconfigured factory method reference that is null at runtime.","commonSituations":"Dependency-injection scenarios where the resource factory is resolved from a container/registry that returned null, or dynamically composed pipelines where optional delegate hooks are left unset.","solutions":["Supply a real factory lambda: `Using(() => File.OpenText(path), reader => ...`.","Fix the DI/registry lookup so the factory delegate is registered and resolved.","Guard optional hooks: `factory ?? DefaultResourceFactory`.","If no resource is needed, don't call Using — just build the sequence directly."],"exampleFix":"// before\nvar seq = Using<int, FileStream>(config.ReaderFactory, fs => Read(fs));\n// after\nvar seq = Using<int, FileStream>(() => File.OpenRead(config.Path), fs => Read(fs));","handlingStrategy":"validation","validationCode":"if (resourceFactory is null) throw new ArgumentNullException(nameof(resourceFactory));","typeGuard":"static bool CanAcquire<TResource>(Func<TResource> f) where TResource : IDisposable => f is not null;","tryCatchPattern":"try { var seq = Using(resFactory, enumFactory); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"resourceFactory\") { /* fix DI/registration */ }","preventionTips":["Register resource factories in DI at startup and verify resolution.","Keep factory lambdas inline where possible to avoid null delegate fields.","Fail fast at composition time when optional factories are missing.","Prefer `using` blocks directly when the resource lifetime is simple."],"tags":["linq","null-argument","disposable","ix-interactive"],"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"}