dotnet/wpf · error · NullReferenceException

template

Error message

template

What it means

RightsManagementProvider.GenerateUnsignedPublishLicense throws NullReferenceException when the 'template' string parameter is null or empty. The method requires a non-empty template string to construct an UnsignedPublishLicense, and guards this with an explicit check at the top of the method. This mirrors the underlying UnsignedPublishLicense(string) constructor's own requirement.

Solutions

  1. Pass a valid, non-empty publish-license template string to GenerateUnsignedPublishLicense.
  2. Load the template from Application.GetResourceStream or a file and verify it is non-null and non-empty before calling.
  3. If no template is needed, use the parameterless UnsignedPublishLicense constructor path instead of this API.
  4. Correct the null-check semantics: catch NullReferenceException is not recommended; validate the input first.

Example fix

// before
provider.GenerateUnsignedPublishLicense(template); // template may be null
// after
if (!string.IsNullOrEmpty(template))
{
    provider.GenerateUnsignedPublishLicense(template);
}
Defensive patterns

Strategy: validation

Validate before calling

if (string.IsNullOrEmpty(template))
    throw new ArgumentException("template must be a non-empty publish license template.", nameof(template));
provider.GenerateUnsignedPublishLicense(template);

Type guard

bool IsValidTemplate(string template) => !string.IsNullOrEmpty(template);

Try / catch

try { provider.GenerateUnsignedPublishLicense(template); }
catch (NullReferenceException ex) { /* handle missing template input */ }

Prevention

When it happens

Trigger: Calling ((IRightsManagementProvider)provider).GenerateUnsignedPublishLicense(null) or GenerateUnsignedPublishLicense("") or a whitespace-only/empty string obtained from an uninitialized configuration or resource load.

Common situations: Developers building rights-managed XPS documents pass a template loaded from a file or resource that failed to load (returning null), or forget to populate the template field before publishing.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationUI/MS/Internal/Documents/RightsManagementProvider.cs:749

        //
        // Save temporary unsigned license and rights dictionary for signing
        //

        _temporaryRightsDictionary = rights;
        _temporaryUnsignedPublishLicense = unsignedPublishLicense;
    }

    /// <summary>
    /// Generates an unsigned publish license for the package from 
    /// the template.
    /// </summary>
    /// <param name="template">
    /// The template from which to generate a publish license</param>
    void IRightsManagementProvider.GenerateUnsignedPublishLicense(string template)
    {
        if (string.IsNullOrEmpty(template))
        {
            throw new NullReferenceException("template");
        }

        UnsignedPublishLicense unsignedPublishLicense = null;
        // Create the license from the template.
        unsignedPublishLicense = new UnsignedPublishLicense(template);

        // Get who is currently listed as the owner of the document in the
        // publish license
        ContentUser currentOwnerFromLicense = null;
        currentOwnerFromLicense = unsignedPublishLicense.Owner;

        RightsManagementUser currentOwner = null;

        if (currentOwnerFromLicense != null)
        {
            currentOwner = RightsManagementUser.CreateUser(
                currentOwnerFromLicense);
        }

View on GitHub (pinned to 81131a70a4)