11

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;
        }
    }
}
4
  • Only a guess: Unity cannot resolve IMemoryCache. Maybe the registration for the implementation of ÌMemoryCache` is missing. Commented Dec 15, 2018 at 13:01
  • 1
    are you sure you added AddMemoryCache() on ConfigureServices at startup check this learn.microsoft.com/en-us/aspnet/core/performance/caching/… Commented Dec 15, 2018 at 13:13
  • 1
    @Ahmed Ghoniem AddMemoryCache() belongs to the .net Core. I am using Asp.Net MVC. Commented Dec 17, 2018 at 6:14
  • if I add container.RegisterType<IMemoryCache, MemoryCache>(); registration it says that MemoryCacheOptions registration is missing Commented Sep 24, 2019 at 10:17

2 Answers 2

9
+50

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);
Sign up to request clarification or add additional context in comments.

Comments

-1

To be able to use existing cache implementation based on .net 4.5.2 in .net core I've ported it to use System.Runtime caching instead of System Web. Works like a charm.

Here's official documentation

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.