Skip to main content
ParityEdge
C# (.NET / ASP.NET)
Backend Runtimes

C# (.NET 8/9 & ASP.NET Core) Backend Integration

Resolve localized purchasing power parity pricing server-side in ASP.NET Core Web APIs, Minimal APIs, Blazor Server, and worker microservices with sub-10ms Anycast resolution.

Production Typed Service with In-Memory Caching

Register a singleton service that uses IHttpClientFactory and IMemoryCache to cache regional pricing lookups with zero lock contention:

// Services/ParityPricingService.cs
using System.Net.Http.Json;
using Microsoft.Extensions.Caching.Memory;

namespace YourApp.Services;

public record ParityResponse(
    bool Eligible,
    int DiscountPercentage,
    string CountryCode,
    string CountryName,
    string Currency,
    string CouponCode,
    int Tier
);

public interface IParityPricingService
{
    Task<(decimal FinalPrice, ParityResponse? Parity)> CalculatePriceAsync(decimal basePrice, string clientIp);
}

public class ParityPricingService : IParityPricingService
{
    private readonly HttpClient _httpClient;
    private readonly IMemoryCache _cache;
    private const string ProjectId = "prj_live_YOUR_KEY";
    private const string EdgeApiUrl = "https://api.parityedge.app/v1/lookup";

    public ParityPricingService(HttpClient httpClient, IMemoryCache cache)
    {
        _httpClient = httpClient;
        _cache = cache;
        _httpClient.Timeout = TimeSpan.FromMilliseconds(800);
    }

    public async Task<(decimal FinalPrice, ParityResponse? Parity)> CalculatePriceAsync(decimal basePrice, string clientIp)
    {
        var cacheKey = $"parity_{clientIp}";

        if (!_cache.TryGetValue(cacheKey, out ParityResponse? parity))
        {
            try
            {
                using var request = new HttpRequestMessage(HttpMethod.Get, $"{EdgeApiUrl}?projectId={ProjectId}");
                if (!string.IsNullOrWhiteSpace(clientIp))
                {
                    request.Headers.Add("x-forwarded-for", clientIp);
                }

                using var response = await _httpClient.SendAsync(request);
                if (response.IsSuccessStatusCode)
                {
                    parity = await response.Content.ReadFromJsonAsync<ParityResponse>();
                    _cache.Set(cacheKey, parity, TimeSpan.FromMinutes(10));
                }
            }
            catch
            {
                // Fail-open guarantee: Default safely to standard base price
                parity = null;
            }
        }

        if (parity != null && parity.Eligible && parity.DiscountPercentage > 0)
        {
            var discountMultiplier = (100m - parity.DiscountPercentage) / 100m;
            var discountedPrice = Math.Round(basePrice * discountMultiplier, 2);
            return (discountedPrice, parity);
        }

        return (basePrice, parity);
    }
}

Program.cs Dependency Injection & Minimal API Endpoint

// Program.cs (.NET 8/9 Minimal API)
using Microsoft.AspNetCore.HttpOverrides;
using YourApp.Services;

var builder = WebApplication.CreateBuilder(args);

// Register MemoryCache and Typed HTTP Client
builder.Services.AddMemoryCache();
builder.Services.AddHttpClient<IParityPricingService, ParityPricingService>();

var app = builder.Build();

// Enable Forwarded Headers for reverse proxies
app.UseForwardedHeaders(new ForwardedHeadersOptions
{
    ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
});

app.MapGet("/api/pricing", async (HttpContext context, IParityPricingService pricingService) =>
{
    var clientIp = context.Connection.RemoteIpAddress?.ToString() ?? "";
    var basePrice = 49.00m;

    var (finalPrice, parity) = await pricingService.CalculatePriceAsync(basePrice, clientIp);

    return Results.Ok(new
    {
        BasePrice = basePrice,
        FinalPrice = finalPrice,
        Parity = parity
    });
});

app.Run();

Enterprise Security & Reliability Highlights

Fail-Open Circuit Safety

If an edge timeout occurs or outbound network connectivity is interrupted, the pricing service fails open to baseline USD rates without throwing exceptions or blocking checkout.

Anti-VPN Threat Inspection

The ParityEdge Anycast engine verifies Autonomous System Numbers (ASNs) against commercial hosting ranges, preventing automated geographic discount abuse.