dotnet/wpf · error · ArgumentException
throw new…
Error message
throw new ArgumentException(SR.ContentTypeCannotHaveLeadingTrailingLWS);
What it means
The ContentType constructor parses an RFC-style media type string (e.g. for System.IO.Packaging). It rejects content types that begin or end with linear white space (spaces, tabs, CR/LF), throwing ArgumentException because leading/trailing LWS makes the value structurally invalid.
Solutions
- Trim the string before constructing: contentType.Trim().
- Validate the input starts with the type token and ends with a parameter value or subtype.
- Check the source (config, XML attribute) for accidental whitespace or line wrapping.
Example fix
// before var ct = new ContentType(userValue); // after var ct = new ContentType(userValue.Trim());
Defensive patterns
Strategy: validation
Validate before calling
public static bool IsValidContentTypeRaw(string s) =>
!string.IsNullOrEmpty(s) &&
!char.IsWhiteSpace(s[0]) && !char.IsWhiteSpace(s[s.Length - 1]); Try / catch
try { var ct = new ContentType(value); }
catch (ArgumentException ex) when (ex.Message.Contains("white space"))
{ value = value.Trim(); /* retry */ } Prevention
- Always .Trim() content-type strings sourced from config, XML, or user input.
- Reject values containing any linear white space at the boundaries at the input boundary.
When it happens
Trigger: new ContentType(" text/plain"), new ContentType("text/plain "), or strings read from config/files with stray whitespace; also any string whose first or last char is \r, \n, \t, or space.
Common situations: Hand-written content types in PackagePart.CreatePart calls, values parsed from .rels or [Content_Types].xml with formatting, trimming forgotten on user/config input.
Understand the failure class
Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.
Related errors
- Cannot have leading path delimiter.
- CompoundFile path must be non-empty.
- FileMode value is not valid.
- SR.CompoundFilePathNullEmpty
- SR.DataSpaceLabelInvalidEmpty
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/ba397432dbac956b.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/WindowsBase/MS/Internal/ContentType.cs:82
/// we used more as an indication of an absent/unknown ContentType.
/// </summary>
/// <param name="contentType">content-type</param>
/// <exception cref="ArgumentNullException">If the contentType parameter is null</exception>
/// <exception cref="ArgumentException">If the contentType string has leading or
/// trailing Linear White Spaces(LWS) characters</exception>
/// <exception cref="ArgumentException">If the contentType string invalid CR-LF characters</exception>
internal ContentType(string contentType)
{
ArgumentNullException.ThrowIfNull(contentType);
if (contentType.Length == 0)
{
_contentType = String.Empty;
}
else
{
if (IsLinearWhiteSpaceChar(contentType[0]) || IsLinearWhiteSpaceChar(contentType[contentType.Length - 1]))
throw new ArgumentException(SR.ContentTypeCannotHaveLeadingTrailingLWS);
//Carriage return can be expressed as '\r\n' or '\n\r'
//We need to make sure that a \r is accompanied by \n
ValidateCarriageReturns(contentType);
//Begin Parsing
int semiColonIndex = contentType.IndexOf(_semicolonSeparator);
if (semiColonIndex == -1)
{
// Parse content type similar to - type/subtype
ParseTypeAndSubType(contentType);
}
else
{
// Parse content type similar to - type/subtype ; param1=value1 ; param2=value2 ; param3="value3"
ParseTypeAndSubType(contentType.AsSpan(0, semiColonIndex));
ParseParameterAndValue(contentType.AsSpan(semiColonIndex));View on GitHub (pinned to 81131a70a4)