MahApps/MahApps.Metro · error · MahAppsException
Could not locate any instances of contract {typeof(T)}.
Error message
Could not locate any instances of contract {typeof(T)}. What it means
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.
Source
Thrown at src/MahApps.Metro.Samples/MahApps.Metro.Caliburn.Demo/Services/MefServiceLocator.cs:32
{
private readonly CompositionContainer compositionContainer;
[ImportingConstructor]
public MefServiceLocator(CompositionContainer compositionContainer)
{
this.compositionContainer = compositionContainer;
}
public T? GetInstance<T>()
where T : class
{
try
{
return this.compositionContainer.GetExportedValue<T>();
}
catch (Exception exception)
{
throw new MahAppsException($"Could not locate any instances of contract {typeof(T)}.", exception);
}
}
}
}View on GitHub (pinned to 72099e310b)
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.
Example fix
// before: GetInstance<IColorService>() throws — no export
public class ColorService : IColorService { }
// after
[Export(typeof(IColorService))]
public class ColorService : IColorService { } Defensive patterns
Strategy: try-catch
Validate before calling
// Prefer GetExportedValues (plural) to avoid cardinality exceptions, then check count
var values = container.GetExportedValues<T>();
if (values.Count() != 1) {
// 0 => missing export; >1 => ambiguous; handle explicitly instead of letting MEF throw
}
return values.SingleOrDefault(); Try / catch
try {
return container.GetExportedValue<T>();
} catch (ImportCardinalityMismatchException ex) {
throw new InvalidOperationException($"No/multiple exports for {typeof(T)}", ex);
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Could not locate any instances of contract {contract}.
- Failed to set the window state for {window} from the setting
- Uups, it seems like there is something wrong with the given
- Cannot convert the given input to a valid color
- The inverse dialog theme only works if the window theme abid
AI-assisted analysis of MahApps/MahApps.Metro@72099e310b (2026-08-13).
Data as JSON: /api/errors/7a66cfe08b966403.
Report an issue: GitHub.