How to avoid conflicts when testing your dotnet templates locally

 
 
  • Gérald Barré
When you author a dotnet template, you can use the dotnet install and dotnet new commands to create a new project based on your local template. However, dotnet install may conflict with a previous installation of the template published as a NuGet package. In this post, I describe how to avoid this conflict and how to test your template locally. dotnet has a few hidden arguments that you can use to debug… [read more]

How to check if a DLL and an exe is a .NET assembly

 
 
  • Gérald Barré
Recently, I needed to check if a file was a .NET assembly without loading the assembly. .NET uses the Portable Executable (PE) format to store assemblies. The PE format is a file format for executables or DLLs on Windows, but .NET also uses it on Linux and MacOS. Hopefully, .NET provides the PEReader class to read the file header. This class can parse the content of a file to extract PE information. You… [read more]

How to use the latest target framework available for a .NET SDK

 
 
  • Gérald Barré
Sometimes, you don't want to bother with the target framework version of your project. You just want to use the latest version available from the .NET SDK. This can be useful if you want to always be on the latest version and don't want to spend time updating the .csproj files. You can fix the version of the SDK in a global.json file and use tools such as Renovate to update the version of the SDK for you.… [read more]

Using type aliases to make code clearer with generics

 
 
  • Gérald Barré
When you use generic types in C#, it is sometimes hard to understand what the generic argument can represent. For example, what do the strings represent in Dictionary<string, string>? To make the code clearer, you can use type aliases. // ❌ What does the string means? _ = new Dictionary<string, string>(); // Create aliases using UserId = string; using ProjectId = string; // Use the type aliases // ✅ It's… [read more]

Creating ico files from multiple images in .NET

 
 
  • Gérald Barré
Ico files are old but still widely used. One specificity of the ico format is that it can contain multiple images of different sizes. The format is not very complicated but there are not a lot of free tools to create an ico file from a list of images. Let's create one in .NET! The specification of the ico format is available on Wikipedia. The following code is a simple implementation of the specification.… [read more]

Convert DateTime to user's time zone with Blazor in .NET 8

 
 
  • Gérald Barré
This post is an update of the original post to take advantage of new .NET 8 features. It is inspired by the following pull request in dotnet/aspire When you display DateTime data to a user, you may want to convert the value to the user's time zone. With server-side Blazor, the code is executed on the server, so DateTime.Now corresponds to the time zone of the server instead of the user's time zone. At the… [read more]

Generate OpenAPI specification at build time from the code in ASP.NET Core

 
 
  • Gérald Barré
The OpenAPI specification is a powerful tool to describe and document APIs. It is a standard that allows you to define the structure of your API, including the endpoints, the request and response models, and the security requirements. The OpenAPI specification is a JSON or YAML file that can be used to generate documentation, client libraries, and server stubs. Most .NET developers generate the… [read more]

Set a blank page for new tabs in Microsoft Edge

 
 
  • Gérald Barré
Microsoft Edge can be configured using the setting pages edge://settings. However, some settings are not available in the UI. For instance, you cannot replace the new tab page. You can only configure the content of the page... So, you have to stick with the Edge-like page with news and other stuff you don't care about. Hopefully, you can configure Microsoft Edge using policies. There are multiple ways to… [read more]