Canceling background tasks when a user navigates away from a Blazor component

  • Gérald Barré
In an application, you don't want to waste resources. When an ongoing operation is not needed anymore, you should cancel it. For instance, if your application downloads data to show them in a view, and the user navigates away from this view, you should cancel the download. In a .NET application, the way to cancel operation is to use a CancellationToken. You can create a CancellationToken using the… [read more]

Running GitHub Super-Linter in Azure Pipelines

  • Gérald Barré
GitHub Super-Linter is a simple combination of various linters to help validate your source code. The end goal of this tool: Prevent broken code from being uploaded to your repository Help establish coding best practices across multiple languages Build guidelines for code layout and format Automate the process to help streamline code reviews Instead of using many linters individually in your CI, you can… [read more]

Never forget a #if when multi-targeting .NET libraries or applications

  • Gérald Barré
It's very easy to multitarget a library in the csproj. All you need to do is listing all target frameworks in the TargetFrameworks element: <Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFrameworks>net5.0;netstandard2.1;netstandard2.0;net461</TargetFrameworks> </PropertyGroup> </Project> Then, you may have different implementations in the code depending on the target framework. When MSBuild… [read more]

C# 9 - Improving performance using the SkipLocalsInit attribute

  • Gérald Barré
C# 9 brings lots of new language features. One of them is the ability to suppress emitting .locals init flag. This feature allows to improve the performance of a method by not zeroing the local variables before executing the method. Even if zeroing local has been improved in .NET 5, not doing it will still be faster. What is locals init? By default, the C# compiler emits the .locals init directive. This… [read more]

Process.WaitForExitAsync is not the async equivalent of Process.WaitForExit in .NET 5

  • Gérald Barré
.NET 5 introduces a new very handy method: Process.WaitForExitAsync(CancellationToken) (documentation). This method waits for the process to exit, or for the cancellationToken to be canceled. It is very similar to the synchronous version Process.WaitForExit and I'm pretty sure some analyzers will suggest replacing the method with the new asynchronous method. Both methods behave differently. WaitForExit… [read more]