Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

InvalidOperationException in Asp.Net MVC while using In-Memory Cache

I need to apply In-Memory Cache on my website with.NetFramework 4.5.2 but I get this exception:

Unity.Exceptions.ResolutionFailedException: 'Resolution of the dependency failed, type = 'Tranship.UI.Areas.Portal.Controllers.SearchResultController', name = '(none)'. Exception occurred while: while resolving. Exception is: InvalidOperationException - The current type, Microsoft.Extensions.Caching.Memory.IMemoryCache, is an interface and cannot be constructed. Are you missing a type mapping?

I am using Asp.net MVC (not Core) and using Microsoft.Extensions.Caching.Memory version 1.1.2 This is my cs file:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tranship.Business.Core;
using Tranship.Business.Interface;
using Tranship.DataAccess.UnitOfWork;
using Tranship.Domain.Context;
using Tranship.Domain.Model;
using Tranship.DomainService.Interface;
using Tranship.ViewModel.Model;
using Tranship.ViewModel.Mapper;
using Tranship.ViewModel.Parameter;
using Microsoft.Extensions.Caching.Memory;

namespace Tranship.DomainService.Core
{
    public class ScheduleDomainService : IScheduleDomainService
    {
        private readonly IMemoryCache MemoryCache;
        private readonly string key = "TranshipMemoryCache";
        public BoundedContextUnitOfWork Context { get; set; }
        public IScheduleBiz ScheduleBiz { get; set; }
        public ScheduleDomainService(IMemoryCache memoryCache)
        {
            Context = new BoundedContextUnitOfWork(new BoundedContext());
            ScheduleBiz = new ScheduleBiz(Context);
            MemoryCache = memoryCache;
        }
        public List<ScheduleViewModel> GetScheduleBySearchParameter(SearchTripParameters parameters)
        {
            DateTime from;
            DateTime to;
            List<ScheduleViewModel> cacheObject = new List<ScheduleViewModel>();
            if (!MemoryCache.TryGetValue(key, out cacheObject))
            {
                // Cache is empty or timespan has been terminated
                cacheObject = ScheduleBiz.GetAll();
                MemoryCache.Set(key, cacheObject, new MemoryCacheEntryOptions().SetAbsoluteExpiration(TimeSpan.FromHours(1)));
            }
            else
            {
                // Cache is full
                cacheObject = MemoryCache.Get(key) as List<ScheduleViewModel>;
            }
            return cacheObject;
        }
    }
}
like image 860
Sasan Karimi Avatar asked Dec 15 '18 10:12

Sasan Karimi


People also ask

When should you use cache memory?

Accelerating online database applications is the most common use case for in-memory caching. For example, a high-traffic website storing content in a database will significantly benefit from the in-memory cache.

Is IMemoryCache a singleton?

Note that the MemoryCache is a singleton, but within the process. It is not (yet) a DistributedCache. Also note that Caching is Complex(tm) and that thousands of pages have been written about caching by smart people.

How can get data from cache in asp net?

From an ASP.NET page s code-behind class, the data cache can be accessed using the Page class s Cache property, and allows for syntax like Cache["key"] = value , as discussed in Step 2. From a class within the architecture, the data cache can be accessed using either HttpRuntime. Cache or HttpContext. Current.

How can set cache control no store in asp net?

AppendHeader("Pragma", "no-cache"); Response. AppendHeader("Expires", "0"); The first line sets Cache-control to no-cache , and the second line adds the other attributes no-store, must-revalidate . This may not be the only way, but does provide an alternative method if the more straightforward Response.


1 Answers

You need to register IMemoryCache to Unity container

using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Options;

UnityContainer container = new UnityContainer(); // your unity container

MemoryCacheOptions memoryCacheOptions = new MemoryCacheOptions
{
    //config your cache here
};

MemoryCache memoryCache = new MemoryCache(Options.Create<MemoryCacheOptions>(memoryCacheOptions));
container.RegisterInstance<IMemoryCache>(memoryCache);
like image 198
Kahbazi Avatar answered Oct 05 '22 18:10

Kahbazi