Measuring performance of Roslyn Source Generators

 
 
  • Gérald Barré
A Roslyn source generator is a tool that allows you to programmatically generate C# or VB.NET source code at compile time. Source generators are implemented as Roslyn analyzers, which are NuGet packages that contain one or more source generators. When you include a source generator NuGet package in your project, the source generator will run during the build process and generate additional source code… [read more]

Testing Roslyn Incremental Source Generators

 
 
  • Gérald Barré
Roslyn Source Generators allow to generate code based on the current project code and additional files. Each keystroke in the editor may trigger source generators. So, it is important to ensure that the source generators are fast enough to not impact the user experience. One important feature is the incremental generation. This means that the source generator only call the source generator when some… [read more]

Extending the System Menu to add advanced commands in .NET

 
 
  • Gérald Barré
What's the system menu? The system menu is the menu that appears when you right-click on the title bar of a window or when you press Alt+Space. It contains commands like 'Close', 'Move', 'Size', 'Minimize', 'Maximize', 'Restore', 'Help', etc. This menu is always accessible whatever the window state. Even for custom windows that doesn't have a title bar, you can still access the system menu by pressing… [read more]

Creating a custom Main method in a WPF application

 
 
  • Gérald Barré
WPF generates a default Main method for you. This method starts the WPF application. In my case, I wanted to ensure only one instance of the application is running. You can make the check in the Startup event, but this means that your code will be executed once all WPF dll are loaded and some of the WPF startup code is executed. This can takes a few hundred milliseconds, not a lot, but for a good UX this… [read more]

Prevent accidental disclosure of configuration secrets

 
 
  • Gérald Barré
An application often use secrets to access databases or external services. The secrets are usually provided using environment variables, configuration file, or Vault (Azure Vault, Google Secret Manager, etc.). This secrets are often bound as string making it easy to accidentally disclose. For example, a secret can be accidentially logged or part of a json serialization. Also, it's harder to know where are… [read more]

Using source-generated regex in ASP.NET Core route constraints

 
 
  • Gérald Barré
To use the Regex source generator, you need to use .NET 7 and C# 11. Source Generated Regex provides multiple advantags over the traditional regex: Faster startup time as the all the code is generated at compile time. You don't need to parse the regex pattern and generate an optimized code to execute the regex at runtime. Better trimming support as the code is generated at compile time. The code that is… [read more]

Using .NET code from JavaScript using WebAssembly

 
 
  • Gérald Barré
Blazor WebAssembly allows to run a .NET web application in a browser. Starting with .NET 7, you can easily run any .NET method from JavaScript without needing the whole Blazor framework. Let's see how to run a .NET method from JavaScript! First, you need to install the WASM workload, so you can publish the app later: dotnet workload install wasm-tools Then, you can create a new console app: dotnet new… [read more]

Reducing Blazor WASM size by providing custom ICU data

 
 
  • Gérald Barré
The International Components for Unicode (ICU) is a set of libraries that provide Unicode and internationalization support for software applications. Unicode is a standardized encoding system that represents almost all of the written languages of the world. It is used to represent characters in computers, mobile devices, and other digital devices. ICU provides a wide range of functions for working with… [read more]