Using type aliases to make code clearer with generics
Meziantou's blog
by Gérald Barré
3d ago
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
Visit website
Computing code coverage for a .NET project
Meziantou's blog
by Gérald Barré
1w ago
In an older post, I wrote about how to compute code coverage for a .NET Core project with Coverlet. In this post, I'll show you how to get code coverage without any additional dependency. Indeed, the .NET SDK 8 now includes all you need to compute code coverage. Microsoft also provides a .NET tool to process the coverage files. Let's start by creating a new xUnit project and removing the dependency on ..read more
Visit website
Creating ico files from multiple images in .NET
Meziantou's blog
by Gérald Barré
2w ago
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
Visit website
Json schema validation in .NET
Meziantou's blog
by Gérald Barré
3w ago
It is often useful to validate JSON documents against a JSON schema. For example, you may want to validate the JSON document that you receive from a REST API or a configuration file written by a user. In this post, I describe how to validate JSON data against a JSON schema in .NET. Using PowerShell, you can use the Test-Json cmdlet to validate JSON data against a JSON schema. .NET doesn't support JSON ..read more
Visit website
Convert DateTime to user's time zone with Blazor in .NET 8
Meziantou's blog
by Gérald Barré
1M ago
This post is an update of the original post Convert DateTime to user's time zone with server-side Blazor 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 ..read more
Visit website
Generate OpenAPI specification at build time from the code in ASP.NET Core
Meziantou's blog
by Gérald Barré
1M ago
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
Visit website
Set a blank page for new tabs in Microsoft Edge
Meziantou's blog
by Gérald Barré
1M ago
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
Visit website
Optional parameters may appear in the middle of the parameter list
Meziantou's blog
by Gérald Barré
1M ago
In .NET, optional parameters are not always the last parameters. While C# does not allow the declaration of optional parameters in the middle of the parameter list, it is possible to do so in IL or from other languages like VB.NET or F#. Also, the compiler may lower some features and create methods with optional parameters in the middle of the parameter list. Why does it matter? If you write a source ..read more
Visit website
Creating an HttpClient that uses DNS over Https
Meziantou's blog
by Gérald Barré
2M ago
DNS is a key component of the Internet. It's used to translate a domain name to an IP address. For instance, when you type https://www.meziantou.net in your browser, the browser will query the DNS server to get the IP address of the server hosting the website. Then, the browser will connect to the server using the IP address. A good practice is to rely on the OS configuration to query the DNS server ..read more
Visit website
Enable the new TerminalLogger in .NET 8 SDK automatically
Meziantou's blog
by Gérald Barré
2M ago
In .NET 8, you can use the new TerminalLogger. This terminal logger provides better output than the default console logger. It provides live progression and improves error reporting. However, it's not enabled by default. You need to use the --tl option to enable it (e.g. dotnet build --tl). Instead of adding the flag every time you run a dotnet command, you can enable the new terminal logger by adding the ..read more
Visit website

Follow Meziantou's blog on FeedSpot

Continue with Google
Continue with Apple
OR