简体   繁体   English

使用内存缓存时,Asp.Net MVC中的InvalidOperationException

[英]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: 我需要使用.NetFramework 4.5.2在我的网站上应用In-Memory Cache ,但出现此异常:

Unity.Exceptions.ResolutionFailedException: 'Resolution of the dependency failed, type = 'Tranship.UI.Areas.Portal.Controllers.SearchResultController', name = '(none)'. Unity.Exceptions.ResolutionFailedException:'解决依赖关系失败,类型='Tranship.UI.Areas.Portal.Controllers.SearchResultController',名称='(无)'。 Exception occurred while: while resolving. 发生以下异常:正在解决。 Exception is: InvalidOperationException - The current type, Microsoft.Extensions.Caching.Memory.IMemoryCache, is an interface and cannot be constructed. 异常是:InvalidOperationException-当前类型Microsoft.Extensions.Caching.Memory.IMemoryCache是​​一个接口,无法构造。 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: 我正在使用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;
        }
    }
}

You need to register IMemoryCache to Unity container 您需要将IMemoryCache注册到Unity容器

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);

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. 为了能够在.net核心中使用基于.net 4.5.2的现有缓存实现,我将其移植为使用System.Runtime缓存而不是System Web。 Works like a charm. 奇迹般有效。

Here's official documentation 这是官方文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.NET 内核将带 IMemoryCache 的内存缓存转换为 Redis - ASP.NET Core Converting In-Memory Cache with IMemoryCache to Redis PostSharp Caching MethodInterceptionAspect 使用 ASP.NET Core 内存缓存 - PostSharp Caching MethodInterceptionAspect using ASP.NET Core in-memory cache 运行时出现 InvalidOperationException ASP.NET Core MVC Web App - InvalidOperationException while running ASP.NET Core MVC Web App InvalidOperationException和ASP.NET MVC中的工作流 - InvalidOperationException and Workflows in ASP.NET MVC 在Asp.net MVC中收到“ InvalidOperationException:” - Receiving “InvalidOperationException:” in Asp.net MVC 浏览器中 ASP.NET Core MVC 中的 InvalidOperationException - InvalidOperationException in ASP.NET Core MVC in Browser 我可以在 ASP.NET 内核中为 SQL 服务器分布式缓存使用内存引擎吗? - Can I use In-Memory Engine for SQL Server Distributed Cache in ASP.NET Core? MVC ASP.NET正在使用大量内存 - MVC ASP.NET is using a lot of memory 如何使用Cache obect缓存数据asp.net mvc - How to cache data asp.net mvc using Cache obect 如何使用Flurl测试在内存中运行的ASP.NET Web API? - How to test an ASP.NET Web API running in-memory using Flurl?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM