{"record":{"id":"2b2480ece765a716","repo":"AvaloniaUI/Avalonia","slug":"unable-to-locate-t","errorCode":null,"errorMessage":"Unable to locate '{t}'.","messagePattern":"Unable to locate '(.+?)'\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Avalonia.Base/AvaloniaLocator.cs","lineNumber":135,"sourceCode":"    }\n\n    [PrivateApi]\n    public interface IAvaloniaDependencyResolver\n    {\n        object? GetService(Type t);\n    }\n\n    [PrivateApi]\n    public static class LocatorExtensions\n    {\n        public static T? GetService<T>(this IAvaloniaDependencyResolver resolver)\n        {\n            return (T?) resolver.GetService(typeof (T));\n        }\n\n        public static object GetRequiredService(this IAvaloniaDependencyResolver resolver, Type t)\n        {\n            return resolver.GetService(t) ?? throw new InvalidOperationException($\"Unable to locate '{t}'.\");\n        }\n\n        public static T GetRequiredService<T>(this IAvaloniaDependencyResolver resolver)\n        {\n            return (T?)resolver.GetService(typeof(T)) ?? throw new InvalidOperationException($\"Unable to locate '{typeof(T)}'.\");\n        }\n    }\n}\n\n","sourceCodeStart":117,"sourceCodeEnd":145,"githubUrl":"https://github.com/AvaloniaUI/Avalonia/blob/11c542726898ae954a1ef668c65ec79ec92ab17d/src/Avalonia.Base/AvaloniaLocator.cs#L117-L145","documentation":"Thrown by LocatorExtensions.GetRequiredService(Type) when the dependency resolver returns null for the requested type. AvaloniaLocator is a minimal service locator; a null result means no registration (ToConstant/ToFunc/ToSingleton/ToTransient) exists for that type in the current scope or its parent scope.","triggerScenarios":"Calling AvaloniaLocator.Current.GetRequiredService(typeof(IFoo)) before registering IFoo; calling it after EnterScope() was disposed; misspelling the service type.","commonSituations":"Headless/test environments where platform services weren't registered; accessing a service during static initialization before the locator is configured; version upgrades that renamed a service type.","solutions":["Register the service first: AvaloniaLocator.CurrentMutable.Bind<IFoo>().ToConstant impl).","If the service is optional, use GetService<T>() (returns null) instead of GetRequiredService.","Verify registration order in AppBuilder or test setup; ensure the call happens after Build()/Initialize()."],"exampleFix":"// before\nvar platform = AvaloniaLocator.Current.GetRequiredService(typeof(IPlatformRenderInterface));\n\n// after\n// register in test/headless setup first\nAvaloniaLocator.CurrentMutable.Bind<IPlatformRenderInterface>().ToConstant(renderInterface);\nvar platform = AvaloniaLocator.Current.GetRequiredService(typeof(IPlatformRenderInterface));","handlingStrategy":"try-catch","validationCode":"var svc = AvaloniaLocator.Current.GetService(t);\nif (svc is null) throw new InvalidOperationException($\"Register {t} first\");","typeGuard":null,"tryCatchPattern":"object svc;\ntry { svc = AvaloniaLocator.Current.GetRequiredService(t); }\ncatch (InvalidOperationException) { svc = CreateFallback(t); }","preventionTips":["Register services in AppBuilder/test setup before resolving.","Use GetService (nullable) when absence is expected."],"tags":["di","locator","invalid-operation","configuration"],"backgroundTag":null,"analyzedSha":"11c542726898ae954a1ef668c65ec79ec92ab17d","analyzedAt":"2026-08-13T11:57:40.261Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}