Skip to content

Instantly share code, notes, and snippets.

View marcominerva's full-sized avatar
🏠
Working from home

Marco Minerva marcominerva

🏠
Working from home
View GitHub Profile
@marcominerva
marcominerva / TimeOnlyConverter.cs
Last active October 20, 2022 20:07
TimeOnlyConverter for System.Text.Json in .NET 6.0
public class TimeOnlyConverter : JsonConverter<TimeOnly>
{
private readonly string serializationFormat;
public TimeOnlyConverter() : this(null)
{
}
public TimeOnlyConverter(string? serializationFormat)
{
@marcominerva
marcominerva / DateOnlyConverter.cs
Last active October 20, 2022 20:07
DateOnlyConverter for System.Text.Json in .NET 6.0
public class DateOnlyConverter : JsonConverter<DateOnly>
{
private readonly string serializationFormat;
public DateOnlyConverter() : this(null)
{
}
public DateOnlyConverter(string? serializationFormat)
{
@marcominerva
marcominerva / Frullino.cs
Last active March 25, 2021 10:47
Il codice perfetto
for (var frullino = 0; frullino < 42; frullino++)
{
System.Console.WriteLine("Taggia");
}
@marcominerva
marcominerva / AsyncLock.cs
Last active April 17, 2020 13:49
How to implement a simple lock in an asynchronous workflow
using System.Threading.Tasks;
namespace System.Threading
{
public class AsyncLock : IDisposable
{
private readonly SemaphoreSlim semaphoreSlim = new SemaphoreSlim(1, 1);
public async Task<AsyncLock> LockAsync()
{
@marcominerva
marcominerva / Position.cs
Created March 27, 2020 09:41
Equality operator for C#
public class Position : IEquatable<Position>
{
public double Latitude { get; set; }
public double Longitude { get; set; }
public bool Equals([AllowNull] Position other)
=> (Latitude, Longitude) == (other?.Latitude, other?.Longitude);
public override bool Equals(object obj) => Equals(obj as Position);
@marcominerva
marcominerva / Dockerfile
Created October 16, 2018 13:54
Modified Dockerfile and app/requirements.txt files to make Custom Vision Linux Container work on Raspberry Pi 2/3
FROM python:3.5
ADD app /app
RUN pip install --upgrade pip
RUN pip install https://github.com/lhelontra/tensorflow-on-arm/releases/download/v1.5.0/tensorflow-1.5.0-cp35-none-linux_armv7l.whl
RUN pip install -r /app/requirements.txt
# Expose the port
EXPOSE 80
@marcominerva
marcominerva / ForbiddenAuthorizeAttribute.cs
Last active March 22, 2018 13:25
Handle forbidden authorization with ASP.NET MVC
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
public class ForbiddenAuthorizeAttribute : AuthorizeAttribute
{   
public string ForbiddenViewName { get; set; } = "Forbidden";
   
protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
{
if (filterContext.HttpContext.Request.IsAuthenticated)
// Access forbidden, display 403 page (instead of the default login page, because the user is actually already