InvalidOperationException в Asp.Net MVC при использовании In-Memory Cache - PullRequest
0 голосов
/ 15 декабря 2018

Мне нужно применить In-Memory Cache на моем веб-сайте с .NetFramework 4.5.2, но я получаю это исключение:

Unity.Exceptions.ResolutionFailedException: 'Не удалось разрешить зависимость, type =' Tranship.UI.Areas.Portal.Controllers.SearchResultController ', name =' (none) '.Исключение произошло во время: при разрешении.Исключение: InvalidOperationException - Текущий тип, Microsoft.Extensions.Caching.Memory.IMemoryCache, является интерфейсом и не может быть создан.Вам не хватает отображения типов?

Я использую Asp.net MVC (не Core) и использую Microsoft.Extensions.Caching.Memory version 1.1.2 Это мой cs-файл:

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;
        }
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...