Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unexpected "Cache entry must specify a value for Size when SizeLimit is set" message in AspNetCore 3

So this all worked fine before updating to AspNetCore 3 today.

I am using a memory cache with dependency injection (IMemoryCache cache).

I add it to my middleware with services.AddMemoryCache(); and do NOT set a size, but I still end up with the error message:

Cache entry must specify a value for Size when SizeLimit is set.

When I inspect the instance of MemoryCache and it does indeed have a size of 10240 set (see image).

The problem is I've been looking for a hour and I have no clue where this was set. Nowhere in my code do I have SizeLimit or 10240 anywhere - including config files.

It seems to have started when I switched to using app.UseEndpoints instead of app.UseMvc() - but I've made so many changes I'm not sure.

Where could this possibly be set that is elluding me.?

enter image description here

like image 717
Simon_Weaver Avatar asked Oct 16 '19 05:10

Simon_Weaver


Video Answer


1 Answers

I managed to stop this exception from being thrown by removing the call to AddEntityFrameworkSqlServer() from my ConfigureServices() method in Startup.cs:

public class Startup
{
   ...

   public void ConfigureServices(IServiceCollection services)
   {
      ...

      services
         .AddEntityFrameworkSqlServer() // <-- Removed this
         .AddDbContext<MyContext>(options =>
            options.UseSqlServer(...)
         )

      ...
   }

   ...
}

Apparently calling AddEntityFrameworkSqlServer() is no longer needed in EF Core 3:

Calling this method is no longer necessary when building most applications, including those that use dependency injection in ASP.NET or elsewhere.

Thanks to @Simon_Weaver for his clue about EF Core!

like image 95
Yogster Avatar answered Oct 11 '22 19:10

Yogster