Files
Net.Sdk.Web.Extensions/AspNetCore.Extensions/HttpContextExtensions.cs
T
2024-08-16 11:24:48 +02:00

28 lines
787 B
C#

using Microsoft.CorrelationVector;
using System.Core.Extensions;
namespace AspNetCore.Extensions;
public static class HttpContextExtensions
{
private const string CorrelationVectorKey = "CorrelationVector";
public static void SetCorrelationVector(this HttpContext context, CorrelationVector cv)
{
context.ThrowIfNull()
.Items.Add(CorrelationVectorKey, cv);
}
public static CorrelationVector GetCorrelationVector(this HttpContext context)
{
context.ThrowIfNull();
if (!context.Items.TryGetValue(CorrelationVectorKey, out var cvVal) ||
cvVal is not CorrelationVector cv)
{
throw new InvalidOperationException("Unable to extract API Key from context");
}
return cv;
}
}