Skip to content

Instantly share code, notes, and snippets.

Avatar

Rich Lander richlander

View GitHub Profile
@richlander
richlander / dotnet-crypto-46.cs
Last active Sep 11, 2021
.NET Cryptography Updates for the .NET Framework 4.6
View dotnet-crypto-46.cs
// 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 / instructions.md
Last active Sep 9, 2021
Installing .NET Core 3.0 on Linux ARM64
View instructions.md

Installing .NET Core on Linux ARM64

The following intructions can be used to install .NET Core on Linux ARM64.

Pro tip: Check out .NET Core Docker files to determine the exact instructions for installing .NET Core builds, for example .NET Core 3.1 ARM32 SDK Dockerfile.

Installing .NET Core Globally

The following instructions install the latest .NET Core globally. It isn't required to do that, but it provides the best experience.

@richlander
richlander / modernizing-csharp9.md
Last active Aug 24, 2021
Modernizing a codebase for C# 9
View modernizing-csharp9.md

Modernizing a codebase for C# 9

There are lots of cases that you can improve. The examples use nullable reference types, but only the WhenNotNull example requires it.

Use the property pattern to replace IsNullorEmpty

Consider adopting the new property pattern, wherever you use IsNullOrEmpty.

string? hello = "hello world";
@richlander
richlander / json.cs
Created Jan 29, 2019
Sample usage of Utf8JsonWriter
View json.cs
static int WriteJson(IBufferWriter<byte> output, long[] extraData)
{
var json = new Utf8JsonWriter(output, state: default);
json.WriteStartObject();
json.WriteNumber("age", 15, escape: false);
json.WriteString("date", DateTime.Now);
json.WriteString("first", "John");
json.WriteString("last", "Smith");
@richlander
richlander / Dockerfile
Created Jun 14, 2018
ASP.NET Web Forms Multi-stage build Dockerfile
View Dockerfile
FROM microsoft/dotnet-framework:4.7.2-sdk AS build
WORKDIR /app
# copy csproj and restore as distinct layers
COPY *.sln .
COPY aspnetapp/*.csproj ./aspnetapp/
COPY aspnetapp/*.config ./aspnetapp/
RUN nuget restore
# copy everything else and build app
@richlander
richlander / share-the-love.md
Last active Jul 20, 2021
.NET Core 3.0 -- Post Launch Blog Posts
View share-the-love.md

.NET Core 3.0 -- Post Launch Blog Posts

We launched .NET Core 3.0! Go team! The super official blog post covered a lot of features, but none of them at depth. Many people will want to learn more about specific scenarios at features at much greater depth, with more guidance and better code samples.

The following is a list of proposed posts (with descriptive but prelimary titles) for us to write and publish during the rest of 2019.

Note: This tweet also has feedback.

Getting Started

@richlander
richlander / dotnetcore-contributor-model.md
Last active Jul 20, 2021
.NET Core Contributor Model
View dotnetcore-contributor-model.md

.NET Core Contributor Model

The .NET Core project has three levels of contributors.

  • All GitHub users: Read access to code and issues. Can participate in pull request code reviews, comment on issues, create new issues, and submit pull requests for review.
  • "Project contributor" - Can be assigned issues and @referenced. Part of the dotnet-community team. Provided based on frequent contributions or upon request1.
  • "Project committer" - Has repo committer access. Can merge PRs, assign people on issues, create branches, .... Currently only MSFT people, but will likely include other folks in the future.

1Requires that a .NET Foundation CLA has been signed.

@richlander
richlander / json-document.cs
Created Jan 29, 2019
Sample Usage of JsonDocument and JsonElement
View json-document.cs
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 / metdata-reading-example.cs
Created Dec 4, 2018
Assembly Metadata Reading with MetadataLoadContext Example
View metdata-reading-example.cs
var paths = new string[] {@"C:\myapp\mscorlib.dll", @"C:\myapp\myapp.dll"};
var resolver = new PathAssemblyResolver(paths);
using (var lc = new MetadataLoadContext(resolver))
{
Assembly a = lc.LoadFromAssemblyName("myapp");
Type myInterface = a.GetType("MyApp.IPluginInterface");
foreach (Type t in a.GetTypes())
{
if (t.IsClass && myInterface.IsAssignableFrom(t))
Console.WriteLine($"Class {t.FullName} implements IPluginInterface");