当设置SizeLimit时,意外的“缓存条目必须为Size指定一个值".在AspNetCore 3中的消息 [英] Unexpected "Cache entry must specify a value for Size when SizeLimit is set" message in AspNetCore 3
问题描述
因此,在今天更新到AspNetCore 3之前,一切都很好.
So this all worked fine before updating to AspNetCore 3 today.
我正在使用具有依赖项注入(IMemoryCache cache
)的内存缓存.
I am using a memory cache with dependency injection (IMemoryCache cache
).
我使用services.AddMemoryCache();
将其添加到我的中间件中,但没有设置大小,但最终还是出现错误消息:
I add it to my middleware with services.AddMemoryCache();
and do NOT set a size, but I still end up with the error message:
设置SizeLimit时,缓存条目必须为Size指定一个值.
Cache entry must specify a value for Size when SizeLimit is set.
当我检查MemoryCache
的实例时,确实确实设置了10240
的大小(参见图片).
When I inspect the instance of MemoryCache
and it does indeed have a size of 10240
set (see image).
问题是我一直在找一个小时,却不知道该在哪里设置.在我的代码中的任何地方都没有SizeLimit
或10240
-包括配置文件.
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.
当我切换为使用app.UseEndpoints
而不是app.UseMvc()
时,它似乎已经开始-但我已经做出了很多不确定的更改.
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.?
推荐答案
我设法通过从Startup.cs
的ConfigureServices()
方法中删除对AddEntityFrameworkSqlServer()
的调用来阻止抛出此异常:
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(...)
)
...
}
...
}