nopSolutions/nopCommerce · error · NopException

'Guests' role could not be loaded

Error message

'Guests' role could not be loaded

What it means

Thrown by NewsLetterSubscriptionService when filtering subscriptions by customer role and detecting the requested role is the built-in 'Guests' role. It resolves the Guests role by system name (NopCustomerDefaults.GuestsRoleName) to switch to guest-specific query logic; if the role is absent, the query cannot proceed and throws NopException. A missing Guests role means the base customer-role seeding was not applied.

Source

Thrown at src/Libraries/Nop.Services/Messages/NewsLetterSubscriptionService.cs:267

                if (createdToUtc.HasValue)
                    query = query.Where(nls => nls.CreatedOnUtc <= createdToUtc.Value);
                if (storeId > 0)
                    query = query.Where(nls => nls.StoreId == storeId);
                if (subscriptionTypeId > 0)
                    query = query.Where(nls => nls.TypeId == subscriptionTypeId);
                if (isActive.HasValue)
                    query = query.Where(nls => nls.Active == isActive.Value);
                query = query.OrderBy(nls => nls.Email);

                return query;
            }, pageIndex, pageSize);

            return subscriptions;
        }

        //filter by customer role
        var guestRole = await _customerService.GetCustomerRoleBySystemNameAsync(NopCustomerDefaults.GuestsRoleName)
            ?? throw new NopException("'Guests' role could not be loaded");

        if (guestRole.Id == customerRoleId)
        {
            //guests
            var subscriptions = await _subscriptionRepository.GetAllPagedAsync(query =>
            {
                if (!string.IsNullOrEmpty(email))
                    query = query.Where(nls => nls.Email.Contains(email));
                if (createdFromUtc.HasValue)
                    query = query.Where(nls => nls.CreatedOnUtc >= createdFromUtc.Value);
                if (createdToUtc.HasValue)
                    query = query.Where(nls => nls.CreatedOnUtc <= createdToUtc.Value);
                if (storeId > 0)
                    query = query.Where(nls => nls.StoreId == storeId);
                if (subscriptionTypeId > 0)
                    query = query.Where(nls => nls.TypeId == subscriptionTypeId);
                if (isActive.HasValue)
                    query = query.Where(nls => nls.Active == isActive.Value);

View on GitHub (pinned to 64bdf2ff08)

Solutions

  1. Ensure the base installer seeded the built-in customer roles (Administrators, Registered, Guests, Vendors).
  2. Verify the Guests role row exists (SystemName == NopCustomerDefaults.GuestsRoleName).
  3. Reinstall customer-role seed data from a clean DB.
  4. Check for a migration that deleted built-in roles.

Example fix

// before
var guestRole = await _customerService.GetCustomerRoleBySystemNameAsync(NopCustomerDefaults.GuestsRoleName)
    ?? throw new NopException("'Guests' role could not be loaded");

// after - degrade gracefully instead of throwing
var guestRole = await _customerService.GetCustomerRoleBySystemNameAsync(NopCustomerDefaults.GuestsRoleName);
if (guestRole is null || guestRole.Id != customerRoleId)
    return await baseQuery.GetAllPagedAsync(pageIndex, pageSize);
Defensive patterns

Strategy: validation

Validate before calling

// Confirm the Guests role exists before newsletter role filtering
var guestRole = await _customerService.GetCustomerRoleBySystemNameAsync(NopCustomerDefaults.GuestsRoleName);
if (guestRole is null)
    throw new InvalidOperationException("Built-in 'Guests' customer role is missing; reseed customer roles.");

Type guard

static async Task<bool> GuestsRoleExistsAsync(ICustomerService svc)
    => await svc.GetCustomerRoleBySystemNameAsync(NopCustomerDefaults.GuestsRoleName) is not null;

Try / catch

try { await _subscriptionRepository.GetAllPagedAsync(...); }
catch (NopException ex) when (ex.Message == "'Guests' role could not be loaded")
{ /* reseed customer roles or fall back to a non-role-filtered query */ }

Prevention

When it happens

Trigger: Calling the newsletter subscription paged query with a customerRoleId equal to the Guests role while the Guests customer role is missing from the DB.

Common situations: Database where customer roles were not seeded (incomplete install); migration that removed built-in roles; test fixture that truncated CustomerRole.

Related errors


AI-assisted analysis of nopSolutions/nopCommerce@64bdf2ff08 (2026-08-13). Data as JSON: /api/errors/72f5690f17d23698. Report an issue: GitHub.