Csharp

How to use task-coalescing in C# to stop doing wasteful work

Lester Sanchez
The post discusses a common inefficiency in multithreaded applications where multiple tasks redundantly compute the same result, particularly in scenarios involving high-concurrency requests to remote servers. It introduces the task-coalescing technique, which ensures that only one task fetches the data while others reuse the result, reducing wasteful work. Using .NET’s Lazy<T> and ConcurrentDictionary<TKey, TValue>, the example implementation demonstrates how to achieve this efficiently, mitigating issues like cache stampedes and improving performance with minimal code changes.

How to do logging in .NET AWS Lambda functions

Lester Sanchez
The article outlines four ways to send logs to AWS Cloudwatch from a .NET Lambda function. It covers ILambdaContext.Logger, a basic option for simple logging; LambdaILogger, which integrates with Microsoft’s ILogger for richer features but has limitations with parameter serialization; Serilog, a highly customizable library for structured logging and control over log formats; and AWS Lambda Powertools, a straightforward utility tailored for AWS Lambda with built-in X-Ray trace correlation. The choice between these methods depends on the need for simplicity or advanced customization.

Testing: Simulate database failures using EF Core Interceptors

Lester Sanchez
The post explains how to use EF Core Interceptors to simulate database failures, enabling tests for application resiliency. It introduces a custom MockFailCommandInterceptor that selectively throws exceptions during specific database operations, such as INSERT, to test failure scenarios. This approach ensures that failure handling is thoroughly tested.

How to route requests based on HTTP headers in ASP.NET Core

Lester Sanchez
The blog post addresses how to route API requests that use the same HTTP verb and path but differ in behavior based on HTTP header values, such as the Accept header for versioning. It introduces a custom Accepts attribute in ASP.NET Core, which implements the IActionConstraint interface to direct requests to specific actions based on header values. This approach can be extended to handle any scenario where routing depends on header values, beyond versioning.