{"record":{"id":"fddbfa104d26e955","repo":"dotnet/aspnetcore","slug":"the-sliding-expiration-value-must-be-positive","errorCode":null,"errorMessage":"The sliding expiration value must be positive.","messagePattern":"The sliding expiration value must be positive\\.","errorType":"validation","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/Caching/SqlServer/src/SqlServerCache.cs","lineNumber":54,"sourceCode":"    public SqlServerCache(IOptions<SqlServerCacheOptions> options)\n    {\n        var cacheOptions = options.Value;\n\n        ArgumentThrowHelper.ThrowIfNullOrEmpty(cacheOptions.ConnectionString);\n        ArgumentThrowHelper.ThrowIfNullOrEmpty(cacheOptions.SchemaName);\n        ArgumentThrowHelper.ThrowIfNullOrEmpty(cacheOptions.TableName);\n\n        if (cacheOptions.ExpiredItemsDeletionInterval.HasValue &&\n            cacheOptions.ExpiredItemsDeletionInterval.Value < MinimumExpiredItemsDeletionInterval)\n        {\n            throw new ArgumentException(\n                $\"{nameof(SqlServerCacheOptions.ExpiredItemsDeletionInterval)} cannot be less than the minimum \" +\n                $\"value of {MinimumExpiredItemsDeletionInterval.TotalMinutes} minutes.\");\n        }\n        if (cacheOptions.DefaultSlidingExpiration <= TimeSpan.Zero)\n        {\n#pragma warning disable CA2208 // Instantiate argument exceptions correctly\n            throw new ArgumentOutOfRangeException(\n                nameof(cacheOptions.DefaultSlidingExpiration),\n                cacheOptions.DefaultSlidingExpiration,\n                \"The sliding expiration value must be positive.\");\n#pragma warning restore CA2208 // Instantiate argument exceptions correctly\n        }\n\n        _systemClock = cacheOptions.SystemClock ?? new SystemClock();\n        _expiredItemsDeletionInterval =\n            cacheOptions.ExpiredItemsDeletionInterval ?? DefaultExpiredItemsDeletionInterval;\n        _deleteExpiredCachedItemsDelegate = DeleteExpiredCacheItems;\n        _defaultSlidingExpiration = cacheOptions.DefaultSlidingExpiration;\n\n        _dbOperations = new DatabaseOperations(\n            cacheOptions.ConnectionString,\n            cacheOptions.SchemaName,\n            cacheOptions.TableName,\n            _systemClock);\n    }","sourceCodeStart":36,"sourceCodeEnd":72,"githubUrl":"https://github.com/dotnet/aspnetcore/blob/294cab2f9b2e03af6b953820c7ab497c3c8b7ad9/src/Caching/SqlServer/src/SqlServerCache.cs#L36-L72","documentation":"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.","triggerScenarios":"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.","commonSituations":"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).","solutions":["Explicitly set DefaultSlidingExpiration to a positive value: options.DefaultSlidingExpiration = TimeSpan.FromMinutes(20).","Check the configuration section binding (e.g., \"DefaultSlidingExpiration\": \"00:20:00\") is present and correctly formatted.","Ensure the value is loaded from config before the cache is constructed."],"exampleFix":"// before — default sliding expiration not set (defaults to Zero)\nservices.AddDistributedSqlServerCache(o =>\n{\n    o.ConnectionString = connStr;\n    o.SchemaName = \"dbo\";\n    o.TableName = \"Cache\";\n    // DefaultSlidingExpiration missing -> TimeSpan.Zero -> throws\n});\n\n// after — set a positive default sliding expiration\nservices.AddDistributedSqlServerCache(o =>\n{\n    o.ConnectionString = connStr;\n    o.SchemaName = \"dbo\";\n    o.TableName = \"Cache\";\n    o.DefaultSlidingExpiration = TimeSpan.FromMinutes(20);\n});","handlingStrategy":"validation","validationCode":"// Validate before constructing\nif (cacheOptions.DefaultSlidingExpiration <= TimeSpan.Zero)\n{\n    throw new ArgumentOutOfRangeException(nameof(cacheOptions.DefaultSlidingExpiration),\n        \"DefaultSlidingExpiration must be positive.\");\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always set DefaultSlidingExpiration to a positive TimeSpan when configuring AddDistributedSqlServerCache.","Verify config binding provides a valid TimeSpan (e.g., \"00:20:00\")."],"tags":["caching","sqlserver","distributed-cache","configuration","startup"],"analyzedSha":"294cab2f9b2e03af6b953820c7ab497c3c8b7ad9","analyzedAt":"2026-08-06T20:08:02.189Z","schemaVersion":2},"datasetVersion":"2026-08-06T23:17:07.152Z"}