{"record":{"id":"7a66cfe08b966403","repo":"MahApps/MahApps.Metro","slug":"could-not-locate-any-instances-of-contract-typeof","errorCode":null,"errorMessage":"Could not locate any instances of contract {typeof(T)}.","messagePattern":"Could not locate any instances of contract (.+?)\\.","errorType":"exception","errorClass":"MahAppsException","httpStatus":null,"severity":"error","filePath":"src/MahApps.Metro.Samples/MahApps.Metro.Caliburn.Demo/Services/MefServiceLocator.cs","lineNumber":32,"sourceCode":"    {\n        private readonly CompositionContainer compositionContainer;\n\n        [ImportingConstructor]\n        public MefServiceLocator(CompositionContainer compositionContainer)\n        {\n            this.compositionContainer = compositionContainer;\n        }\n\n        public T? GetInstance<T>()\n            where T : class\n        {\n            try\n            {\n                return this.compositionContainer.GetExportedValue<T>();\n            }\n            catch (Exception exception)\n            {\n                throw new MahAppsException($\"Could not locate any instances of contract {typeof(T)}.\", exception);\n            }\n        }\n    }\n}","sourceCodeStart":14,"sourceCodeEnd":36,"githubUrl":"https://github.com/MahApps/MahApps.Metro/blob/72099e310bac2d12ac98fd7560b69679252519f5/src/MahApps.Metro.Samples/MahApps.Metro.Caliburn.Demo/Services/MefServiceLocator.cs#L14-L36","documentation":"MefServiceLocator.GetInstance<T>() calls the CompositionContainer's GetExportedValue<T>(), which throws when there is no matching export or when more than one exists (cardinality mismatch). The catch wraps the original exception in a MahAppsException naming the contract type T. This is a service-locator lookup failure surfaced through the MahApps exception type.","triggerScenarios":"GetInstance<T>() is called for a type T that has no [Export] of that contract, or has multiple exports when exactly one is required. The underlying CompositionContainer throws ImportCardinalityMismatchException (or CompositionException) and it is rewrapped here.","commonSituations":"A service used by MahApps samples is not exported, or two implementations are exported for the same interface causing ambiguity. The container was built from a catalog missing the exporting assembly. A type was renamed without updating its exports.","solutions":["Inspect the InnerException of the MahAppsException to see the exact MEF failure (no export vs. multiple exports).","Add exactly one [Export(typeof(T))] for the missing contract, or remove duplicate exports to resolve ambiguity."],"exampleFix":"// before: GetInstance<IColorService>() throws — no export\npublic class ColorService : IColorService { }\n// after\n[Export(typeof(IColorService))]\npublic class ColorService : IColorService { }","handlingStrategy":"try-catch","validationCode":"// Prefer GetExportedValues (plural) to avoid cardinality exceptions, then check count\nvar values = container.GetExportedValues<T>();\nif (values.Count() != 1) {\n    // 0 => missing export; >1 => ambiguous; handle explicitly instead of letting MEF throw\n}\nreturn values.SingleOrDefault();","typeGuard":null,"tryCatchPattern":"try {\n    return container.GetExportedValue<T>();\n} catch (ImportCardinalityMismatchException ex) {\n    throw new InvalidOperationException($\"No/multiple exports for {typeof(T)}\", ex);\n}","preventionTips":["Use GetExportedValues<T>() and count results to distinguish missing vs. duplicate exports.","Ensure exactly one [Export(typeof(T))] per service contract.","Keep a test that asserts each expected service resolves to a single instance.","Log the InnerException of MahAppsException to see the real MEF error."],"tags":["mef","di","service-locator","wpf","runtime"],"backgroundTag":null,"analyzedSha":"72099e310bac2d12ac98fd7560b69679252519f5","analyzedAt":"2026-08-13T20:19:34.419Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}