{"record":{"id":"afaa82831a6c190f","repo":"AvaloniaUI/Avalonia","slug":"unable-to-locate-typeof-t","errorCode":null,"errorMessage":"Unable to locate '{typeof(T)}'.","messagePattern":"Unable to locate '(.+?)'\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Avalonia.Base/AvaloniaLocator.cs","lineNumber":140,"sourceCode":"        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":122,"sourceCodeEnd":145,"githubUrl":"https://github.com/AvaloniaUI/Avalonia/blob/11c542726898ae954a1ef668c65ec79ec92ab17d/src/Avalonia.Base/AvaloniaLocator.cs#L122-L145","documentation":"Thrown by the generic LocatorExtensions.GetRequiredService<T> when the resolver has no registration for typeof(T). Identical semantics to [88] but reached via the generic overload; the message includes the full type name of T.","triggerScenarios":"Calling AvaloniaLocator.Current.GetRequiredService<IFoo>() without prior registration; calling from a background thread or scope where the registration lives only in the root locator.","commonSituations":"Unit tests missing AppBuilder or locator setup; consuming an internal service that platform code registers lazily; calling a service before the app's compositor/render loop is up.","solutions":["Register the type before resolving: AvaloniaLocator.CurrentMutable.Bind<T>().ToSingleton<Impl>().","Use the nullable GetService<T>() when absence is acceptable.","Ensure resolution runs after the platform/app is initialized (e.g. inside a using var app = BuildAvaloniaApp().Setup()) ."],"exampleFix":"// before\nvar clock = AvaloniaLocator.Current.GetRequiredService<IGlobalClock>();\n\n// after\nAvaloniaLocator.CurrentMutable.Bind<IGlobalClock>().ToSingleton<GlobalClock>();\nvar clock = AvaloniaLocator.Current.GetRequiredService<IGlobalClock>();","handlingStrategy":"try-catch","validationCode":"var svc = AvaloniaLocator.Current.GetService<T>();\nif (svc is null) throw new InvalidOperationException($\"Register {typeof(T)} first\");","typeGuard":null,"tryCatchPattern":"T svc;\ntry { svc = AvaloniaLocator.Current.GetRequiredService<T>(); }\ncatch (InvalidOperationException) { svc = Fallback<T>(); }","preventionTips":["Register T before the first GetRequiredService<T> call.","Centralize locator setup in a single composition root."],"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"}