{"record":{"id":"325ab99d75ea0279","repo":"dotnet/wpf","slug":"0-contenttype-is-not-valid-xpsmanager","errorCode":null,"errorMessage":"'{0}' ContentType is not valid.","messagePattern":"'(.+?)' ContentType is not valid\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/ReachFramework/Packaging/XpsManager.cs","lineNumber":300,"sourceCode":"        /// Generate a unique Part for the content type and add it to the package.\n        /// Adding to any relationships or selector/sequence markup is not done.\n        /// </summary>\n        public\n        PackagePart\n        GeneratePart(\n            ContentType contentType,\n            Uri      \tpartUri\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 (0 == contentType.ToString().Length)\n            {\n                throw new ArgumentException(SR.Format(SR.ReachPackaging_InvalidContentType, contentType), nameof(contentType));\n            }\n            \n            //Do not compress image Content Types\n            CompressionOption compressionOption = _compressionOption;\n\n            if (contentType.AreTypeAndSubTypeEqual(XpsS0Markup.JpgContentType) ||\n                contentType.AreTypeAndSubTypeEqual(XpsS0Markup.PngContentType) ||\n                contentType.AreTypeAndSubTypeEqual(XpsS0Markup.TifContentType) ||\n                contentType.AreTypeAndSubTypeEqual(XpsS0Markup.WdpContentType))\n            {\n                compressionOption = CompressionOption.NotCompressed;\n            }\n\n            PackagePart metroPart = _metroPackage.CreatePart(partUri,\n                                                             contentType.ToString(),\n                                                             compressionOption);\n\n            //","sourceCodeStart":282,"sourceCodeEnd":318,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/ReachFramework/Packaging/XpsManager.cs#L282-L318","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nvar ct = new ContentType(config[\"PartContentType\"] ?? \"\");\nvar part = xpsManager.GeneratePart(partUri, ct, comp);\n// after\nstring ctString = config[\"PartContentType\"];\nif (string.IsNullOrEmpty(ctString)) throw new ArgumentException(\"PartContentType must be a non-empty MIME type\");\nvar part = xpsManager.GeneratePart(partUri, new ContentType(ctString), comp);","handlingStrategy":"validation","validationCode":"if (contentType is null) throw new ArgumentNullException(nameof(contentType));\nif (string.IsNullOrWhiteSpace(contentType.ToString())) throw new ArgumentException(\"Content type must be non-empty\", nameof(contentType));","typeGuard":"bool IsValidContentType(ContentType ct) => ct != null && !string.IsNullOrEmpty(ct.ToString());","tryCatchPattern":"try { return xpsManager.GeneratePart(partUri, contentType, option); }\ncatch (ArgumentException ex) when (ex.ParamName == nameof(contentType)) { /* fix/derive a valid MIME type */ throw new ArgumentException($\"Invalid content type: {contentType}\", ex); }","preventionTips":["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."],"tags":["wpf","xps","content-type","argument"],"backgroundTag":"empty-required-field","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"}