Ecozum.Autofac.Extensions.Interception 1.2.9
Introduction
Bu paket, Autofac DI kullanımında interface ve class interceptor kullanımı için oluşturulmuştur. MemoryCache, DistributedCache, Logging ve Trace için ön yüklü interceptor barındırmaktadır.
Getting Started
Interceptor kullanımı interface ve class olarak 2 farklı şekilde uygulanıyor.
Interface için InterfaceIntercept, class için ClassIntercept kullanılmalıdır.
Class interception kullanılacak method'un virtual olarak işaretlenmesi gerekiyor. Aksi takdirde interception yapılamayacaktır.
//Interface interception
builder.RegisterType<SomeService>().As<ISomeService>().InterfaceIntercept(typeof(InterceptType));
//Class interception
builder.RegisterType<SomeUtility>().ClassIntercept(typeof(InterceptType));
Installing
dotnet add package Ecozum.Autofac.Extensions.Interception
Sample
//AutofacModule.cs
public class AutofacModule : Module
{
protected override void Load(ContainerBuilder builder)
{
//Register interceptors
builder.RegisterType<LoggingInterceptor>();
builder.RegisterType<DistributedCacheInterceptor>();
builder.RegisterType<TraceInterceptor>();
builder.RegisterAssemblyTypes(typeof(WeatherForecastController).Assembly)
.Where(t => typeof(ControllerBase).IsAssignableFrom(t))
.PropertiesAutowired()
.ClassIntercept(typeof(TraceInterceptor));
builder.RegisterType<StringUtility>()
.InstancePerLifetimeScope()
.ClassIntercept(typeof(TraceInterceptor));
builder.RegisterType<WeatherForecastService>()
.As<IWeatherForecastService>()
.InstancePerLifetimeScope()
.InterfaceIntercept(typeof(LoggingInterceptor),
typeof(DistributedCacheInterceptor),
typeof(TraceInterceptor));
builder.RegisterType<SomeService>()
.As<ISomeService>()
.InstancePerLifetimeScope()
.InterfaceIntercept(typeof(TraceInterceptor));
}
}
public class SecretKeyModel
{
public int Length { get; set; }
public string OrderId { get; set; }
}
[ApiController]
[Route("api/weather")]
public class WeatherForecastController : ControllerBase
{
private readonly IWeatherForecastService _weatherForecastService;
private readonly ILogger<WeatherForecastController> _logger;
public WeatherForecastController(IWeatherForecastService weatherForecastService, ILogger<WeatherForecastController> logger)
{
_weatherForecastService = weatherForecastService;
_logger = logger;
}
[HttpPost("secretKey/")]
[Trace("trace-example", Ref = "model.OrderId", Description = "secret key request", LogInputOutput = true, LogException = true)]
public virtual string Get([FromBody]SecretKeyModel model)
{
return _weatherForecastService.GenerateSecretKey(model.Length);
}
}
//WeatherForecastService.cs
public interface IWeatherForecastService
{
WeatherForecast[] GetForRegion(WeatherRequest region);
void NoForCaching();
Guid FixCacheKey();
string GenerateSecretKey(int len);
}
public class WeatherForecastService : IWeatherForecastService
{
private readonly ISomeService _someService;
private readonly StringUtility _stringUtility;
public WeatherForecastService(ISomeService someService, StringUtility stringUtility)
{
_someService = someService;
_stringUtility = stringUtility;
}
[Cache("region.City+region.District", KeyType = CacheKeyType.ByParameter)]
public void NoForCaching()
{
Debug.WriteLine("Test the" + nameof(NoForCaching));
}
[Cache("region.City+region.District", KeyType = CacheKeyType.ByParameter)]
public WeatherForecast[] GetForRegion(WeatherRequest region)
{
string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot","Sweltering", "Scorching"
};
var rng = new Random();
var now = DateTime.Now;
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
MeasurementedAt = now,
Date = DateTime.Now.AddDays(index),
TemperatureC = rng.Next(-20, 55),
Summary = Summaries[rng.Next(Summaries.Length)]
})
.ToArray();
}
[Cache("fixed", AbsoluteExpirationAsMinute = 1)]
public Guid FixCacheKey()
{
return Guid.NewGuid();
}
[Trace("trace-example", Description = "Random Secret Key", LogInputOutput = true,LogException = true)]
public string GenerateSecretKey(int len)
{
string randomString = _someService.RandomString();
return _stringUtility.Cut(randomString, len);
}
}
//SomeService.cs
public interface ISomeService
{
string RandomString();
}
public class SomeService : ISomeService
{
[Trace("trace-example", Description = "Generate Random String", LogInputOutput = true,LogException = true)]
public string RandomString()
{
using (RNGCryptoServiceProvider provider = new RNGCryptoServiceProvider())
{
byte[] bytes = new byte[64];
provider.GetBytes(bytes);
return Convert.ToBase64String(bytes);
}
}
}
public class StringUtility
{
[Trace("trace-example", Description = "String Cut", LogInputOutput = true, LogException = true)]
public virtual string Cut(string text, int length)
{
return text.Substring(0, length);
}
}
Output
[23:37:15 INF] AutofacInterceptorExample Development EMRECAGLAR ::1 d81b6a10-598b-4efd-af0f-5b842f6992b2 trace-example #3 NT1625P000123 Generate Random String [String SomeService.RandomString()] Elapsed: 11ms - {"args":{},"ReturnValue":"7yA/nfuL9EtmVfLZKUgHZunaHlMzxw66dJLknPtkcIeG3rY+/+wBA0UQNRZmANnkF5devR+NJC2vYjP5LdXHPg=="}
[23:37:15 INF] AutofacInterceptorExample Development EMRECAGLAR ::1 d81b6a10-598b-4efd-af0f-5b842f6992b2 trace-example #4 NT1625P000123 String Cut [String StringUtility.Cut(String text, Int32 length)] Elapsed: 0ms - {"args":{"text":"7yA/nfuL9EtmVfLZKUgHZunaHlMzxw66dJLknPtkcIeG3rY+/+wBA0UQNRZmANnkF5devR+NJC2vYjP5LdXHPg==","length":16},"ReturnValue":"7yA/nfuL9EtmVfLZ"}
[23:37:15 INF] AutofacInterceptorExample Development EMRECAGLAR ::1 d81b6a10-598b-4efd-af0f-5b842f6992b2 trace-example #2 NT1625P000123 Random Secret Key [String WeatherForecastService.GenerateSecretKey(Int32 len)] Elapsed: 22ms - {"args":{"len":16},"ReturnValue":"7yA/nfuL9EtmVfLZ"}
[23:37:15 INF] AutofacInterceptorExample Development EMRECAGLAR ::1 d81b6a10-598b-4efd-af0f-5b842f6992b2 trace-example #1 NT1625P000123 secret key request [String WeatherForecastController.Get(SecretKeyModel model)] Elapsed: 24ms - {"args":{"model":{"Length":16,"OrderId":"NT1625P000123","$type":"SecretKeyModel"}},"ReturnValue":"7yA/nfuL9EtmVfLZ"}
No packages depend on Ecozum.Autofac.Extensions.Interception.
.NET Standard 2.0
- Autofac.Extensions.DependencyInjection (>= 7.2.0)
- Autofac.Extras.DynamicProxy (>= 6.0.1)
- Microsoft.Extensions.Caching.Abstractions (>= 3.1.24)
- Microsoft.Extensions.Logging (>= 3.1.24)
- System.Text.Json (>= 6.0.8)
| Version | Downloads | Last updated |
|---|---|---|
| 1.3.8 | 307 | 5.01.2024 |
| 1.3.7 | 165 | 2.11.2023 |
| 1.3.6 | 176 | 2.11.2023 |
| 1.3.5 | 146 | 2.11.2023 |
| 1.3.4 | 167 | 2.11.2023 |
| 1.3.3 | 162 | 31.08.2023 |
| 1.3.2 | 153 | 31.08.2023 |
| 1.3.1 | 145 | 18.08.2023 |
| 1.3.0 | 174 | 18.08.2023 |
| 1.2.9 | 142 | 18.08.2023 |
| 1.2.6 | 280 | 6.09.2022 |
| 1.2.5 | 168 | 26.08.2022 |
| 1.2.4 | 134 | 16.08.2022 |
| 1.2.3 | 189 | 9.08.2022 |
| 1.2.2 | 183 | 9.08.2022 |
| 1.2.1 | 171 | 8.08.2022 |
| 1.2.0 | 183 | 8.08.2022 |
| 1.1.9 | 154 | 8.08.2022 |
| 1.1.8 | 146 | 8.08.2022 |
| 1.1.7 | 132 | 5.08.2022 |
| 1.1.6 | 165 | 5.08.2022 |
| 1.1.5 | 172 | 5.08.2022 |
| 1.1.4 | 216 | 25.05.2022 |
| 1.1.3 | 163 | 24.05.2022 |
| 1.1.2 | 155 | 24.05.2022 |
| 1.0.0 | 170 | 28.04.2022 |