Skip to content

Instantly share code, notes, and snippets.

@ankitvijay
ankitvijay / DistributedLockTests.cs
Last active Feb 28, 2021
Distributed Lock TestsDistributed Lock Tests
View DistributedLockTests.cs
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Xunit;
using Xunit.Abstractions;
namespace PostgreSQLDistributedLock.Tests
{
public class DistributedLockTests
@ankitvijay
ankitvijay / DistributedLock.cs
Last active Feb 28, 2021
DistributedLockTaskDistributedLockTask
View DistributedLock.cs
using System;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Npgsql;
namespace PostgreSQLDistributedLock
{
public sealed class DistributedLock : IDisposable
{
private readonly ILogger<DistributedLock> _logger;
@ankitvijay
ankitvijay / Program.cs
Created Feb 21, 2021
Register scoped Service Register Scoped Service
View Program.cs
services.AddHostedService<SchedulerService>();
services.AddScoped<IScopedSchedulerService, ScopedSchedulerService>();
@ankitvijay
ankitvijay / SchedulerService.cs
Created Feb 21, 2021
Scheduler Background Service Scope Injection
View SchedulerService.cs
using System;
using System.Threading;
using System.Threading.Tasks;
using Cronos;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
namespace SchedulerJobSample.Worker
{
View ScopedSchedulerService.cs
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
namespace SchedulerJobSample.Worker
{
public interface IScopedSchedulerService
{
Task ExecuteAsync(CancellationToken cancellationToken);
@ankitvijay
ankitvijay / Worker.cs
Last active Feb 19, 2021
Schedule Job every one min
View Worker.cs
using System;
using System.Threading;
using System.Threading.Tasks;
using Cronos;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
namespace SchedulerJobSample.Worker
{
public class SchedulerService : BackgroundService
@ankitvijay
ankitvijay / Worker.cs
Created Feb 19, 2021
Default Background Service
View Worker.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
namespace SchedulerJobSample.Worker
{
@ankitvijay
ankitvijay / HttpClient.cs
Created Nov 23, 2020
Passing Correlation Id to subsequent request
View HttpClient.cs
public class Startup
{
public virtual void ConfigureServices(IServiceCollection services)
{
services.AddHttpClient<IMyServiceClient, MyServiceClient>((serviceProvider, client) =>
{
client.BaseAddress = customerCoreServiceOptions.BaseUri;
client.DefaultRequestHeaders.Add("correlation-id",
CorrelationIdContext.GetCorrelationId() ??
Guid.NewGuid().ToString());
@ankitvijay
ankitvijay / CorrelationIdMiddleware.cs
Last active Nov 23, 2020
CorrelationId Middleware
View CorrelationIdMiddleware.cs
public class CorrelationIdMiddleware
{
private readonly RequestDelegate _next;
public CorrelationIdMiddleware(RequestDelegate next)
{
_next = next;
}
public async Task Invoke(HttpContext context)
@ankitvijay
ankitvijay / CorrelationIdContext.cs
Last active Nov 22, 2020
CorrelationId Context
View CorrelationIdContext.cs
public class CorrelationIdContext
{
private static readonly AsyncLocal<string> _correlationId = new AsyncLocal<string>();
public static void SetCorrelationId(string correlationId)
{
if (string.IsNullOrWhiteSpace(correlationId)
{
throw new ArgumentException("Correlation Id cannot be null or empty", nameof(correlationId));
}