Performance: Lambda Expressions, Method Groups, and delegate caching

  • Gérald Barré
Delegates are used to pass methods as arguments to other methods. The most common delegates are Action, Func<T>, and EventHandler. You can use a lambda expression to provide a delegate or you can use a method group. You can also cache the delegate into a field and reuse the instance when needed. void Demo(Func<string> action) { } string GetString() => ""; // Method Group Demo(GetString); // Lambda… [read more]

Enabling IntelliSense for GitHub Actions workflows in VS Code

  • Gérald Barré
If you edit a GitHub Actions workflow in GitHub, you can use auto-completion to quickly know what is possible and detect errors. I don't often use the GitHub editor. Instead, I prefer to use VS Code to edit my files. By default, VS Code doesn't support IntelliSense for GitHub Actions workflows. But, you can get it using an extensions. First, install the YAML extension developed by Red Hat. Then, you can… [read more]

Getting telemetry data from inside or outside a .NET application

  • Gérald Barré
In a previous post, I explained how to monitor a .NET application using OpenTelemetry. The telemetry data includes traces, metrics, and logs. When using OpenTelemetry, the application publishes the data to the OpenTelemetry Collector or exposes endpoints to get the data. However, .NET provides a way to get the data from outside the application using ETW or the diagnostics event pipe. In this post, I… [read more]

Sharing coding style and Roslyn analyzers across projects

  • Gérald Barré
I've written many times about enforcing a coding style and enabling static analysis in a project: How to enforce a consistent coding style in your projects Enforce .NET code style in CI with dotnet format The Roslyn analyzers I use in my projects What if you now want to share a coding style and Roslyn analyzers across multiple projects in a company? Recent version of .NET and Roslyn have made it possible… [read more]

How to list all routes in an ASP.NET Core application

  • Gérald Barré
When your ASP.NET Core application is big enough, you may want to have a comprehensive view of all routes. There are multiple ways to declare routes. You can use Minimal API, Controllers, Razor Pages, gRPC, Health checks, etc. But all of them use the same routing system under the hood. The collection of routes can be listed by retrieving the collection of EndpointDataSource from DI. var builder =… [read more]

Uploading multiple files using InputFile in Blazor

  • Gérald Barré
Blazor provides a component to upload files. You can use the <InputFile> component and its OnChange event to get the selected files. It works well for single file uploads. But, if the user selects new files while the first files are still uploading, you may get an error. You can reproduce this error using the following Razor component: <InputFile OnChange="OnFileChanged" multiple></InputFile>… [read more]

Converting code to the new Regex Source Generator

  • Gérald Barré
.NET 7 brings a new feature for Regex. Indeed, it allows generating the source code of a regular expression at compile time using a Roslyn Source Generator. Generating the source code at compile-time instead of runtime has multiple advantages: The first regex execution is faster. Indeed, you don't need to parse the regex and generate the code to execute it at runtime. The regex takes advantage of all the… [read more]