Skip to content

Instantly share code, notes, and snippets.

View richlander's full-sized avatar

Rich Lander richlander

View GitHub Profile
@richlander
richlander / Program.cs
Created October 12, 2021 15:33
const interpolated string example
const string Bar = "Bar";
const string DoubleBar = $"{Bar}_{Bar}";
WriteLine(DoubleBar);
@richlander
richlander / using-registry-with-core.cs
Created February 27, 2018 07:55
Using Registry APIs with .NET Core
private static string GetLoggingPath()
{
// Verify the code is running on Windows.
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
using (var key = Registry.CurrentUser.OpenSubKey(@"Software\Fabrikam\AssetManagement"))
{
if (key?.GetValue("LoggingDirectoryPath") is string configuredPath)
return configuredPath;
}
@richlander
richlander / dotnet8-container-demo.md
Last active February 23, 2023 02:57
.NET Container Demo

Playing with .NET 8 Containers

We just shipped a cool new scenario for .NET, which is making it really easy to run .NET 8 container images as a non-root user.

Friends don't let friends run as root

We should all get T-Shirts that say that.

Let's take a quick look using our favourite aspnetapp sample. I'm going to use WSL. I'll show you everthing I do. I use the patterns that are easiest for me, which might not be the same for you.

@richlander
richlander / sdk-size-improvements.md
Created July 23, 2019 16:32
.NET Core 3.0 SDK Size Improvements

.NET Core 3.0 SDK Size Improvements

The .NET Core SDK is significantly smaller with .NET Core 3.0. The primary reason is that we changed the way we construct the SDK, by moving to purpose-built “packs” of various kinds (reference assemblies, frameworks, templates). In previous versions (including .NET Core 2.2), we constructed the SDK from NuGet packages, which included many artifacts that were not required and wasted a lot of space.

The following sections demonstrate the size improvements for Windows, Linux and macOS, including container delivery. They detail the process and commands that were used to determine the product sizes, enabling you to reproduce the same results in your own environment. To keep thing simple, zips and tar balls were downloaded from dotnet/core-sdk as opposed to the official installers.

Some readers will be shocked on how large the .NET Core 2.2 installer directory grows when the NuGetFallback archive is expanded to the NuGetFallBackFolder. W

@richlander
richlander / dotnet-crypto-46.cs
Last active June 4, 2022 18:45
.NET Cryptography Updates for the .NET Framework 4.6
// Example 1: Signing a byte[] using PKCS#1 v1.5 padding and a SHA-256 hash
// 4.5:
public static byte[] SignDataPkcs1Sha256(X509Certificate2 cert, byte[] data)
{
// X509Certificate2.PrivateKey returns the same object across multiple calls,
// so it shouldn't be Disposed independent of the X509Certificate2 object.
//
// The RSA base class doesn't expose any signature-based methods.
// The PrivateKey property returns AsymmetricAlgorithm, so really this call should be
@richlander
richlander / json-document.cs
Created January 29, 2019 17:21
Sample Usage of JsonDocument and JsonElement
static double ParseJson()
{
const string json = " [ { \"name\": \"John\" }, [ \"425-000-1212\", 15 ], { \"grades\": [ 90, 80, 100, 75 ] } ]";
double average = -1;
using (JsonDocument doc = JsonDocument.Parse(json))
{
JsonElement root = doc.RootElement;
JsonElement info = root[1];
@richlander
richlander / Program.cs
Last active October 21, 2021 15:52
Record struct example
Battery battery = new("CR2032", 0.235, 100);
WriteLine(battery);
while (battery.RemainingCapacityPercentage > 0)
{
battery.RemainingCapacityPercentage--;
}
WriteLine(battery);
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
@richlander
richlander / Program.cs
Created October 11, 2021 21:56
readonly record struct example
Battery battery = new("CR2032", 0.235, 100);
WriteLine(battery);
while (battery.RemainingCapacityPercentage > 0)
{
Battery updatedBattery = battery with {RemainingCapacityPercentage = battery.RemainingCapacityPercentage - 1};
battery = updatedBattery;
}