aspnetboilerplate: I can't use Second Level Cache with ABP

I cannot implement the Second Level Cache library because it needs to use the service provider and it is not available in the method that configures the DbContext Configuration.Modules.AbpEfCore().AddDBContext<MyDbContext>(options => ....... ..

And something like this is needed Configuration.Modules.AbpEfCore().AddDBContext<MyDbContext>((serviceProvider, options) => .......

Can you please help me to find a solution so inside the configurer I can implement this builder.UseSqlServer(connectionString).AddInterceptors(serviceProvider.GetRequiredService<SecondLevelCacheInterceptor>());

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 23 (11 by maintainers)

Most upvoted comments

@mardemza I think I figured it out. Since AppService methods and Controller actions are starting a transaction by default (UnitOfWork), you should use AllowCachingWithExplicitTransactions(true) while configuring second level cache. I tried this and there was no query in SQL Server profiler.

services.AddEFSecondLevelCache(options =>
		options.UseMemoryCacheProvider()
			.ConfigureLogging()
			.AllowCachingWithExplicitTransactions(true)
			.CacheAllQueries(CacheExpirationMode.Sliding, TimeSpan.FromMinutes(60)) // Does not take effect
);

I will try to check it again.

You can define method below in {ProjectName}DbContextConfigurer;

public static void ConfigureDesignTime(DbContextOptionsBuilder<Issue6672DbContext> builder, string connectionString)
{
	builder.UseSqlServer(connectionString);
}

and use it in {ProjectName}DbContextFactory as shown below;

{ProjectName}DbContextConfigurer.ConfigureDesignTime(builder, configuration.GetConnectionString(Issue6672Consts.ConnectionStringName));

see https://github.com/ismcagdas/Issue6672/commit/023c7e9db6106e46368f4baab21e8b6386558311

I have prepared a sample https://github.com/ismcagdas/Issue6672. Run the project and go to Users page and press the search button several times. You will see that only first request will go to database.

Yes, it is not same.

I will try to prepare a sample but it might take some time.

Maybe you can access serviceProvider in your interceptor by following an approach similar to https://stackoverflow.com/a/67003567