MahApps/MahApps.Metro · critical · Exception

Could not locate any instances of contract {contract}.

Error message

Could not locate any instances of contract {contract}.

What it means

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.

Source

Thrown at src/MahApps.Metro.Samples/MahApps.Metro.Caliburn.Demo/AppBootstrapper.cs:67

            this.container.Compose(batch);
        }

        protected override IEnumerable<object> GetAllInstances(Type serviceType)
        {
            return this.container?.GetExportedValues<object>(AttributedModelServices.GetContractName(serviceType)) ?? Enumerable.Empty<object>();
        }

        protected override object GetInstance(Type serviceType, string key)
        {
            var contract = string.IsNullOrEmpty(key) ? AttributedModelServices.GetContractName(serviceType) : key;
            var export = this.container?.GetExportedValues<object>(contract).FirstOrDefault();

            if (export is not null)
            {
                return export;
            }

            throw new Exception($"Could not locate any instances of contract {contract}.");
        }

        protected override async void OnStartup(object sender, StartupEventArgs e)
        {
            var startupTasks = this.GetAllInstances(typeof(StartupTask))
                                   .OfType<ExportedDelegate>()
                                   .Select(exportedDelegate => (StartupTask)exportedDelegate.CreateDelegate(typeof(StartupTask))!);

            startupTasks.Apply(s => s());

            await this.DisplayRootViewForAsync<IShell>();
        }
    }
}

View on GitHub (pinned to 72099e310b)

Solutions

  1. 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).
  2. Ensure the assembly that contains the export is registered in AssemblySource.Instance / included in the AggregateCatalog so MEF can discover it.
  3. Check for typos between the requested contract (interface or key) and the exported contract; they must match exactly.
  4. Rebuild to make sure the exporting assembly is up to date and present in the output directory.

Example fix

// before: no export for IShell
public class ShellViewModel : Conductor<object>, IShell { }
// after
[Export(typeof(IShell))]
public class ShellViewModel : Conductor<object>, IShell { }
Defensive patterns

Strategy: validation

Validate before calling

// Before resolving, assert an export exists for the contract
var contract = AttributedModelServices.GetContractName(serviceType);
var export = container?.GetExportedValues<object>(contract).FirstOrDefault();
if (export is null) {
    throw new InvalidOperationException(
        $"No MEF export found for contract '{contract}'. " +
        $"Add [Export(typeof({serviceType.Name}))] and ensure the assembly is in AssemblySource.");
}

Try / catch

try {
    var instance = container.GetExportedValues<object>(contract).FirstOrDefault();
} catch (CompositionException ex) {
    // log which part/contract failed, fail fast with actionable detail
    throw new InvalidOperationException($"MEF resolution failed for {contract}", ex);
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of MahApps/MahApps.Metro@72099e310b (2026-08-13). Data as JSON: /api/errors/d8ddcc656cfed9d0. Report an issue: GitHub.