Latest Posts On Code Maze

Built-In Rate Limiting Middleware in ASP.NET Core

Built-In Rate Limiting Middleware in ASP.NET Core

The rate limiting middleware has shipped since ASP.NET Core 7.0, and the code here runs on .NET 10. We configure it with builder.Services.AddRateLimiter(), put it in the pipeline with app.UseRateLimiter(), and attach a policy to an endpoint with the...

Baseline Styling in BenchmarkDotNet

Baseline Styling in BenchmarkDotNet

In this article, we will learn how to set a baseline for our benchmark in BenchmarkDotNet and discuss styling the baseline results. As software developers, benchmarking the performance of our methods is a crucial part of our day-to-day activities. With the...

Introduction to Strawberry Shake and GraphQL in .NET

Introduction to Strawberry Shake and GraphQL in .NET

In this article, we'll get familiar with GraphQL and Strawberry Shake, a GraphQL tool that facilitates interaction with local and remote data. Subsequently, we will build an application using the .NET API, focusing on customers searching for available space in...

Fastest Way to Find and Extract a Number From a String in C#

Fastest Way to Find and Extract a Number From a String in C#

This article will explore the quickest method to find and extract a number from a string in C#. We will discuss and implement different techniques for accomplishing this task. Subsequently, we will evaluate the performance of these techniques using the BenchmarkDotNet...

How to Detect if a Key is Pressed in a C# Console Application

How to Detect if a Key is Pressed in a C# Console Application

We frequently read data from user input in our C# application. However, we sometimes need to detect a key press and continue the operation. In this article, we discuss how to detect if a key is pressed in a C# console application. By understanding the usage of these...

How to Test gRPC Services in ASP.NET Core

How to Test gRPC Services in ASP.NET Core

gRPC is a popular way to create fast and scalable API's in the .NET world. In this article, we explore how to test gRPC Services in .NET applications, and set up both unit and integration tests to ensure the reliability and performance of gRPC services. [sc...

How to Set a Default User-Agent on an HttpClient in C#

How to Set a Default User-Agent on an HttpClient in C#

HttpClient sends no User-Agent header unless we add one. We can set it on a single HttpRequestMessage, or set it once on the client's DefaultRequestHeaders so that every request it sends carries the same value. We'll look at what the header holds first, then set it...

Restricting a Value Lifetime With the “scoped” Keyword in C#

Restricting a Value Lifetime With the “scoped” Keyword in C#

In this article, we will discuss the scoped keyword in C#, explore its use case, and examine how it can be advantageous. The contextual keyword scoped in C# serves as a powerful tool for restricting the lifetime of a value. Employing the scoped modifier allows us to...

How to Validate File Upload Extensions in ASP.NET Core

How to Validate File Upload Extensions in ASP.NET Core

In this article, we will see how we can validate file upload extensions and be certain that our applications are safe from malicious uploads. When receiving files uploaded by users, relying solely on file names to determine file types is risky. This is because we can...

How to Get the Number of CPU Cores in C#

How to Get the Number of CPU Cores in C#

Environment.ProcessorCount gives us the number of logical processors our process can use, and it works on every platform .NET runs on. For physical cores and processor sockets, we query WMI on Windows through the System.Management package. Knowing the difference...

GeneratedRegex in C#: Improve RegEx Performance in .NET

GeneratedRegex in C#: Improve RegEx Performance in .NET

Source-generated RegEx moves the work of building a regular expression from run time to build time. We mark a partial method with the [GeneratedRegex] attribute, and the .NET SDK writes the matching code for us as plain C#. In our benchmark on .NET 10, it validated a...