Ecozum.Autofac.Extensions.Interception 1.1.2

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.

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.RegisterType<WeatherForecastService>()
               .As<IWeatherForecastService>()
               .InstancePerLifetimeScope()
               .InterfaceIntercept(typeof(LoggingInterceptor), 
                                   typeof(DistributedCacheInterceptor),
                                   typeof(TraceInterceptor));
        
        builder.RegisterType<SomeService>()
               .As<ISomeService>()
               .InstancePerLifetimeScope()
               .InterfaceIntercept(typeof(TraceInterceptor));
    }
}
//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;

    public WeatherForecastService(ISomeService someService)
    {
        _someService = someService;
    }
    
    [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 randomString.Substring(0, 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);
        }
    }
}

Output

[09:03:51 INF] AutofacInterceptorExample Development EMRECAGLAR ::1 4d524efe-83eb-435b-898d-182b14dd7289 trace-example #2 Generate Random String [String SomeService.RandomString()] TraceCorrelation: "129439ff-d0ca-4681-81af-69954ab5c415" Elapsed: 12ms - {"args":{},"ReturnValue":"F4nLy8HbQ068LijPSaAZTo9Cz4tnr295/UYvnhySg8e/tzHG7L7hb5NxTuye06BDfpZ6lAZKCBa4S+7uOflbiQ=="}

[09:03:51 INF] AutofacInterceptorExample Development EMRECAGLAR ::1 4d524efe-83eb-435b-898d-182b14dd7289 trace-example #1 Random Secret Key [String WeatherForecastService.GenerateSecretKey(Int32 len)] TraceCorrelation: "129439ff-d0ca-4681-81af-69954ab5c415" Elapsed: 13ms - geçen süre {"args":{"len":32},"ReturnValue":"F4nLy8HbQ068LijPSaAZTo9Cz4tnr295"}

No packages depend on Ecozum.Autofac.Extensions.Interception.

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