microsoft/FeatureManagement-Dotnet

Cache Entry does not set size

Closed this issue ยท 3 comments

We're in the process of implementing a feature using Azure App Config to power feature gates, but hit a rather annoying snag.

System.InvalidOperationException: Cache entry must specify a value for Size when SizeLimit is set.
   at Microsoft.Extensions.Caching.Memory.MemoryCache.SetEntry(CacheEntry entry)
   at Microsoft.Extensions.Caching.Memory.CacheExtensions.Set[TItem](IMemoryCache cache, Object key, TItem value, MemoryCacheEntryOptions options)
   at Microsoft.FeatureManagement.FeatureManager.BindSettings(IFeatureFilterMetadata filter, FeatureFilterEvaluationContext context, Int32 filterIndex)
   at Microsoft.FeatureManagement.FeatureManager.IsEnabledAsync[TContext](String feature, TContext appContext, Boolean useAppContext)
   at Microsoft.FeatureManagement.Mvc.FeatureGateAttribute.<>c__DisplayClass10_0.<<OnActionExecutionAsync>b__0>d.MoveNext(

The Feature Manager is getting a shared IMemoryCache from the dependency injection container and tries to add cache entries to it without specifying a size. The only work-around for this seems to be to alter all of our code to register and pull a keyed IMemoryCache instead of the default unkeyed instance.

@mjameson-se

Thanks for raising this. Looks like an issue introduced in 3.1.0 release.

@zhiyuanliang-ms We need to specify size for all entries placed in the cache.

@zhiyuanliang-ms

To repro this issue add the following in Startup.cs of the FeatureFlagDemo example project.

            services.AddMemoryCache(mo =>
            {
                mo.SizeLimit = 50_000_000;
            });

This issue occurs when the shared memory cache in DI is configured to have a size limit.

Hello @mjameson-se this issue has been fixed and released in version 3.1.1.