microsoft / FeatureManagement-Dotnet

Microsoft.FeatureManagement provides standardized APIs for enabling feature flags within applications. Utilize this library to secure a consistent experience when developing applications that use patterns such as beta access, rollout, dark deployments, and more.
MIT License
1.06k stars 115 forks source link

Cache Entry does not set size #325

Closed mjameson-se closed 11 months ago

mjameson-se commented 12 months ago

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.

jimmyca15 commented 12 months ago

@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.

jimmyca15 commented 12 months ago

@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.

jimmyca15 commented 11 months ago

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