Skip to content

Instantly share code, notes, and snippets.

View AnthonyGiretti's full-sized avatar
💭
👍

[MVP] Anthony Giretti AnthonyGiretti

💭
👍
View GitHub Profile
@AnthonyGiretti
AnthonyGiretti / ServiceCollectionHelper.cs
Last active April 8, 2026 00:50
Seeking Validators by reflection when Using FluentValidation <= 11.4
var implementationTypes = AppDomain.CurrentDomain
.GetAssemblies()
.SelectMany(x => x.GetTypes())
.Where(t => t.GetInterface(typeof(IValidator<>).FullName) != null)
.Where(t => !t.Name.Contains("InlineValidator") && !t.Name.Contains("AbstractValidator"))
.ToList();
@AnthonyGiretti
AnthonyGiretti / IValidator.cs
Created April 8, 2026 00:48
Old vs new FluentValidation IValidator interface
// Before (FluentValidation ≤ 11.4)
public interface IValidator<T>
// After (FluentValidation > 11.4)
public interface IValidator<in T>
@AnthonyGiretti
AnthonyGiretti / IServiceCollection.cs
Created April 8, 2026 00:30
Nex version of AddValidators method in the Calzolari.Grpc.AspNetCore.Validation Nuget package which allows the usage of FluentValidation 11.4+ version
public static IServiceCollection AddValidators(this IServiceCollection services,
ServiceLifetime lifetime = ServiceLifetime.Scoped)
{
var fluentValidationAssembly = typeof(IValidator).Assembly;
var implementationTypes = AppDomain.CurrentDomain
.GetAssemblies()
.SelectMany(x =>
{
try { return x.GetTypes(); }
@AnthonyGiretti
AnthonyGiretti / CountryService.cs
Last active April 5, 2026 02:25
Using EF Core to stream data from database
public class CountryService
{
private readonly IDbContextFactory<CountryDbContext> _contextFactory;
public CountryService(IDbContextFactory<CountryDbContext> contextFactory)
=> _contextFactory = contextFactory;
public async IAsyncEnumerable<Country> StreamCountries(
[EnumeratorCancellation] CancellationToken ct = default)
{
@AnthonyGiretti
AnthonyGiretti / Program.cs
Created April 4, 2026 18:53
ASP.NET Core: Using JWT for Authentication from Query string (Exemple: Server Sent Event scenario)
builder.Services.AddAuthentication()
.AddJwtBearer(options => {
options.Events = new JwtBearerEvents {
OnMessageReceived = context => {
var accessToken = context.Request.Query["access_token"];
// If the request is for the SSE endpoint, grab the token from the query string
var path = context.HttpContext.Request.Path;
if (!string.IsNullOrEmpty(accessToken) && path.StartsWithSegments("/your-sse-endpoint")) {
context.Token = accessToken;
}
@AnthonyGiretti
AnthonyGiretti / Program.cs
Last active April 5, 2026 02:32
ASP.NET Core 10 SSE implementing SseItem<T> enabling resume-on-connect with the EventId
using System.Net.ServerSentEvents;
app.MapGet("/countries/stream", (CountryService service, HttpRequest request, CancellationToken ct) =>
{
// On reconnect, the browser sends this header automatically
int.TryParse(request.Headers["Last-Event-ID"], out var lastEventId);
async IAsyncEnumerable<SseItem<Country>> Stream(
[EnumeratorCancellation] CancellationToken token)
{
@AnthonyGiretti
AnthonyGiretti / CountryListComponent.ts
Created April 4, 2026 18:43
Angular component streaming data from a streaming service
import { Component, inject, DestroyRef } from '@angular/core';
import { CountryStreamService } from './country-stream.service';
@Component({
template: `
@for (country of countryStream.countries(); track country.id) {
<div>{{ country.name }} — {{ country.population | number }}</div>
}
@if (countryStream.error(); as err) {
<p class="error">{{ err }}</p>
@AnthonyGiretti
AnthonyGiretti / CountryService.cs
Last active April 5, 2026 02:27
Using EF Core to stream data from database handling paging
public class CountryService
{
private readonly IDbContextFactory<CountryDbContext> _contextFactory;
public CountryService(IDbContextFactory<CountryDbContext> contextFactory)
=> _contextFactory = contextFactory;
public async IAsyncEnumerable<Country> StreamCountries(
int startAfterId = 0,
[EnumeratorCancellation] CancellationToken ct = default)
@AnthonyGiretti
AnthonyGiretti / CountryDbContext.cs
Last active April 4, 2026 18:48
Example of a DbContext that carries Countries metadata over a Country database entity
public class Country
{
public int Id { get; set; }
public required string Name { get; set; }
public required string Code { get; set; }
public long Population { get; set; }
}
public class CountryDbContext : DbContext
{
@AnthonyGiretti
AnthonyGiretti / Program.cs
Created April 3, 2026 16:05
ASP.NET Core 10: Easy support of local multi host: Routing by Hostname
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
// This endpoint only responds to api.localhost
app.MapGet("/", () => "Welcome to the API")
.RequireHost("api.localhost:7001");
// This endpoint only responds to admin.localhost
app.MapGet("/", () => "Welcome to the Admin Dashboard")
.RequireHost("admin.localhost:7002");