How to solve problem with IMemoryCache, which is passed to another project

394 Views Asked by At

I would like to ask you for help in solving the problem with IMemoryCache. There problem appears when i add the value to the IMemoryCache in the second project, and the value is not available in the first one.

First project code:

builder.Services.AddMemoryCache();
builder.Services.AddIntoMemoryCache(); //Extension from second project where i add to memory cache

Second project code:

public static class ServiceCollectionExtension
{
    public static void AddIntoMemoryCache(this IServiceCollection serviceCollection)
    {
        using var serviceProvider = serviceCollection.BuildServiceProvider();
        var memoryCacheService = serviceProvider.GetService<IMemoryCache>();

        memoryCacheService.Set(1, 1, TimeSpan.FromMinutes(10)); //Values is added
    }
}

First project code (there is no value added in second project):

using var serviceProvider = builder.Services.BuildServiceProvider();
var memoryCacheService = serviceProvider.GetService<IMemoryCache>();
var values = memoryCacheService; // 0 values

Thanks for all answers.

*Edited: IMemoryCacheService in first project and second has different object id.

0

There are 0 best solutions below