{"record":{"id":"e8300720e2ad0c04","repo":"dotnet/wpf","slug":"no-image-service-is-registered-with-resource-policy","errorCode":null,"errorMessage":"No image service is registered with resource policy.","messagePattern":"No image service is registered with resource policy\\.","errorType":"exception","errorClass":"XpsSerializationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/ReachFramework/Serialization/ImageSourceTypeConverter.cs","lineNumber":201,"sourceCode":"                   // Also, add a relationship for the current page to this image\n                   // resource.  This is needed to conform with Xps specification.\n                   //\n                   manager.AddRelationshipToCurrentPage(imageUri, XpsS0Markup.ResourceRelationshipName);\n                   currentPageImageTable.Add(uriHashCode, imageUri);\n                }\n            }\n            else\n            {\n                //\n                // This image as never serialized before so we will do it now.\n                // Retrieve the image serialization service from the resource policy\n                //\n                IServiceProvider resourceServiceProvider = manager.ResourcePolicy;\n\n                XpsImageSerializationService imageService = (XpsImageSerializationService)resourceServiceProvider.GetService(typeof(XpsImageSerializationService));\n                if (imageService == null)\n                {\n                    throw new XpsSerializationException(SR.ReachSerialization_NoImageService);\n                }\n\n                //\n                // Obtain a valid encoder for the image.\n                //\n                BitmapEncoder encoder = imageService.GetEncoder(bitmapSource);\n                string imageMimeType = GetImageMimeType(encoder);\n\n                bool isSupportedMimeType = imageService.IsSupportedMimeType(bitmapSource);\n\n                //\n                // Acquire a writable stream from the serialization manager and encode\n                // and serialize the image into the stream.\n                //\n                XpsResourceStream resourceStream = manager.AcquireResourceStream(typeof(BitmapSource), imageMimeType);\n                bool bCopiedStream = false;\n\n                BitmapFrame bitmapFrame = bitmapSource as BitmapFrame;","sourceCodeStart":183,"sourceCodeEnd":219,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/ReachFramework/Serialization/ImageSourceTypeConverter.cs#L183-L219","documentation":"ImageSourceTypeConverter.ConvertTo obtains an image encoding service from the serialization manager's ResourcePolicy. When GetService(typeof(XpsImageSerializationService)) returns null, the converter cannot acquire an encoder or write the image part into the XPS package and throws XpsSerializationException with ReachSerialization_NoImageService.","triggerScenarios":"Calling ConvertTo with a context whose XpsSerializationManager/PackageSerializationManager has a ResourcePolicy lacking an XpsImageSerializationService registration — e.g. a hand-built serialization manager or a pipeline that only registered font services.","commonSituations":"Custom XPS packaging hosts missing image resource services; reusing a serialization manager configured for a different resource kind; invoking the converter outside the XpsDocumentWriter save flow.","solutions":["Run image serialization through the standard XpsDocumentWriter/XpsSerializationManager pipeline, which registers XpsImageSerializationService.","If using a custom manager, register an XpsImageSerializationService on its ResourcePolicy before ConvertTo.","Catch XpsSerializationException and fall back to writing the image part manually with an appropriate BitmapEncoder."],"exampleFix":"// before\nvar manager = new PackageSerializationManager(); // no image service\nconverter.ConvertTo(context, culture, bitmapSource, typeof(Uri));\n\n// after\nusing (XpsDocument doc = new XpsDocument(pkgPath, FileAccess.ReadWrite))\n{\n    var writer = XpsDocument.CreateXpsDocumentWriter(doc);\n    writer.Write(visual); // full pipeline registers the image service\n}","handlingStrategy":"try-catch","validationCode":"var manager = (PackageSerializationManager)context.GetService(typeof(XpsSerializationManager));\nbool hasImageService = manager?.ResourcePolicy?.GetService(typeof(XpsImageSerializationService)) != null;\nif (!hasImageService) throw new InvalidOperationException(\"Image serialization requires XpsImageSerializationService.\");","typeGuard":"static bool HasImageService(IServiceProvider provider) =>\n    provider?.GetService(typeof(XpsImageSerializationService)) is XpsImageSerializationService;","tryCatchPattern":"try\n{\n    converter.ConvertTo(context, culture, bitmapSource, typeof(Uri));\n}\ncatch (XpsSerializationException ex) when (ex.Message.Contains(\"image service\"))\n{\n    // write the image part manually with PngBitmapEncoder/JpegBitmapEncoder\n}","preventionTips":["Use the standard XpsDocumentWriter pipeline so all resource services are registered.","Validate ResourcePolicy registrations (font + image) before serializing documents with visuals.","Test custom serialization managers with image-bearing pages, not just text."],"tags":["wpf","xps","serialization","images","missing-service"],"backgroundTag":"missing-dependency","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"}