ASP.NET Core 2.1 - Error Implementing MemoryCache
Solution 1
Seems to be that you try to inject IDistributedCache which is different from memory cache. Distributed cache will be using external services to store cache while memory cache is going to use servers memory.
As I said something,somewhere is using distributed cache. And that something is session
From that page
The default session provider in ASP.NET Core loads session records from the underlying IDistributedCache
Solution 2
Simply adding services.AddMemoryCache()
after services.AddControllers()
worked for me.
Related videos on Youtube
James Poulose
Updated on September 16, 2022Comments
-
James Poulose over 1 year
I was following the steps given here to implement a
MemoryCache
inASP.NET Core
and when i start the application (dotnet run
from command prompt), i get the following error.System.InvalidOperationException: Unable to resolve service for type 'Microsoft.Extensions.Caching.Distributed.IDistributedCache' while attempting to activate 'Microsoft.AspNetCore.Session.DistributedSessionStore'.
What is confusing me is that i am using
services.AddMemoryCache()
and NOTservices.AddDistributedMemoryCache()
. Full stack trace is available in this bin. I have only these packages referenced<PackageReference Include="Microsoft.AspNetCore.App" /> <PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.1.2" PrivateAssets="All" /> <PackageReference Include="System.Configuration.ConfigurationManager" Version="4.5.0" />
My
Configure
public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseStaticFiles(); app.UseSpaStaticFiles(); app.UseSession(); app.UseCors( builder => builder .WithOrigins("http://localhost:4200") .AllowAnyHeader() .AllowAnyMethod() .AllowAnyOrigin() .AllowCredentials()); app.UseMvc( routes => { routes.MapRoute( "default", "api/{controller}/{action}/{id?}"); }); }
ConfigureServices
public void ConfigureServices(IServiceCollection services) { services.AddCors(); services .AddMvcCore() .SetCompatibilityVersion(CompatibilityVersion.Version_2_1) .AddJsonFormatters(); services.AddMemoryCache(); // Angular files will be served from this directory services.AddSpaStaticFiles(configuration => { configuration.RootPath = "wwwroot"; }); services.AddSession( options => { // Set a short timeout for easy testing. options.IdleTimeout = TimeSpan.FromHours(1); options.Cookie.HttpOnly = true; }); }
Program.cs
public static IWebHostBuilder CreateWebHostBuilder(string[] args) { return WebHost.CreateDefaultBuilder(args) .UseStartup<Startup>(); } public static void Main(string[] args) { CreateWebHostBuilder(args).Build().Run(); }
-
jazb over 5 yearscan you check your *.csproj xml file for refs for any ref to
IDistributedCache
that you may need to clear out if it was previously added
-
-
James Poulose over 5 yearsAs i have mentioned in the question, I am not using (at least knowingly)
IDistributedCache
(services.AddDistributedMemoryCache()
). I am usingservices.AddMemoryCache()
. -
Vova Bilyachat over 5 years@JamesPoulose its not about what you add to services, something somewhere wants to inject distributed cache.
-
James Poulose over 5 yearsRemoving
app.UseSession()
fromConfigure
method was the key - thanks!