CsWin32 – Automatic PInvoke generation

CsWin32 is a compile time package generating Win32 PInvoke from a list of name including wildcard.

You add a reference to Microsoft.Windows.CsWin32 (pre release as of time of writing) and then you just have to create a text file named NativeMethods.txt alongside your project file.

During next build, a source generator will generate all requested PInvoke inside a Windows.Win32.PInvoke class. Each recursively needed type is also included.

A NativeMethods.json file can be used to customize generation

microsoft/CsWin32: A source generator to add a user-defined set of Win32 P/Invoke methods and supporting types to a C# project.

Beware: An issue with source generator in WPF projects requires you some hacking.

dotnet/datalab – a GitHub repository full of interesting EF Core extensions

A repository listing many projects related to extending EF Core.

I haven’t tested if these projects are robust implementations or even practical to use but it’s worth testing them if necessary and, why not, taking inspiration from them.

Some of them could be interesting:

Some extensions are already implemented in latest EF Core version but it can be useful for those stuck with old versions, I know something about it.

In a close domain, DapperLib/DapperAOT can also be very usefull. It’s a source generator that handle the heavy task of checking the SQL syntax before generating the Dapper code from attributes, all in AOT mode.

Entity Framework Core Remove method sometimes leads to an antipattern

A very simple way to delete entities in EF Core without preliminarily getting that entity. If we know its primary key, there is no need to do a get request, we just have to inform EF that the object is supposed to exist and deal with the case where it doesn’t exist. It’s obvious but very little used

More Efficient Deletes With Entity Framework Core | Khalid Abuhakmeh

Improving Azure Key Vault use as configuration source in ASP.NET Core

Something I’ve missed: loading Key Vault secrets in .Net Core as configuration source can be a lengthy process if you don’t opt-out from unused authentication modes.

Hopefully, authentication chain construction can be customized by setting numerous ExcludeXxxx properties of DefaultAzureCredentialOptions. Some of these property are true by default but only a few.

More details here: Improving Azure Key Vault Performance in ASP.NET Core By Up To 10x (the assertion “Up to 10x” is a bit marketing, actual gain should be tested as it depends on many factors 🙂 ).

RecyclableMemoryStream, a better MemoryStream

More than 5 years after my first discovery, RecyclableMemoryStream is still relevant. With a clever use of buffer chains, this class should be used in (any) situation where a MemoryStream could be used.

This project has evolved to take into account the new memory efficiency paradigms integrated into the latest versions of .Net Core (Span, ReadOnlySpan, ReadOnlySequence, and Memory). It is more useful than ever to reconcile throughput and CPU load.

From Github project’s Readme.md:

 “In particular it is optimized to do the following:

  • Eliminate Large Object Heap allocations by using pooled buffers
  • Incur far fewer gen 2 GCs, and spend far less time paused due to GC
  • Avoid memory leaks by having a bounded pool size
  • Avoid memory fragmentation
  • Allow for multiple ways to read and write data that will avoid extraneous allocations
  • Provide excellent debuggability and logging
  • Provide metrics for performance tracking

DamianEdwards/MinimalApiPlayground

Some sample API implemented in .Net 6.0 using minimal API with customized OpenApi support:

  • a playground with different test cases of miminal API possibilities (Results, custom IResults, advanced binding, etc…),
  • a classical TODO Api in EF Core,
  • a classical TODO Api in Dapper,

with validation support using System.ComponentModel.Validation extended by MinimalApis.Extensions (for now in preview).

Tomasz Pęczek repositories

Tomasz GitHub is a gem of asp.Net core related stuff:

Each subject is largely explain in understandable but not simplistic blog posts (and often very advanced technically) . Multiples sample projects are present. A very educational approach that makes you want to dig deeper.