{"record":{"id":"471fd543fa9c122a","repo":"dotnet/wpf","slug":"sr-endinitwithoutbegininitnotsupported-resourcedictionary","errorCode":null,"errorMessage":"SR.EndInitWithoutBeginInitNotSupported","messagePattern":"SR\\.EndInitWithoutBeginInitNotSupported","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/ResourceDictionary.cs","lineNumber":1011,"sourceCode":"            }\n\n            IsInitializePending = true;\n            IsInitialized = false;\n        }\n\n        /// <summary>\n        ///     Fire Invalidation at the end of Init phase\n        /// </summary>\n        /// <remarks>\n        ///     BeginInit and EndInit follow a transaction model. BeginInit marks the\n        ///     dictionary uninitialized and EndInit marks it initialized.\n        /// </remarks>\n        public void EndInit()\n        {\n            // EndInit without a BeginInit isn't permitted\n            if (!IsInitializePending)\n            {\n                throw new InvalidOperationException(SR.EndInitWithoutBeginInitNotSupported);\n            }\n            Debug.Assert(!IsInitialized, \"Dictionary should not be initialized when EndInit is called\");\n\n            IsInitializePending = false;\n            IsInitialized = true;\n\n            // Fire Invalidations collectively for all changes made during the Init Phase\n            NotifyOwners(new ResourcesChangeInfo(null, this));\n        }\n\n        #endregion ISupportInitialize\n\n        #region DeferContent\n\n        private bool CanCache(KeyRecord keyRecord, object value)\n        {\n            if (keyRecord.SharedSet)\n            {","sourceCodeStart":993,"sourceCodeEnd":1029,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/ResourceDictionary.cs#L993-L1029","documentation":"ResourceDictionary.EndInit throws InvalidOperationException (SR.EndInitWithoutBeginInitNotSupported) when called without a prior BeginInit, since IsInitializePending is false. EndInit completes a batch initialization and requires the matching BeginInit.","triggerScenarios":"Calling EndInit on a ResourceDictionary that was never BeginInit-ed, or calling EndInit twice (the second call after IsInitializePending was cleared by the first).","commonSituations":"XAML loader/deferred-content helpers calling EndInit unconditionally; exception-recovery code that calls EndInit in a finally block even when BeginInit was never reached.","solutions":["Call BeginInit before EndInit, keeping the pair in the same method or try/finally scope.","Check IsInitializePending (or track your own flag) before invoking EndInit.","Only place EndInit in a finally block if BeginInit is guaranteed to have run first."],"exampleFix":"// before\ndictionary.EndInit(); // no BeginInit -> throws\n// after\ndictionary.BeginInit();\ntry { /* configure dictionary */ }\nfinally { dictionary.EndInit(); }","handlingStrategy":"try-catch","validationCode":"if (!dictionary.IsInitializePending) throw new InvalidOperationException(\"EndInit called without a matching BeginInit\");","typeGuard":"bool CanEndInit(ResourceDictionary rd) => rd.IsInitializePending;","tryCatchPattern":"try { dictionary.EndInit(); } catch (InvalidOperationException ex) when (ex.Message.Contains(\"BeginInit\")) { log.Warn(\"EndInit without BeginInit ignored\"); }","preventionTips":["Only call EndInit after your own code path executed BeginInit","Put EndInit in finally only when BeginInit is guaranteed to have run","Treat BeginInit/EndInit as a strict pairing contract and enforce it in a helper API"],"tags":["wpf","resourcedictionary","initialization"],"backgroundTag":"invalid-state-transition","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}