Skip to content

Instantly share code, notes, and snippets.

@ankitvijay
ankitvijay / ScopedSchedulerService.cs
Created February 21, 2021 19:54
Scoped Scheduler Service
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 February 19, 2021 19:18
Schedule Job every one min
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 February 19, 2021 18:39
Default Background Service
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 November 23, 2020 20:56
Passing Correlation Id to subsequent request
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 November 23, 2020 20:41
CorrelationId Middleware
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 November 22, 2020 20:42
CorrelationId Context
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));
}
@ankitvijay
ankitvijay / Program.cs
Created September 10, 2020 20:33
C# dynamic issue
class Program
{
public static void Main()
{
dynamic data = new { SomeProperty = "ABC" };
var response = IsTrue(data);
if (response == "1")
{
Console.WriteLine("How can this compile?");
@ankitvijay
ankitvijay / PaymentType.cs
Created August 7, 2020 22:45
Part 5 - Inheritance Example 2 - PaymentType.cs
public abstract class PaymentType : Enumeration
{
public static readonly PaymentType DebitCard = new DebitCardType();
public static readonly PaymentType CreditCard = new CreditCardType();
public abstract string Code { get; }
protected PaymentType(int value, string name = null) : base(value, name)
{
@ankitvijay
ankitvijay / PaymentType.cs
Created August 7, 2020 22:38
Part 5 - Inheritance Example 1 (PaymentType)
protected PaymentType(int value, string name = null) : base(value, name)
{
}
@ankitvijay
ankitvijay / StatesPaymentType.cs
Last active August 7, 2020 22:34
Part 5 - Inheritance Example 2
public abstract class StatesPaymentType : PaymentType
{
public new static readonly PaymentType DebitCard = new DebitCardType();
public new static readonly PaymentType CreditCard = new CreditCardType();
public static readonly PaymentType Bitcoin = new BitCoinType();
private class BitCoinType : PaymentType
{