{"record":{"id":"d8ddcc656cfed9d0","repo":"MahApps/MahApps.Metro","slug":"could-not-locate-any-instances-of-contract-contra","errorCode":null,"errorMessage":"Could not locate any instances of contract {contract}.","messagePattern":"Could not locate any instances of contract (.+?)\\.","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"critical","filePath":"src/MahApps.Metro.Samples/MahApps.Metro.Caliburn.Demo/AppBootstrapper.cs","lineNumber":67,"sourceCode":"            this.container.Compose(batch);\n        }\n\n        protected override IEnumerable<object> GetAllInstances(Type serviceType)\n        {\n            return this.container?.GetExportedValues<object>(AttributedModelServices.GetContractName(serviceType)) ?? Enumerable.Empty<object>();\n        }\n\n        protected override object GetInstance(Type serviceType, string key)\n        {\n            var contract = string.IsNullOrEmpty(key) ? AttributedModelServices.GetContractName(serviceType) : key;\n            var export = this.container?.GetExportedValues<object>(contract).FirstOrDefault();\n\n            if (export is not null)\n            {\n                return export;\n            }\n\n            throw new Exception($\"Could not locate any instances of contract {contract}.\");\n        }\n\n        protected override async void OnStartup(object sender, StartupEventArgs e)\n        {\n            var startupTasks = this.GetAllInstances(typeof(StartupTask))\n                                   .OfType<ExportedDelegate>()\n                                   .Select(exportedDelegate => (StartupTask)exportedDelegate.CreateDelegate(typeof(StartupTask))!);\n\n            startupTasks.Apply(s => s());\n\n            await this.DisplayRootViewForAsync<IShell>();\n        }\n    }\n}","sourceCodeStart":49,"sourceCodeEnd":81,"githubUrl":"https://github.com/MahApps/MahApps.Metro/blob/72099e310bac2d12ac98fd7560b69679252519f5/src/MahApps.Metro.Samples/MahApps.Metro.Caliburn.Demo/AppBootstrapper.cs#L49-L81","documentation":"In the Caliburn.Micro demo bootstrapper, GetInstance overrides the framework's service resolution. It derives a MEF contract name from the requested type (or uses the supplied key) and asks the CompositionContainer for an exported value. If no export matches the contract it throws a generic Exception listing the missing contract. This is the standard 'no MEF export registered' failure for Caliburn.Micro + MEF apps.","triggerScenarios":"Caliburn.Micro calls GetInstance(serviceType, key) (view-model activation, conductor resolution, service location) and the MEF container has zero exports whose [Export] contract equals the derived name. Happens at app startup when resolving the shell or any imported part.","commonSituations":"A view-model or service class is missing the [Export(typeof(...))] attribute, or exports the wrong contract. The assembly containing the export was not added to AssemblySource (so the AggregateCatalog never sees it). A rename changed the type but not the contract key. Running an incomplete build that didn't compile the exporting assembly.","solutions":["Add the correct MEF [Export(...)] attribute on the type whose contract name appears in the message (use [Export(typeof(IShell))] rather than a bare [Export] when an interface contract is expected).","Ensure the assembly that contains the export is registered in AssemblySource.Instance / included in the AggregateCatalog so MEF can discover it.","Check for typos between the requested contract (interface or key) and the exported contract; they must match exactly.","Rebuild to make sure the exporting assembly is up to date and present in the output directory."],"exampleFix":"// before: no export for IShell\npublic class ShellViewModel : Conductor<object>, IShell { }\n// after\n[Export(typeof(IShell))]\npublic class ShellViewModel : Conductor<object>, IShell { }","handlingStrategy":"validation","validationCode":"// Before resolving, assert an export exists for the contract\nvar contract = AttributedModelServices.GetContractName(serviceType);\nvar export = container?.GetExportedValues<object>(contract).FirstOrDefault();\nif (export is null) {\n    throw new InvalidOperationException(\n        $\"No MEF export found for contract '{contract}'. \" +\n        $\"Add [Export(typeof({serviceType.Name}))] and ensure the assembly is in AssemblySource.\");\n}","typeGuard":null,"tryCatchPattern":"try {\n    var instance = container.GetExportedValues<object>(contract).FirstOrDefault();\n} catch (CompositionException ex) {\n    // log which part/contract failed, fail fast with actionable detail\n    throw new InvalidOperationException($\"MEF resolution failed for {contract}\", ex);\n}","preventionTips":["Decorate every view-model/service with [Export(typeof(TheInterface))] using the interface contract.","Ensure exporting assemblies are added to AssemblySource.Instance / the AggregateCatalog.","Write a startup composition test asserting at least one export exists for each expected contract.","Use contract interfaces consistently; avoid mixing type-name and string contracts."],"tags":["caliburn-micro","mef","di","wpf","runtime"],"backgroundTag":null,"analyzedSha":"72099e310bac2d12ac98fd7560b69679252519f5","analyzedAt":"2026-08-13T20:19:34.419Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}