Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where do I register IMemoryCache in my ASP.NET application?

I have an ASP.NET web application that I created from Visual Studio 2015 Community Edition. The .NET Framework is 4.6.1.

I have no idea which version of ASP.NET my web application is using ASP.NET MVC 4? ASP.NET MVC 5? It's not mentioned anywhere.

I'm trying to register IMemoryCache service (from Microsoft.Extensions.Caching.Memory) into my Microsoft Unity container. However, anything I found about it on Google refers to adding services.AddCaching() in a Startup.cs file.

I don't have a Startup.cs file. I only see Global.asax. Furthermor, all my custom dependencies are registered within UnityConfig.cs which was provided when installing the NuGet package Unity.Mvc which is a Microsoft Unity bootstrapper for ASP.NET by Microsoft.

Any help appreciated.

Edit:

Here's a screenshot of my project:

enter image description here

like image 924
TchiYuan Avatar asked Jul 25 '16 14:07

TchiYuan


Video Answer


3 Answers

In .NET Core AddMemoryCache Extensions looks like this:

services.TryAdd(ServiceDescriptor.Singleton<IMemoryCache, MemoryCache>());

So in .Net Framework you can use something like this (for Autofac):

builder.RegisterType<MemoryCache>().As<IMemoryCache>().SingleInstance();

UPD: However MemoryCachedependends on IOptions<> you should register it too:

builder.RegisterGeneric(typeof(OptionsManager<>)).As(typeof(IOptions<>)).SingleInstance();
builder.RegisterGeneric(typeof(OptionsManager<>)).As(typeof(IOptionsSnapshot<>)).InstancePerLifetimeScope();
builder.RegisterGeneric(typeof(OptionsMonitor<>)).As(typeof(IOptionsMonitor<>)).SingleInstance();
builder.RegisterGeneric(typeof(OptionsFactory<>)).As(typeof(IOptionsFactory<>));
builder.RegisterGeneric(typeof(OptionsCache<>)).As(typeof(IOptionsMonitorCache<>)).SingleInstance();
like image 157
ondator Avatar answered Sep 16 '22 20:09

ondator


The things you are referring to are part of the ASP.Net Core (ASP.Net 5)

In the new version you no longer have a Global.asax file, you have a new Startup processed defined in a Startup class. Also, DI is standard, so things work in a different way now. So either upgrade to the latest version of ASP.Net or apply use the DI solutions for the older version:

RESOLVING DEPENDENCIES IN ASP.NET MVC 5 USING UNITY CONTAINER

Also the package you are using is for the new ASP.NET version.

Introduction to ASP.NET Core

like image 42
MeTitus Avatar answered Sep 17 '22 20:09

MeTitus


If you are using Unity, you can do something similar like this in UnityConfig.cs

var memoryCache = new MemoryCacheService(new MemoryCacheOptions());
            container.RegisterInstance<IMemoryCache>(memoryCache);

create your MemoryCacheService

public class MemoryCacheService : MemoryCache
    {
        public MemoryCacheService(IOptions<MemoryCacheOptions> optionsAccessor) : base(optionsAccessor)
        {
        }
    }

yourcontroller

    private readonly IMemoryCache _memoryCacheService;
public yourcontroller(IMemoryCache memoryCacheService)
{
            _memoryCacheService = memoryCacheService;
        }

    public IHttpActionResult AddCacheItem(string cacheKey)
            {
                _memoryCacheService.Set(cacheKey, DateTime.Now.Ticks );
                var temp = _memoryCacheService.Get(cacheKey);
                return Ok(temp);
            }
like image 22
Grey Wolf Avatar answered Sep 16 '22 20:09

Grey Wolf