当设置SizeLimit时,意外的“缓存条目必须为Size指定一个值".在AspNetCore 3中的消息 [英] Unexpected "Cache entry must specify a value for Size when SizeLimit is set" message in AspNetCore 3

查看:600
本文介绍了当设置SizeLimit时,意外的“缓存条目必须为Size指定一个值".在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).

问题是我一直在找一个小时,却不知道该在哪里设置.在我的代码中的任何地方都没有SizeLimit10240-包括配置文件.

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.csConfigureServices()方法中删除对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(...)
         )

      ...
   }

   ...
}

明显调用AddEntityFrameworkSqlServer()

在构建大多数应用程序时,包括在ASP.NET或其他地方使用依赖注入的应用程序时,不再需要调用此方法.

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

感谢 @Simon_Weaver 关于EF Core的线索!

Thanks to @Simon_Weaver for his clue about EF Core!

这篇关于当设置SizeLimit时,意外的“缓存条目必须为Size指定一个值".在AspNetCore 3中的消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆