How to generate a dump file of a .NET application

  • Gérald Barré
When an application is not running well, it could be useful to generate a dump file to debug it. There are many ways to generate a dump file on Windows, Linux, or Azure. Windows dotnet-dump (Windows) The dotnet-dump global tool is a way to collect and analyze dumps for .NET Core applications. Install dotnet-dump (require .NET SDK) or download it dotnet tool install --global dotnet-dump Find the process id… [read more]

Writing unsafe .NET code without the unsafe keyword

  • Gérald Barré
Someone asked me why I set AllowUnsafeBlocks to true in the Meziantou.DotNet.CodingStandard package (source). In this post, I'll explain what this property enables, and why it is not more unsafe to set it to true. The AllowUnsafeBlocks property The AllowUnsafeBlocks compiler option allows code that uses unsafe features to compile. The default value for this option is false, meaning unsafe code is not… [read more]

Detecting Double-Writes in MSBuild using the binlog

  • Gérald Barré
Double-writes are a common problem when building an application. This occurs when the same file is written at least twice during a build. This could be because 2 different projects are outputing the same files, so you are doing unneeded work. Thus, the build is slower than it needs to be. Also, it may break some incremental builds. MSBuild allows to detect files written multiple times during compilation,… [read more]

Getting user consent before executing sensitive code

  • Gérald Barré
Before executing sensitive actions, such as purchasing something or showing sensitive data, you need to get user consent. Also, you need to ensure the actual user is the one who is executing the action. Windows provides an API to get user consent before executing sensitive actions. First, you need indicate the project targets Windows by setting the TargetFramework property in the .csproj file: <Project… [read more]

Performance: string.Create vs FormattableString

  • Gérald Barré
Interpolated strings are very common in C#. For instance, you can write $"Hello {name}! You are {age} years old.". This expression is evaluated using the current culture. If you want to use an invariant culture, you can use FormattableString.Invariant($"..."). Starting with .NET 6 and C# 10, you can use string.Create(culture, $"...") to evaluate the interpolated string using a specific culture, including… [read more]

Regex with IgnoreCase option may match more characters than expected

  • Gérald Barré
In a previous post, I explained why \d is different from [0-9]. In this post, I'll explain why the regex [a-zA-Z] is different from the regex [a-z] with the IgnoreCase option. var regex1 = new Regex("^[a-zA-Z]+$"); var regex2 = new Regex("^[a-z]+$", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant); Console.WriteLine(regex1.IsMatch("Test")); // true Console.WriteLine(regex2.IsMatch("Test")); //… [read more]

Generating PInvoke code for Win32 apis using a Source Generator

  • Gérald Barré
Writing PInvoke code is not trivial. Most of the time you need to find the method signature from the documentation or the header files. This takes time and is error prone. Also, you can't find a NuGet package that wraps all Win32 methods because the number of methods, constants and structures is huge. Hopefully, Microsoft has released a Roslyn source generator to generate PInvoke for Win32 apis. In this… [read more]

Using a custom build pipeline with Azure Static Web Apps

  • Gérald Barré
Azure Static Web Apps allows to host static web sites and their API. They also provide a GitHub action to build and deploy your application. The GitHub action automatically detects the kind of project from the sources and builds it. It currently supports many JavaScript frameworks and static site generators like Hugo, Gastby, Angular, React, Svelte, Vue, or Blazor. While this magic works most of the time,… [read more]

Faster and Safer NuGet restore using Source Mapping and Lock files

  • Gérald Barré
Using NuGet packages is very common. Some options may help get better performance and security when restoring packages. Lock Files When restoring packages, NuGet creates a dependency graph. This graph includes all declared packages and the transitive packages. This dependency graph is then used to determine which packages should be downloaded and installed. A lock file allows to store the dependency graph… [read more]

Running Lighthouse CI when deploying Azure SWA

  • Gérald Barré
When you add new features to a web application, it's important to ensure there is no regression in term of performance. LightHouse CI is a tool that helps you to run a Lighthouse analysis on your web application. If you don't know lighthouse, it's a tool that analyzes web apps and web pages and generate a report about performance and best practices. Lighthouse integrated into the dev tools of Chromium… [read more]