问题
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.?
回答1:
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!
来源:https://stackoverflow.com/questions/58406143/unexpected-cache-entry-must-specify-a-value-for-size-when-sizelimit-is-set-mes