Skip to content

Instantly share code, notes, and snippets.

@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));
}
@ankitvijay
ankitvijay / Program.cs
Created Sep 10, 2020
C# dynamic issue
View Program.cs
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 Aug 7, 2020
Part 5 - Inheritance Example 2 - PaymentType.cs
View 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 Aug 7, 2020
Part 5 - Inheritance Example 1 (PaymentType)
View PaymentType.cs
protected PaymentType(int value, string name = null) : base(value, name)
{
}
@ankitvijay
ankitvijay / StatesPaymentType.cs
Last active Aug 7, 2020
Part 5 - Inheritance Example 2
View StatesPaymentType.cs
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
{
@ankitvijay
ankitvijay / StatesPaymentType.cs
Last active Aug 7, 2020
Part 5 - Inheritance Example 1
View StatesPaymentType.cs
public abstract class StatesPaymentType : PaymentType
{
public static readonly PaymentType Bitcoin = new BitCoinType();
private class BitCoinType : PaymentType
{
public BitCoinType() : base(3, "Bitcoin")
{
}
@ankitvijay
ankitvijay / InheritanceExample1Tests.cs
Last active Aug 7, 2020
Part 5 -InheritanceeExample1TestsPart 5 - InheritanceExample1Tests
View InheritanceExample1Tests.cs
public class InheritanceExample1Tests
{
[Fact]
public void CanReadAllPaymentTypesForUnitedStates()
{
Assert.Equal("CC", StatesPaymentType.CreditCard.Code);
Assert.Equal("DC", StatesPaymentType.DebitCard.Code);
Assert.Equal("BT", StatesPaymentType.Bitcoin.Code);
}
@ankitvijay
ankitvijay / InheritanceExample2Tests.cs
Created Jul 24, 2020
Part 5 - InheritanceExample2Tests
View InheritanceExample2Tests.cs
using System;
using AV.Enumeration.Sample.Inheritance.Example2;
using Xunit;
namespace Enumeration.Sample.Inheritance.Example2.Tests
{
public class InheritanceExample2Tests
{
[Fact]
public void CanReadAllPaymentTypesForUnitedStates()
{
You can’t perform that action at this time.