{"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":"exception","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/3600ca084e9c8b5f4174fc5e747f4c52d2100806/src/Caching/SqlServer/src/SqlServerCache.cs#L36-L72","documentation":"Thrown in the SqlServerCache constructor when DefaultSlidingExpiration is <= TimeSpan.Zero. The default sliding expiration is the fallback lifetime applied to entries that do not specify their own expiration, so it must be a positive duration.","triggerScenarios":"Configuring SqlServerCache with options.DefaultSlidingExpiration <= TimeSpan.Zero (zero, negative, or unset default that resolves to zero).","commonSituations":"Forgetting to set DefaultSlidingExpiration in AddDistributedSqlServerCache (when it defaults to TimeSpan.Zero); misconfiguring via a config binding that produced zero; passing a negative value by mistake.","solutions":["Set DefaultSlidingExpiration to a positive TimeSpan (e.g. TimeSpan.FromMinutes(20)) in AddDistributedSqlServerCache.","If binding from IConfiguration, ensure the config key maps to a valid duration string.","Validate the value at startup and fail fast with a clear message before cache use."],"exampleFix":"// before: unset default sliding expiration resolves to zero\nservices.AddDistributedSqlServerCache(o => {\n    o.ConnectionString = cs; o.SchemaName = \"dbo\"; o.TableName = \"Cache\";\n});\n\n// after: positive default\nservices.AddDistributedSqlServerCache(o => {\n    o.ConnectionString = cs; o.SchemaName = \"dbo\"; o.TableName = \"Cache\";\n    o.DefaultSlidingExpiration = TimeSpan.FromMinutes(20);\n});","handlingStrategy":"validation","validationCode":"if (options.DefaultSlidingExpiration <= TimeSpan.Zero) { /* set a positive default before building the cache */ }","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always set DefaultSlidingExpiration in AddDistributedSqlServerCache.","Bind config with a sensible default and ValidateOnStart().","Add a startup smoke test that resolves SqlServerCache from DI."],"tags":["caching","sqlserver","aspnetcore","distributed-cache","configuration"],"backgroundTag":null,"analyzedSha":"3600ca084e9c8b5f4174fc5e747f4c52d2100806","analyzedAt":"2026-08-11T16:32:30.678Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}