Asp Core Automating Cache with Aspect Programming (Autofac + Dynamic Proxy)

Aspect-Oriented Programming

These behaviors that are not central to the business logic can be added without cluttering your code.

You’ll be using Autofac and DynamicProxy from the Castle project for the examples, below. download source in Github repository. We automatically cache our method using interception.

1-New Project

new project asp core empty template.

2-Instal Nuget Package




add AddDistributedMemoryCache for cache provider. You can have your own provider, like Redis or SQL server distributed.


Autofac Add Registry to Services That Have Been Already Added.

var builder = new ContainerBuilder();builder.Populate(services);

Add interceptor


Add service


build autofac container

AutofacContainer = builder.Build();// this will be used as the service-provider for the application!return new AutofacServiceProvider(AutofacContainer);

4.Add CacheMethodAttribute

5.Add Extension Methods

6-Add CacheInterceptor

7-Add Service

There are a few points to using
- You can use the HTTP cache next to this type of cache
-You can also serialize the message pack using the message pack, which has a good return
- Self-processing is a costly process, but the point is that it is far less expensive than doing it yourself, such as connecting to a database or requesting HTTP

Web Developer, i love .net

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store