{"record":{"id":"34539abc79a73ac0","repo":"dotnet/wpf","slug":"sr-rmproviderexceptionnopackagetodecrypt","errorCode":null,"errorMessage":"SR.RMProviderExceptionNoPackageToDecrypt","messagePattern":"SR\\.RMProviderExceptionNoPackageToDecrypt","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationUI/MS/Internal/Documents/RightsManagementProvider.cs","lineNumber":193,"sourceCode":"        Trace.SafeWriteIf(\n            (_secureEnvironment != null),\n            Trace.Rights,\n            \"SecureEnvironment was initialized for a specific user.\");\n\n        SetUserFromSecureEnvironment();        \n    }\n\n    /// <summary>\n    /// Loads a use license for the user from the package.\n    /// This requires InitializeEnvironment to have been called.\n    /// </summary>\n    /// <returns>Whether or not a use license could be loaded directly from the\n    /// package</returns>\n    bool IRightsManagementProvider.LoadUseLicense()\n    {\n        if (!IsProtected)\n        {\n            throw new InvalidOperationException(\n                SR.RMProviderExceptionNoPackageToDecrypt);\n        }\n\n        UseLicense useLicense;\n\n        useLicense = _encryptedPackageEnvelope\n                .RightsManagementInformation.LoadUseLicense(_user);\n\n        if (useLicense != null)\n        {\n            Trace.SafeWrite(Trace.Rights, \"Existing use license was found.\");\n            _useLicense = useLicense;\n        }\n\n        return (useLicense != null);\n    }\n\n    /// <summary>","sourceCodeStart":175,"sourceCodeEnd":211,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationUI/MS/Internal/Documents/RightsManagementProvider.cs#L175-L211","documentation":"IRightsManagementProvider.LoadUseLicense requires the provider to be wrapping an encrypted (RM-protected) package. When IsProtected is false there is no package to decrypt a use license from, so an InvalidOperationException with RMProviderExceptionNoPackageToDecrypt is thrown.","triggerScenarios":"Explicitly calling LoadUseLicense (via IRightsManagementProvider) on a provider whose _encryptedPackageEnvelope is null or unprotected.","commonSituations":"Calling license APIs before attaching/opening an encrypted package; passing an unencrypted XPS to RM tooling; provider constructed but no package loaded yet.","solutions":["Only call LoadUseLicense after IsProtected returns true","Load/attach the EncryptedPackageEnvelope first, or use the non-interface LoadUseLicense path appropriate for unprotected content","Guard with a check like if (provider.IsProtected) before calling license methods"],"exampleFix":"// before\nbool loaded = provider.LoadUseLicense();\n// after\nbool loaded = provider.IsProtected ? provider.LoadUseLicense() : false;","handlingStrategy":"validation","validationCode":"if (provider.IsProtected) { provider.LoadUseLicense(); }","typeGuard":"bool CanLoadLicense(IRightsManagementProvider p) => p is { IsProtected: true };","tryCatchPattern":"try { provider.LoadUseLicense(); }\ncatch (InvalidOperationException) { /* not a protected package; handle unprotected path */ }","preventionTips":["Always check IsProtected before RM license APIs","Structure RM steps behind a single guarded entry point","Validate the package is encrypted before handing it to the provider"],"tags":["rights-management","invalid-state","package"],"backgroundTag":"invalid-state-transition","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"}