dotnet/wpf · error · XpsPackagingException

SR.ReachPackaging_ServiceTypeAlreadyAdded

Error message

SR.ReachPackaging_ServiceTypeAlreadyAdded

What it means

XpsResourcePolicy.RegisterService stores one service instance per service type in an internal dictionary. If the service type is already registered with a DIFFERENT instance, it throws XpsPackagingException(SR.ReachPackaging_ServiceTypeAlreadyAdded); re-registering the same instance is silently allowed.

Solutions

  1. Check whether the service type is already registered before calling RegisterService, and only add if absent.
  2. Unregister/remove the existing service (or create a fresh XpsSerializationManager) before registering the new instance.
  3. Reuse the same service instance if you must register again, since identical instances are accepted.

Example fix

// before
manager.RegisterService(typeof(MyService), serviceA);
manager.RegisterService(typeof(MyService), serviceB); // throws
// after
manager.RegisterService(typeof(MyService), serviceA);
// or remove/replace first, then register serviceB
Defensive patterns

Strategy: validation

Try / catch

catch (XpsPackagingException) { /* service type already registered with a different instance: reuse or unregister first */ }

Prevention

When it happens

Trigger: Calling RegisterService twice for the same serviceType with two different service objects — e.g. registering a custom service on an XpsSerializationManager/XpsOMSerializationManager that already registered its own service of that type, or double-initialization in serialization pipeline setup.

Common situations: Custom printing/serialization pipelines adding a service both globally and per-manager; re-running serialization setup without clearing state; two components each registering their own implementation of the same service interface.

Understand the failure class

Background: Conflicting config options: "cannot be used together" — configuration validation errors across open-source libraries — this error's family across 162 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/a7aede776b880189. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/ReachFramework/Packaging/XpsResourcePolicy.cs:91

        /// <exception cref="ArgumentNullException">service or serviceType is null..</exception>
        /// <exception cref="XpsPackagingException">serviceType has already been registered..</exception>
        public
        void
        RegisterService(
            object      service,
            Type        serviceType
            )
        {
            ArgumentNullException.ThrowIfNull(serviceType);
            ArgumentNullException.ThrowIfNull(service);

            if (!_objDict.ContainsKey(serviceType))
            {
                _objDict.Add(serviceType, service);
            }
            else if (_objDict[serviceType] != service)
            {
                throw new XpsPackagingException(SR.Format(SR.ReachPackaging_ServiceTypeAlreadyAdded, serviceType));
            }
        }
        
        internal
        bool
        SubsetComplete(INode node)
        {
            FontSubsetterCommitPolicies signal = FontSubsetterCommitPolicies.CommitPerPage;
            bool validSubsetNode = true;
            bool subsetComplete = false;
            if( node is IXpsFixedDocumentSequenceWriter )
            {
                signal  = FontSubsetterCommitPolicies.CommitEntireSequence;
            }
            else
            if( node is IXpsFixedDocumentWriter )
            {
                signal  = FontSubsetterCommitPolicies.CommitPerDocument;

View on GitHub (pinned to 81131a70a4)