Skip to content

Instantly share code, notes, and snippets.

View AnthonyGiretti's full-sized avatar
💭
👍

[MVP] Anthony Giretti AnthonyGiretti

💭
👍
View GitHub Profile
@AnthonyGiretti
AnthonyGiretti / Program.cs
Last active January 3, 2024 02:26
C# 12 collection expressions: spread operator
char[] array1 = ['b', 'c'];
char[] array2 = ['d', 'e'];
char[] mergedArray = ['a', .. array1, .. array2, 'f'];
Console.WriteLine(string.Join(",", mergedArray)); // Output: a, b, c, d, e, f
var list1 = new List<int> { 1, 2 };
List<int> mergedList = [ ..list1, 3 ];
Console.WriteLine(string.Join(",", mergedList)); // Output: 1, 2, 3
@AnthonyGiretti
AnthonyGiretti / Programs.cs
Created June 16, 2023 03:14
Example of usage of the new IHttpSysRequestTimingFeature interface
using Microsoft.AspNetCore.Server.HttpSys;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddHttpContextAccessor();
builder.WebHost.UseHttpSys(options =>
{
options.AllowSynchronousIO = false;
options.Authentication.Schemes = AuthenticationSchemes.None;
@AnthonyGiretti
AnthonyGiretti / Program.cs
Created June 16, 2023 03:06
New interface IHttpSysRequestTimingFeature contracts
namespace Microsoft.AspNetCore.Server.HttpSys
{
public interface IHttpSysRequestTimingFeature
{
ReadOnlySpan<long> Timestamps { get; }
bool TryGetTimestamp(HttpSysRequestTimingType timestampType, out long timestamp);
bool TryGetElapsedTime(HttpSysRequestTimingType startingTimestampType, HttpSysRequestTimingType endingTimestampType, out TimeSpan elapsed);
}
}
@AnthonyGiretti
AnthonyGiretti / Program.cs
Created June 14, 2023 03:18
Register in order two chained Exception handlers
using DemoAspNetCore8.ErrorHandling;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddExceptionHandler<TimeOutExceptionHandler>();
builder.Services.AddExceptionHandler<DefaultExceptionHandler>();
var app = builder.Build();
app.UseExceptionHandler(opt => { });
@AnthonyGiretti
AnthonyGiretti / TimeOutExceptionHandler.cs
Created June 14, 2023 03:16
Exception handler that handles only TimeOutException and breaks or not the pipeline execution
using Microsoft.AspNetCore.Diagnostics;
using Microsoft.AspNetCore.Mvc;
using System.Net;
namespace DemoAspNetCore8.ErrorHandling;
public class TimeOutExceptionHandler : IExceptionHandler
{
private readonly ILogger<DefaultExceptionHandler> _logger;
public TimeOutExceptionHandler(ILogger<DefaultExceptionHandler> logger)
@AnthonyGiretti
AnthonyGiretti / Program.cs
Created June 14, 2023 02:57
Register a custom exception handler that implements IExceptionHandler interface
using DemoAspNetCore8.ErrorHandling;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddExceptionHandler<DefaultExceptionHandler>();
var app = builder.Build();
app.UseExceptionHandler(opt => { });
@AnthonyGiretti
AnthonyGiretti / DefaultExceptionHandler.cs
Created June 14, 2023 02:49
Global exception handler that implements IExceptionHandler interface
using Microsoft.AspNetCore.Diagnostics;
using Microsoft.AspNetCore.Mvc;
using System.Net;
namespace DemoAspNetCore8.ErrorHandling;
public class DefaultExceptionHandler : IExceptionHandler
{
private readonly ILogger<DefaultExceptionHandler> _logger;
public DefaultExceptionHandler(ILogger<DefaultExceptionHandler> logger)
@AnthonyGiretti
AnthonyGiretti / Program.cs
Created June 10, 2023 22:44
C# 12 Primary constructor on classes with filed initializers
public class Product(string name, int categoryId)
{
public Product() : this("Default", -1) { ProductDescription = "InvalidProductId"; }
public Product(string name) : this(name, 0) { }
public string Name => name;
public int CategoryId => categoryId;
public string ProductDescription { get; private set; }
}
@AnthonyGiretti
AnthonyGiretti / Program.cs
Last active June 10, 2023 22:29
C# 12 example of primary constructor on classes
var product1 = new Product();
var product2 = new Product("Basic");
var product3 = new Product("My product", 1);
public class Product(string name, int categoryId)
{
public Product() : this("Default", -1) { }
public Product(string name) : this(name, 0) {}
@AnthonyGiretti
AnthonyGiretti / Program.cs
Last active May 24, 2023 13:10
Patch HTTP request since .NET 7 without any manual serialization
using (HttpClient client = new HttpClient())
using (HttpResponseMessage response = await client.PatchAsJsonAsync("http://someuri.com", new { Name = "Anthony" }))
using (HttpContent content = response.Content)
{
string result = await content.ReadAsStringAsync();
}