{"record":{"id":"b626505cd8d14494","repo":"dotnet/wpf","slug":"string-passed-for-type-is-not-valid-xpsmanager","errorCode":null,"errorMessage":"String passed for type is not valid.","messagePattern":"String passed for type is not valid\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/ReachFramework/Packaging/XpsManager.cs","lineNumber":350,"sourceCode":"        /// </param>\n        /// <returns>\n        /// Returns the newly created Metro Part.\n        /// </returns>\n        public\n        PackagePart\n        GenerateUniquePart(\n            ContentType contentType\n            )\n        {\n            ObjectDisposedException.ThrowIf(_metroPackage is null, typeof(XpsManager));\n            if (!IsWriter)\n            {\n                throw new XpsPackagingException(SR.ReachPackaging_OnlyWriters);\n            }\n            ArgumentNullException.ThrowIfNull(contentType);\n            if (ContentType.Empty.AreTypeAndSubTypeEqual(contentType))\n            {\n                throw new ArgumentException(SR.ReachPackaging_InvalidType);\n            }\n\n            //\n            // Generate a unique part Uri\n            //\n            System.Uri partUri = GenerateUniqueUri(contentType);\n\n            return GeneratePart( contentType, partUri);\n       }\n\n        public\n        PrintTicket\n        EnsurePrintTicket(\n            Uri partUri\n            )\n        {\n            PrintTicket printTicket = null;\n            PackagePart printTicketPart = GetPrintTicketPart(partUri);","sourceCodeStart":332,"sourceCodeEnd":368,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/ReachFramework/Packaging/XpsManager.cs#L332-L368","documentation":"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.","triggerScenarios":"Calling GenerateUniquePart (or AddSignatureDefinitionPart, AddDocumentPropertiesPart, AddThumbnail that funnel into it) with contentType equal to ContentType.Empty, e.g. default-initialized or failed parse results.","commonSituations":"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.","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."],"exampleFix":"// before\nContentType ct = ContentType.Empty; // placeholder\nxpsManager.GenerateUniquePart(ct);\n// after\nContentType ct = new ContentType(XpsS0Markup.PrintTicketContentType);\nif (ContentType.Empty.AreTypeAndSubTypeEqual(ct)) throw new ArgumentException(\"contentType must not be empty\");\nxpsManager.GenerateUniquePart(ct);","handlingStrategy":"validation","validationCode":"if (contentType is null || ContentType.Empty.AreTypeAndSubTypeEqual(contentType)) throw new ArgumentException(\"contentType must be a concrete, non-empty type\", nameof(contentType));","typeGuard":"bool IsConcreteType(ContentType ct) => ct != null && !ContentType.Empty.AreTypeAndSubTypeEqual(ct);","tryCatchPattern":"try { return xpsManager.GenerateUniquePart(contentType); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"type\")) { /* substitute the correct XPS content type constant */ }","preventionTips":["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."],"tags":["wpf","xps","content-type","argument"],"backgroundTag":"invalid-argument-value","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}