jcat
jcat

Reputation: 35

How to inject IDistributedCache in a business class

I have an asp.net core project and I'm trying to use IDistributedCache (Redis).

I want to access IDistributedCache in a custom class, not only in a controller.

Is that possible?

Upvotes: 2

Views: 1536

Answers (1)

Nkosi
Nkosi

Reputation: 246998

I want to access IDistributedCache in a custom class, not only in a controller. Is that possible?

Yes.

Register with service collection

var redisconnection = "...";
services.AddDistributedRedisCache(o => { o.Configuration = redisconnection; });
services.AddScoped<MyCustomClass>();

//...

and have it as an injectable argument on custom class

public MyCustomClass(IDistributedCache cache) {
    //...
}

Upvotes: 2

Related Questions