microsoft/aspire · error · InvalidOperationException
Expected KubernetesManifestResource but got
Error message
Expected KubernetesManifestResource but got {value?.GetType()}. What it means
WriteYaml of KubernetesManifestResourceYamlConverter expects the value being serialized to be exactly a KubernetesManifestResource. Any other type reaching this converter is an InvalidOperationException, indicating the converter was invoked for a type it does not handle (it only claims typeof(KubernetesManifestResource) in Accepts).
Solutions
- Pass the KubernetesManifestResource instance itself to the serializer, not a derived wrapper or unrelated object.
- Verify converter registration: KubernetesManifestResourceYamlConverter should be bound only to typeof(KubernetesManifestResource).
- If serializing related models, write dedicated converters for those types rather than reusing this one.
Example fix
// before serializer.Serialize(writer, manifestWrapper); // wrong object type // after serializer.Serialize(writer, kubernetesManifestResource);
Defensive patterns
Strategy: type-guard
Validate before calling
if (value is not KubernetesManifestResource) throw new ArgumentException("Serializer expects KubernetesManifestResource."); Type guard
static bool IsManifestResource(object? v) => v is KubernetesManifestResource;
Try / catch
try { yamlSerializer.Serialize(writer, value); } catch (InvalidOperationException ex) when (ex.Message.Contains("KubernetesManifestResource")) { /* pass the manifest resource itself */ } Prevention
- Serialize the KubernetesManifestResource instance itself, not wrappers
- Bind this converter only to typeof(KubernetesManifestResource)
- Cover manifest serialization with a snapshot test
When it happens
Trigger: Serializing an object graph where the converter is applied to a non-KubernetesManifestResource value — e.g. registering the converter globally and serializing other types, or passing a subclass/wrapper instead of the manifest resource (src/Aspire.Hosting.Kubernetes/Yaml/KubernetesManifestResourceYamlConverter.cs:26).
Common situations: Custom YamlDotNet pipelines that mis-route types; serializing a wrapper or projection of the manifest instead of the resource itself; reflection-based tooling that pulls the wrong object out of the app model.
Understand the failure class
Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.
Related errors
- Expected Int32OrStringV1 but got
- Command line args must be strings
- KubernetesManifestResource does not support YAML…
- The specified value is not an Int32.
- A ConfigureRadiusInfrastructure callback left container
AI-assisted analysis of microsoft/aspire@25830f84bd (2026-09-16).
Data as JSON: /api/errors/7ee106c1171b10c3.
Report an issue: GitHub.
Appendix: source
Thrown at src/Aspire.Hosting.Kubernetes/Yaml/KubernetesManifestResourceYamlConverter.cs:26
namespace Aspire.Hosting.Kubernetes.Yaml;
internal sealed class KubernetesManifestResourceYamlConverter : IYamlTypeConverter
{
public bool Accepts(Type type)
{
return type == typeof(KubernetesManifestResource);
}
public object? ReadYaml(IParser parser, Type type, ObjectDeserializer rootDeserializer)
{
throw new NotSupportedException($"{nameof(KubernetesManifestResource)} does not support YAML deserialization.");
}
public void WriteYaml(IEmitter emitter, object? value, Type type, ObjectSerializer serializer)
{
if (value is not KubernetesManifestResource manifest)
{
throw new InvalidOperationException($"Expected {nameof(KubernetesManifestResource)} but got {value?.GetType()}.");
}
emitter.Emit(new MappingStart());
WriteProperty("apiVersion", manifest.ApiVersion, serializer);
WriteProperty("kind", manifest.Kind, serializer);
WriteProperty("metadata", manifest.Metadata, serializer);
foreach (var (key, fieldValue) in manifest.Fields)
{
WriteProperty(key, fieldValue, serializer);
}
emitter.Emit(new MappingEnd());
}
private static void WriteProperty(string name, object? value, ObjectSerializer serializer)
{View on GitHub (pinned to 25830f84bd)