dotnet/wpf · error · ArgumentException
String passed for type is not valid.
Error message
String passed for type is not valid.
What it means
GenerateUniquePart validates that the ContentType is not ContentType.Empty; passing an empty type throws ArgumentException with SR.ReachPackaging_InvalidType ("String passed for type is not valid."). Unlike GeneratePart this checks for the empty sentinel rather than merely zero length.
Solutions
- Pass a concrete MIME ContentType, e.g. new ContentType("application/vnd.ms-printing.printticket") for print tickets or the image type for thumbnails.
- Compare against ContentType.Empty before calling and substitute a proper type or abort early.
- Fix upstream parsing that yields ContentType.Empty and handle the failure at the parse site.
- Use the XpsS0Markup content type constants for known XPS part types.
Example fix
// before
ContentType ct = ContentType.Empty; // placeholder
xpsManager.GenerateUniquePart(ct);
// after
ContentType ct = new ContentType(XpsS0Markup.PrintTicketContentType);
if (ContentType.Empty.AreTypeAndSubTypeEqual(ct)) throw new ArgumentException("contentType must not be empty");
xpsManager.GenerateUniquePart(ct); Defensive patterns
Strategy: validation
Validate before calling
if (contentType is null || ContentType.Empty.AreTypeAndSubTypeEqual(contentType)) throw new ArgumentException("contentType must be a concrete, non-empty type", nameof(contentType)); Type guard
bool IsConcreteType(ContentType ct) => ct != null && !ContentType.Empty.AreTypeAndSubTypeEqual(ct);
Try / catch
try { return xpsManager.GenerateUniquePart(contentType); }
catch (ArgumentException ex) when (ex.Message.Contains("type")) { /* substitute the correct XPS content type constant */ } Prevention
- Replace ContentType.Empty placeholders with real MIME types at construction time.
- Fail fast where a ContentType parse produces Empty instead of passing it downstream.
- Prefer well-known XPS type constants over dynamic construction.
When it happens
Trigger: Calling GenerateUniquePart (or AddSignatureDefinitionPart, AddDocumentPropertiesPart, AddThumbnail that funnel into it) with contentType equal to ContentType.Empty, e.g. default-initialized or failed parse results.
Common situations: ContentType fields default-initialized and never assigned; a parsing step that returned ContentType.Empty on failure being passed through; refactored code that replaced a literal MIME string with ContentType.Empty placeholder.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- ' ' ContentType 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/b626505cd8d14494.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/ReachFramework/Packaging/XpsManager.cs:350
/// </param>
/// <returns>
/// Returns the newly created Metro Part.
/// </returns>
public
PackagePart
GenerateUniquePart(
ContentType contentType
)
{
ObjectDisposedException.ThrowIf(_metroPackage is null, typeof(XpsManager));
if (!IsWriter)
{
throw new XpsPackagingException(SR.ReachPackaging_OnlyWriters);
}
ArgumentNullException.ThrowIfNull(contentType);
if (ContentType.Empty.AreTypeAndSubTypeEqual(contentType))
{
throw new ArgumentException(SR.ReachPackaging_InvalidType);
}
//
// Generate a unique part Uri
//
System.Uri partUri = GenerateUniqueUri(contentType);
return GeneratePart( contentType, partUri);
}
public
PrintTicket
EnsurePrintTicket(
Uri partUri
)
{
PrintTicket printTicket = null;
PackagePart printTicketPart = GetPrintTicketPart(partUri);View on GitHub (pinned to 81131a70a4)