Skip to content

Instantly share code, notes, and snippets.

@wgross
wgross / rust-like-return-poc-test.cs
Created February 13, 2023 14:38
POC for a rust like return
[Fact]
void Return_result()
{
Result Function() => Result.Ok();
Assert.True(Function() is Ok);
}
[Fact]
void Return_result_value()
@wgross
wgross / cli_return_code.ps1
Created January 15, 2023 14:40
Tiny PowerShell helpers for CLIs
filter zero_is_true {
# incoming value of 0 is mapped to $true, mapped to $false ortherwise
if(0 -eq $_) {
$true
}
else {
$false
}
}
@wgross
wgross / Program.cs
Created December 11, 2021 21:29
Serilog Compact Strcuture Json Logging
public static IHostBuilder CreateHostBuilder(string[] args) =>
Microsoft.Extensions.Hosting.Host.CreateDefaultBuilder(args)
.UseSerilog(ConfigureSerilog)
.ConfigureWebHostDefaults(webBuilder => webBuilder.UseStartup<Startup>());
private static void ConfigureSerilog(HostBuilderContext hostBuilderContext, LoggerConfiguration loggerConfiguration)
{
loggerConfiguration.ReadFrom.Configuration(hostBuilderContext.Configuration);
}
@wgross
wgross / get-registry-property-value.ps1
Created September 22, 2021 08:46
Fetches Key name, property name and property value from registry keys
Get-ChildItem . | Foreach-Object {
# this is done foreach reg key
$regKey = $_;
# property is a list of properties of the registry key
$_.Property | ForEach-Object -Process {
[PScustomobject]@{
Key =$regKey.Name
Property=$_
# Value is returned as a complex object extsnded with sevaral PS properties.
@wgross
wgross / Debounce_Throttle.cs
Last active June 2, 2021 21:06
Debounce and Throttle Event Handlers
// https://www.meziantou.net/debouncing-throttling-javascript-events-in-a-blazor-application.htm
Action<T> Debounce<T>(Action<T> action, TimeSpan interval)
{
if (action == null) throw new ArgumentNullException(nameof(action));
var last = 0;
return arg =>
{
@wgross
wgross / jwt-token-sign-validate-symmetric.linq
Last active March 14, 2021 12:04
Creates a symmetric key, signs a JWT and validates the signature
// https://weblog.west-wind.com/posts/2021/Mar/09/Role-based-JWT-Tokens-in-ASPNET-Core
// create token and sign it with key
var signingCredentials = new SigningCredentials(
key: new SymmetricSecurityKey(Encoding.UTF8.GetBytes("Thats a sufficiently long key")),
algorithm: SecurityAlgorithms.HmacSha256);
var token = new JwtSecurityToken(
issuer:"issuer",
audience: "audience",
@wgross
wgross / jwt-token-sign-validate-asymmetric.linq
Last active March 23, 2021 18:58
Creates an asymmetric key, signs a JWT and validates the signature
// https://medium.com/dev-genius/jwt-authentication-in-asp-net-core-e67dca9ae3e8
var rsa = RSA.Create();
var keyPair = (
prv: rsa.ExportRSAPrivateKey(),
pub: rsa.ExportRSAPublicKey()
);
// create token and sign it with private key
var rsa_signer = RSA.Create();
@wgross
wgross / dotnet-test-finecodecoverage.csproj
Last active March 23, 2021 19:02
default configuration for test csprojs using FineCoverage
<PropertyGroup Label="FineCodeCoverage">
<Enabled>True</Enabled>
<Exclude>
[Microsoft.*]*
[System.*]*
</Exclude>
<Include>[*]*</Include>
<ExcludeByFile></ExcludeByFile>
<ExcludeByAttribute></ExcludeByAttribute>
<IncludeTestAssembly>True</IncludeTestAssembly>
@wgross
wgross / ClassLogger.cs
Last active February 28, 2020 07:34
Logger facade with using and Caller-attributes
public class ClassLogger
{
private readonly string typeName;
public static ClassLogger Make<T>(T instance)
{
return new ClassLogger(typeof(T).Name);
}
public ClassLogger(string typeName)
@wgross
wgross / dyn_param.ps1
Created July 24, 2019 17:45
POC: replicate parameters of a called script as parameters of a calling script
using namespace System.Management.Automation
using namespace System.Collections.ObjectModel
function inner {
param(
[int]$Value
)
$Value
}