{"record":{"id":"ac7219774aa31529","repo":"lepoco/wpfui","slug":"nameof-serviceprovider-getservice-returned-nul","errorCode":null,"errorMessage":"{nameof(_serviceProvider.GetService)} returned null","messagePattern":"(.+?) returned null","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Wpf.Ui/Controls/NavigationView/NavigationView.Navigation.cs","lineNumber":317,"sourceCode":"\n        object? ComputeCachedNavigationInstance() => GetPageInstanceFromCache(viewItem.TargetPageType);\n    }\n\n    private object? GetPageInstanceFromCache(Type? targetPageType)\n    {\n        if (targetPageType is null)\n        {\n            return default;\n        }\n\n        if (_serviceProvider is not null)\n        {\n            System.Diagnostics.Debug.WriteLine(\n                $\"Getting {targetPageType} from cache using IServiceProvider.\"\n            );\n\n            return _serviceProvider.GetService(targetPageType)\n                ?? throw new InvalidOperationException(\n                    $\"{nameof(_serviceProvider.GetService)} returned null\"\n                );\n        }\n\n        if (_pageService is not null)\n        {\n            System.Diagnostics.Debug.WriteLine(\n                $\"Getting {targetPageType} from cache using INavigationViewPageProvider.\"\n            );\n\n            return _pageService.GetPage(targetPageType)\n                ?? throw new InvalidOperationException($\"{nameof(_pageService.GetPage)} returned null\");\n        }\n\n        System.Diagnostics.Debug.WriteLine($\"Getting {targetPageType} from cache using reflection.\");\n\n        return NavigationViewActivator.CreateInstance(targetPageType)\n            ?? throw new InvalidOperationException(\"Failed to create instance of the page\");","sourceCodeStart":299,"sourceCodeEnd":335,"githubUrl":"https://github.com/lepoco/wpfui/blob/ffebacd61058170cf63864b7d5aa730cffff848a/src/Wpf.Ui/Controls/NavigationView/NavigationView.Navigation.cs#L299-L335","documentation":"Thrown by NavigationView.GetPageInstanceFromCache (the cache-fill path) when an IServiceProvider is configured but GetService(targetPageType) returns null. This is the cache-layer twin of error 35: same root cause (page not registered with DI) but surfaced while populating the navigation cache rather than during the primary resolution attempt. Note the message is shorter and lacks the type name, which can make diagnosis harder.","triggerScenarios":"Cache mode is active for a NavigationViewItem, GetNavigationItemInstance delegated to GetPageInstanceFromCache, an IServiceProvider is set, and GetService returns null for the target page type.","commonSituations":"Page type missing from DI while NavigationCacheMode is enabled (so the cache factory path runs); transient registration not registered; composition root conditionally omitted the page.","solutions":["Register the page type with the IServiceProvider: services.AddTransient<MyPage>().","Verify the registered type matches TargetPageType exactly.","If using assembly scanning, ensure the page assembly is scanned.","Alternatively disable caching for that item or supply a page provider."],"exampleFix":"// before\nbuilder.Services.AddSingleton<MainWindow>();\n// MyPage missing -> throws on cached navigate\n\n// after\nbuilder.Services.AddTransient<MyPage>();","handlingStrategy":"validation","validationCode":"if (_serviceProvider.GetService(targetPageType) is null)\n    throw new InvalidOperationException(\n        $\"Page {targetPageType} not registered; cache cannot fill it.\");","typeGuard":"static bool IsPageRegistered(IServiceProvider sp, Type t) => sp.GetService(t) is not null;","tryCatchPattern":null,"preventionTips":["Register every page type with the DI container, especially cached ones.","Verify the registered type exactly equals TargetPageType.","Consider disabling NavigationCacheMode for pages not in DI."],"tags":["wpf","navigationview","dependency-injection","cache","configuration"],"backgroundTag":null,"analyzedSha":"ffebacd61058170cf63864b7d5aa730cffff848a","analyzedAt":"2026-08-13T21:36:01.370Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}