dotnet/wpf · error · ArgumentException
' ' ContentType is not valid.
Error message
'{0}' ContentType is not valid. What it means
GeneratePart validates the supplied ContentType after the writer check: a null or zero-length content type string is rejected with ArgumentException (SR.ReachPackaging_InvalidContentType) formatted with the invalid value. The library requires a well-formed, non-empty MIME content type for every package part.
Solutions
- Pass a valid non-empty MIME type such as "application/vnd.ms-package.obfuscated-opentype" or "application/vnd.ms-printing.printticket".
- Validate contentType.ToString().Length > 0 before calling GeneratePart.
- Fix the configuration/mapping that produced an empty content type string.
- If the content type is unknown, use one of the constants exposed by XpsS0Markup instead of constructing a bare ContentType.
Example fix
// before
var ct = new ContentType(config["PartContentType"] ?? "");
var part = xpsManager.GeneratePart(partUri, ct, comp);
// after
string ctString = config["PartContentType"];
if (string.IsNullOrEmpty(ctString)) throw new ArgumentException("PartContentType must be a non-empty MIME type");
var part = xpsManager.GeneratePart(partUri, new ContentType(ctString), comp); Defensive patterns
Strategy: validation
Validate before calling
if (contentType is null) throw new ArgumentNullException(nameof(contentType));
if (string.IsNullOrWhiteSpace(contentType.ToString())) throw new ArgumentException("Content type must be non-empty", nameof(contentType)); Type guard
bool IsValidContentType(ContentType ct) => ct != null && !string.IsNullOrEmpty(ct.ToString());
Try / catch
try { return xpsManager.GeneratePart(partUri, contentType, option); }
catch (ArgumentException ex) when (ex.ParamName == nameof(contentType)) { /* fix/derive a valid MIME type */ throw new ArgumentException($"Invalid content type: {contentType}", ex); } Prevention
- Use the XpsS0Markup content type constants instead of ad-hoc strings.
- Validate any config-driven content type mapping at startup.
- Never construct ContentType from values that may be null/empty without checks.
When it happens
Trigger: Calling GeneratePart (directly or via GenerateUniquePart/printTicketPart paths) with a contentType whose ToString() yields an empty string, e.g. new ContentType("") or a ContentType constructed from an empty/null-derived string.
Common situations: Building content types dynamically from config or file extension mapping where the mapping returns an empty string; copying ContentType values from data that lost its value; typo causing an empty constant to be passed.
Understand the failure class
Background: "must not be empty", "cannot be empty" — required-field validation errors across open-source libraries — this error's family across 41 libraries.
Related errors
- String passed for type is not valid.
- " }} " element found. Expected fixed page element ( }} ).
- PackagePart URI does not correspond to a FixedDocument.
- PackagePart URI does not correspond to a FixedPage.
- SR.DigitalSignatureNoFixedDocumentSequence
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/325ab99d75ea0279.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/ReachFramework/Packaging/XpsManager.cs:300
/// Generate a unique Part for the content type and add it to the package.
/// Adding to any relationships or selector/sequence markup is not done.
/// </summary>
public
PackagePart
GeneratePart(
ContentType contentType,
Uri partUri
)
{
ObjectDisposedException.ThrowIf(_metroPackage is null, typeof(XpsManager));
if (!IsWriter)
{
throw new XpsPackagingException(SR.ReachPackaging_OnlyWriters);
}
ArgumentNullException.ThrowIfNull(contentType);
if (0 == contentType.ToString().Length)
{
throw new ArgumentException(SR.Format(SR.ReachPackaging_InvalidContentType, contentType), nameof(contentType));
}
//Do not compress image Content Types
CompressionOption compressionOption = _compressionOption;
if (contentType.AreTypeAndSubTypeEqual(XpsS0Markup.JpgContentType) ||
contentType.AreTypeAndSubTypeEqual(XpsS0Markup.PngContentType) ||
contentType.AreTypeAndSubTypeEqual(XpsS0Markup.TifContentType) ||
contentType.AreTypeAndSubTypeEqual(XpsS0Markup.WdpContentType))
{
compressionOption = CompressionOption.NotCompressed;
}
PackagePart metroPart = _metroPackage.CreatePart(partUri,
contentType.ToString(),
compressionOption);
//View on GitHub (pinned to 81131a70a4)