Skip to content

Instantly share code, notes, and snippets.

View EngRajabi's full-sized avatar

Mohsen Rajabi EngRajabi

View GitHub Profile
@EngRajabi
EngRajabi / CacheInterceptor.cs
Last active May 6, 2021 12:36
CacheInterceptor.cs
public class CacheInterceptor : IInterceptor
{
private readonly IDistributedCache _distributedCache;
public CacheInterceptor(IDistributedCache distributedCache)
{
_distributedCache = distributedCache;
}
public void Intercept(IInvocation invocation)
@EngRajabi
EngRajabi / CacheMethodAttribute.cs
Created March 10, 2020 07:47
CacheMethodAttribute.cs
[AttributeUsage(AttributeTargets.Method)]
public class CacheMethodAttribute : Attribute
{
public CacheMethodAttribute(int secondsToCache = 10)
{
SecondsToCache = secondsToCache;
}
public int SecondsToCache { get; set; }
}
@EngRajabi
EngRajabi / Extensions.cs
Last active May 6, 2021 12:35
Extensions.cs
public static class Extensions
{
public static byte[] ToByteArray(this Object obj)
{
BinaryFormatter bf = new BinaryFormatter();
using (var ms = new MemoryStream())
{
bf.Serialize(ms, obj);
return ms.ToArray();
}
@EngRajabi
EngRajabi / TestService.cs
Last active May 6, 2021 12:37
TestService.cs
public interface ITestService
{
Result GetName(Param1 param);
}
public class TestService : ITestService
{
public TestService()
{
@EngRajabi
EngRajabi / Worker.cs
Last active September 17, 2020 13:13
Worker.cs
// add to di singleton
public class Worker
{
private readonly ILogger<Worker> _logger;
private readonly IServiceProvider _serviceProvider;
private readonly ActionBlock<SmsReq> _action;
public Worker(IServiceProvider serviceProvider, ILogger<Worker> logger)
{
_serviceProvider = serviceProvider;
@EngRajabi
EngRajabi / SmsReq.cs
Created September 17, 2020 12:14
SmsReq.cs
public class SmsReq
{
// add body, encoding, receipt, from
}
@EngRajabi
EngRajabi / ISmsSender.cs
Created September 17, 2020 12:15
ISmsSender.cs
public interface ISmsSender
{
Task Send(SmsReq model);
}
@EngRajabi
EngRajabi / Workers.cs
Created September 17, 2020 12:19
Workers.cs
// add to di singleton
public class Workers
{
private ConcurrentBag<IDataflowBlock> Actions { get; set; } = new ConcurrentBag<IDataflowBlock>();
public ActionBlock<TInput> AddWork<TInput>(Func<TInput, Task> work) where TInput : class, new()
{
var action = new ActionBlock<TInput>(work);
Actions.Add(action);
@EngRajabi
EngRajabi / UseWorkers.cs
Last active September 17, 2020 12:23
UseWorkers.cs
//fill object
var smsContext = new SmsReq();
var action = _workers.AddWork<SmsReq>(SendSms);
action.Post(smsContext);
private async Task SendSms(SmsReq smsReq)
{
try
@EngRajabi
EngRajabi / ChannelService.cs
Created March 12, 2021 15:19
ChannelService.cs
public class ChannelService<TMessage> where TMessage : class, new()
{
private readonly Channel<TMessage> _serviceChannel;
public ChannelService()
{
_serviceChannel = Channel.CreateBounded<TMessage>(new BoundedChannelOptions(5000)
{
SingleReader = false,
SingleWriter = false,