{"record":{"id":"e449755a0d9adbcf","repo":"microsoft/aspire","slug":"the-specified-value-is-not-an-int32","errorCode":null,"errorMessage":"The specified value is not an Int32.","messagePattern":"The specified value is not an Int32\\.","errorType":"exception","errorClass":"InvalidCastException","httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting.Kubernetes/Resources/Int32OrStringV1.cs","lineNumber":70,"sourceCode":"    /// <param name=\"other\">The string to compare with.</param>\n    /// <returns>True if the current instance is equal to the other string; otherwise, false.</returns>\n    public bool Equals(string? other) =>\n        Text == other;\n\n    /// <summary>\n    /// Returns a string representation of the current instance.\n    /// </summary>\n    /// <returns>The string representation of the value.</returns>\n    public override string? ToString() => Value;\n\n    /// <summary>\n    /// Gets the value as a 32-bit integer.\n    /// </summary>\n    /// <param name=\"value\">The value to get.</param>\n    /// <returns></returns>\n    /// <exception cref=\"InvalidCastException\">Thrown if the value isn't a valid integer.</exception>\n    public static explicit operator int(Int32OrStringV1 value) =>\n        value.Number ?? throw new InvalidCastException(\"The specified value is not an Int32.\");\n\n    /// <summary>\n    /// Gets the value as a string.\n    /// </summary>\n    /// <param name=\"value\">The value to get.</param>\n    /// <returns>The value as a string.</returns>\n    public static explicit operator string?(Int32OrStringV1? value) =>\n        value?.Text ?? value?.Number?.ToString(CultureInfo.InvariantCulture);\n\n    /// <summary>\n    /// Gets the integer value as a Int32OrStringV1 instance.\n    /// </summary>\n    /// <param name=\"value\">The integer to get.</param>\n    /// <returns>An Int32OrStringV1 instance representing the integer value.</returns>\n    public static implicit operator Int32OrStringV1(int value)\n    {\n        return new(value);\n    }","sourceCodeStart":52,"sourceCodeEnd":88,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting.Kubernetes/Resources/Int32OrStringV1.cs#L52-L88","documentation":"Thrown by the explicit operator int on Int32OrStringV1 (used for Kubernetes quantity-like values that can be either a number or a string) when the underlying value is a string rather than an int. The explicit cast is documented to throw InvalidCastException if the value isn't a valid integer.","triggerScenarios":"Explicitly casting (int)someInt32OrStringV1 when the value was constructed from a string (e.g. Int32OrStringV1.Parse(\"6379\") with a non-numeric or string-held value), such as a port value that was specified as a string.","commonSituations":"Ports or quantities configured from config files/user input where the value came in as a string; casting after reading a value that the K8s model intentionally allows as a named string (e.g. port names); copying code that assumed the numeric form.","solutions":["Check value.Number.HasValue before casting, or read the string form instead","Use the string cast/property when the value may be non-numeric","Parse the underlying string to int with int.TryParse when a numeric interpretation is needed","Ensure the value was constructed with the integer overload if an int is required downstream"],"exampleFix":"// before\nint port = (int)value; // throws if value is a string\n// after\nint port = value.Number ?? int.Parse(value.String, CultureInfo.InvariantCulture);","handlingStrategy":"type-guard","validationCode":"if (!value.Number.HasValue) throw new InvalidOperationException($\"Expected numeric value, got string '{value.String}'\");","typeGuard":"static bool TryAsInt(Int32OrStringV1 v, out int i) { i = v.Number ?? default; return v.Number.HasValue; }","tryCatchPattern":"try { int port = (int)value; }\ncatch (InvalidCastException) { /* read the string form or parse it */ }","preventionTips":["Check Number.HasValue before explicit int casts","Prefer the string accessor when values may be non-numeric","Validate config-driven values parse as int when int is required"],"tags":["kubernetes","casting","int32orstring","type-mismatch"],"backgroundTag":"type-mismatch","analyzedSha":"25830f84bd145686607ad00c057b3f84e2e51d43","analyzedAt":"2026-09-16T11:10:06.193Z","contentChangedAt":"2026-09-16T11:10:06.193Z","schemaVersion":2},"datasetVersion":"2026-09-21T04:17:39.646Z"}