{"record":{"id":"ed1334f9c57bbc3f","repo":"microsoft/ailab","slug":"blobclient-is-null","errorCode":null,"errorMessage":"blobClient is null","messagePattern":"blobClient is null","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"Sketch2Code/Sketch2Code.Core/Services/ObjectDetectionAppService.cs","lineNumber":200,"sourceCode":"            using (var ms = new MemoryStream(data))\n            {\n                img = Image.FromStream(ms);\n\n                imageWidth = img.Width;\n                imageHeight = img.Height;\n\n                if ((imageWidth == 0) || (imageHeight == 0))\n                {\n                    throw new InvalidOperationException(\"Invalid image dimensions\");\n                }\n            }\n\n            return img;\n        }\n\n        public async Task SaveResults(IList<PredictedObject> predictedObjects, string id)\n        {\n            if (_cloudBlobClient == null) throw new InvalidOperationException(\"blobClient is null\");\n            var slices_container = $\"{id}/slices\";\n\n            for (int i = 0; i < predictedObjects.Count; i++)\n            {\n                PredictedObject result = (PredictedObject)predictedObjects[i];\n                await this.SaveResults(result.SlicedImage, slices_container, $\"{result.Name}.png\");\n            }\n        }\n        public async Task SaveResults(byte[] file, string container, string fileName)\n        {\n            CloudBlobContainer theContainer = null;\n\n            if (_cloudBlobClient == null) throw new InvalidOperationException(\"blobClient is null\");\n\n            var segments = container.Split(@\"/\".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);\n\n            var rootContainerPath = segments.First();\n","sourceCodeStart":182,"sourceCodeEnd":218,"githubUrl":"https://github.com/microsoft/ailab/blob/89fe2fc62081145ec5408d42059cbcb7c4a033c8/Sketch2Code/Sketch2Code.Core/Services/ObjectDetectionAppService.cs#L182-L218","documentation":"SaveResults(IList<PredictedObject>, string) writes each predicted object's sliced image to Azure Blob Storage using the injected CloudBlobClient. If that client was never initialized (null), no blob operations are possible, so the method throws InvalidOperationException('blobClient is null') before doing any work.","triggerScenarios":"Invoking Run/saveResults on an ObjectDetectionAppService instance whose _cloudBlobClient field was never assigned — e.g. constructed without the blob connection string or a failed initialization path.","commonSituations":"Missing Azure Storage connection string in config so the factory method never creates the client; constructing the service manually instead of via DI/factory; initialization step skipped in a new hosting path.","solutions":["Ensure ObjectDetectionAppService is constructed/initialized with a valid CloudBlobClient (pass the Azure Storage connection string)","Check the storage connection string configuration value is present and valid","Construct the service via the factory/DI path used by Run rather than new ObjectDetectionAppService() with defaults","Null-check _cloudBlobClient at construction time and fail fast with a clear configuration error"],"exampleFix":"// before\nvar service = new ObjectDetectionAppService();\nawait service.SaveResults(objects, id);\n// after\nvar service = ObjectDetectionAppService.Create(storageConnectionString);\nawait service.SaveResults(objects, id);","handlingStrategy":"try-catch","validationCode":"if (service.BlobClient == null)\n    throw new InvalidOperationException(\"ObjectDetectionAppService not initialized with a CloudBlobClient\");","typeGuard":null,"tryCatchPattern":"try\n{\n    await service.SaveResults(predictedObjects, id);\n}\ncatch (InvalidOperationException ex) when (ex.Message == \"blobClient is null\")\n{\n    logger.LogError(ex, \"Blob storage client not initialized; check storage connection string\");\n    throw;\n}","preventionTips":["Always construct the service through the factory that creates CloudBlobClient","Assert the storage connection string exists at startup","Never bypass the DI/factory initialization path","Add a unit test asserting SaveResults works with an initialized client"],"tags":["csharp","azure-blob-storage","null-reference","sketch2code"],"backgroundTag":"null-argument","analyzedSha":"89fe2fc62081145ec5408d42059cbcb7c4a033c8","analyzedAt":"2026-09-13T20:10:53.835Z","contentChangedAt":"2026-09-13T20:10:53.835Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}