使用内存缓存时 Asp.Net MVC 中的 InvalidOperationException
InvalidOperationException in Asp.Net MVC while using In-Memory Cache
我需要在我的网站上使用 .NetFramework 4.5.2
应用 In-Memory Cache
,但我遇到了这个异常:
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?
我正在使用 Asp.net MVC(不是核心)并使用 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;
}
}
}
为了能够在 .net 核心中使用基于 .net 4.5.2 的现有缓存实现,我将其移植为使用 System.Runtime 缓存 而不是系统网络。很有魅力。
您需要将 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);
我需要在我的网站上使用 .NetFramework 4.5.2
应用 In-Memory Cache
,但我遇到了这个异常:
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?
我正在使用 Asp.net MVC(不是核心)并使用 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;
}
}
}
为了能够在 .net 核心中使用基于 .net 4.5.2 的现有缓存实现,我将其移植为使用 System.Runtime 缓存 而不是系统网络。很有魅力。
您需要将 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);