Skip to content

Instantly share code, notes, and snippets.

View AnthonyGiretti's full-sized avatar
💭
👍

[MVP] Anthony Giretti AnthonyGiretti

💭
👍
View GitHub Profile
@AnthonyGiretti
AnthonyGiretti / UserInput.cs
Created November 24, 2025 01:30
ASP.NET Core DataAnnotation unifications
// Before ASP.NET Core 10, depending on the scenario:
// Some attributes were ignored
// Record struct parameters weren’t validated consistently
// Constructor binding didn’t always trigger annotation checks.
public readonly record struct UserInput(
[Required] string Name,
[Range(18, 99)] int Age);
@AnthonyGiretti
AnthonyGiretti / Program.cs
Created November 24, 2025 01:21
ASP.NET Core 10: Easy support of local multi host: Program.cs
var builder = WebApplication.CreateBuilder(args);
....
builder.WebHost.UseUrls(
"https://api.localhost:7001",
"https://admin.localhost:7002",
"https://frontend.localhost:7003");
var app = builder.Build();
@AnthonyGiretti
AnthonyGiretti / appsettings.Development.json
Created November 24, 2025 01:19
ASP.NET Core 10: Easy support of local multi host: appsettings.Development.json
{
"Kestrel": {
"Endpoints": {
"Api": {
"Url": "https://api.localhost:7001"
},
"Admin": {
"Url": "https://admin.localhost:7002"
},
"Frontend": {
@AnthonyGiretti
AnthonyGiretti / launchsettings.json
Last active November 24, 2025 01:18
ASP.NET Core 10: Easy support of local multi host: launchsettings.json
{
"profiles": {
"ApiApp": {
"commandName": "Project",
"dotnetRunMessages": true,
"applicationUrl": "https://api.localhost:7001",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
@AnthonyGiretti
AnthonyGiretti / Program.cs
Created November 24, 2025 01:05
ASP.NET Core 10: Clearer Separation Between Middlewares and Endpoints for More Predictable Routing
// Before ASP.NET Core 10
// Order matters
app.UseAuthentication();
app.UseRouting();
app.UseAuthorization(); // This might run before endpoint metadata is fully known
app.MapControllers();
// With ASP.NET Core 10
// Order does not matter
app.UseRouting();
@AnthonyGiretti
AnthonyGiretti / Program.cs
Last active November 24, 2025 00:59
ASP.NET Core 10: New OpenTelemetry metrics for Identity
builder.Services
.AddOpenTelemetry()
.ConfigureResource(r => r.AddService(
serviceName: builder.Environment.ApplicationName,
serviceVersion: "1.0.0"))
.WithMetrics(metrics =>
{
// HTTP pipeline instrumentation (http.server.* etc.)
metrics.AddAspNetCoreInstrumentation();
@AnthonyGiretti
AnthonyGiretti / CountryStreamService.ts
Created November 24, 2025 00:43
Angular SSE handling
import { Injectable, NgZone } from '@angular/core';
import { Observable } from 'rxjs';
import { Country } from './country.model';
@Injectable({ providedIn: 'root' })
export class CountryStreamService {
private readonly url = 'https://localhost:5001/countries/stream';
constructor(private zone: NgZone) {}
@AnthonyGiretti
AnthonyGiretti / Program.cs
Last active November 24, 2025 00:40
Server-sent event in ASP.NET Core 10
var app = builder.Build();
app.UseCors();
// SSE endpoint in ASP.NET Core 10
app.MapGet("/countries/stream", (CountryService service, CancellationToken ct) =>
{
var stream = service.StreamCountries(ct);
// Each Country will be sent as an SSE event named "country"
@AnthonyGiretti
AnthonyGiretti / AudioAnalyzer.cs
Created November 23, 2025 23:29
C# 14: More Implicit Conversions for Span<T> and ReadOnlySpan<T>
public static class AudioAnalyzer
{
public static double AverageLevel(ReadOnlySpan<byte> data)
=> data.IsEmpty ? 0 : data.ToArray().Average(b => (double)b);
}
var bytes = track.RawData; // byte[]
// Before C# 14
AudioAnalyzer.AverageLevel(bytes.AsSpan()); // implicit conversion
@AnthonyGiretti
AnthonyGiretti / nullassignement.cs
Last active November 24, 2025 17:26
Before C# 14 and with C# 14 null conditional assignment
// Not allowed before C# 14, does not compile
track?.Info = new Metadata();
track?.Info?.Duration = TimeSpan.Zero;
playlist?["title"] = "My Mix";
// Must do this
if (track != null && track.Info != null)
{
track.Info.Duration = TimeSpan.FromMinutes(5);
}