dotnet/aspnetcore · error · ArgumentOutOfRangeException

The sliding expiration value must be positive.

Error message

The sliding expiration value must be positive.

What it means

Thrown by the SqlServerCache constructor (line 54) when SqlServerCacheOptions.DefaultSlidingExpiration is less than or equal to TimeSpan.Zero. The default sliding expiration is the fallback applied to cache entries that don't specify their own expiration (see GetOptions at SqlServerCache.cs:283), so it must be a positive duration. The exception type is ArgumentOutOfRangeException, thrown at construction time.

Source

Thrown at src/Caching/SqlServer/src/SqlServerCache.cs:54

    public SqlServerCache(IOptions<SqlServerCacheOptions> options)
    {
        var cacheOptions = options.Value;

        ArgumentThrowHelper.ThrowIfNullOrEmpty(cacheOptions.ConnectionString);
        ArgumentThrowHelper.ThrowIfNullOrEmpty(cacheOptions.SchemaName);
        ArgumentThrowHelper.ThrowIfNullOrEmpty(cacheOptions.TableName);

        if (cacheOptions.ExpiredItemsDeletionInterval.HasValue &&
            cacheOptions.ExpiredItemsDeletionInterval.Value < MinimumExpiredItemsDeletionInterval)
        {
            throw new ArgumentException(
                $"{nameof(SqlServerCacheOptions.ExpiredItemsDeletionInterval)} cannot be less than the minimum " +
                $"value of {MinimumExpiredItemsDeletionInterval.TotalMinutes} minutes.");
        }
        if (cacheOptions.DefaultSlidingExpiration <= TimeSpan.Zero)
        {
#pragma warning disable CA2208 // Instantiate argument exceptions correctly
            throw new ArgumentOutOfRangeException(
                nameof(cacheOptions.DefaultSlidingExpiration),
                cacheOptions.DefaultSlidingExpiration,
                "The sliding expiration value must be positive.");
#pragma warning restore CA2208 // Instantiate argument exceptions correctly
        }

        _systemClock = cacheOptions.SystemClock ?? new SystemClock();
        _expiredItemsDeletionInterval =
            cacheOptions.ExpiredItemsDeletionInterval ?? DefaultExpiredItemsDeletionInterval;
        _deleteExpiredCachedItemsDelegate = DeleteExpiredCacheItems;
        _defaultSlidingExpiration = cacheOptions.DefaultSlidingExpiration;

        _dbOperations = new DatabaseOperations(
            cacheOptions.ConnectionString,
            cacheOptions.SchemaName,
            cacheOptions.TableName,
            _systemClock);
    }

View on GitHub (pinned to 294cab2f9b)

Solutions

  1. Explicitly set DefaultSlidingExpiration to a positive value: options.DefaultSlidingExpiration = TimeSpan.FromMinutes(20).
  2. Check the configuration section binding (e.g., "DefaultSlidingExpiration": "00:20:00") is present and correctly formatted.
  3. Ensure the value is loaded from config before the cache is constructed.

Example fix

// before — default sliding expiration not set (defaults to Zero)
services.AddDistributedSqlServerCache(o =>
{
    o.ConnectionString = connStr;
    o.SchemaName = "dbo";
    o.TableName = "Cache";
    // DefaultSlidingExpiration missing -> TimeSpan.Zero -> throws
});

// after — set a positive default sliding expiration
services.AddDistributedSqlServerCache(o =>
{
    o.ConnectionString = connStr;
    o.SchemaName = "dbo";
    o.TableName = "Cache";
    o.DefaultSlidingExpiration = TimeSpan.FromMinutes(20);
});
Defensive patterns

Strategy: validation

Validate before calling

// Validate before constructing
if (cacheOptions.DefaultSlidingExpiration <= TimeSpan.Zero)
{
    throw new ArgumentOutOfRangeException(nameof(cacheOptions.DefaultSlidingExpiration),
        "DefaultSlidingExpiration must be positive.");
}

Prevention

When it happens

Trigger: SqlServerCache is instantiated with options.DefaultSlidingExpiration <= TimeSpan.Zero. The default value of DefaultSlidingExpiration is TimeSpan.Zero if not explicitly set, which means leaving it unset also triggers this unless the options object provides a non-zero default.

Common situations: Forgetting to set DefaultSlidingExpiration when configuring AddDistributedSqlServerCache; setting it to zero or negative via config; a config binding error that leaves the TimeSpan at its default (zero).

Related errors


AI-assisted analysis of dotnet/aspnetcore@294cab2f9b (2026-08-06). Data as JSON: /api/errors/fddbfa104d26e955. Report an issue: GitHub.