Posts

Your HttpClient is Broken: The IHttpClientFactory Playbook

🌐 Making HTTP Requests in .NET: Best Practices When developing API-driven services in .NET, the strategy used for managing HTTP communication is critical to application performance, reliability, and scalability. This document details the modern, recommended approach using IHttpClientFactory . HttpClient vs. IHttpClientFactory in Modern Architectures Historically, developers often instantiated HttpClient directly: C# using var client = new HttpClient(); // Use client to make a request While simple, this pattern is highly problematic in production environments, especially in microservice and high-load architectures. ⚠️ Common Pitfalls of Direct HttpClient Instantiation Pattern Issue Description new HttpClient() in a using block Socket Exhaustion Each new instance allocates a new HttpMessageHandler and potentially a new socket connection. While the HttpClient instance is disposed, the underlying socket may take time to ...

Data Protection API (DPAPI) system in ASP.NET Core

Image
Over the year’s developers are struggling to secure web applications and their data. In the windows desktop application system, we have a Data Protection API (DPAPI) but not in the web applications but ASP.NET Core 2.0 release brought more goodies to developers in the realm of cryptography as Microsoft has added DPAPI to make it easier for developers to use strong cryptography to safeguard their data.  In this article, we will discuss What is the data protection system. How does the data protection system work. Why do we need the data protection system. I mplementation of the data protection system in ASP.NET Core project. Custom configuration of the data protection system.     What is data-protection system?      The data-protection system is a set of cryptography APIs used by the ASP.NET Core to encrypt/ decrypt the sensitive data. So, it is all about how to protect sensitive information that will be exposed to the attackers, ideally without exposing an...

MERGE Statement - UPSERT into SQL Server

Image
Normally, when you want to write data to a table from the application you must first do a SELECT to check if the row exists, if it does exist you execute an UPDATE and if it does not exist you execute an INSERT, which is the standard SELECT-UPDATE-INSERT pattern. Check the below query: This looks like a pretty logical flow that reflects how we think about this in real life: Does a row already exist for this key? YES : OK, update that row. NO : OK, then add it. But this is a wrong practice. Locating the row to confirm it exists, only to have to locate it again to update it, is doing  twice the work  for nothing. Even if the key is indexed (which we hope is always the case). If we put this logic into a flow chart and associate, at each step, the type of operation that would have to happen within the database, we would have this: From a developer point of view, we really enjoy the freedom, if we have, of making a single call to the database and be able to Insert/Update the data w...

Using Dependency Injection in Asp.Net Core

Image
What is Dependency Injection? The dependency injection (DI) is a software design pattern, which is a technique for achieving Inversion of control (IOC) between classes and their dependencies. ASP.NET Core is designed from scratch to support Dependency Injection. ASP.NET Core injects objects of dependency classes through constructor or method by using built-in IoC container. Why use Dependency Injection? Using Dependency Injection, we make a class independent of its dependencies. It achieves that by decoupling the usage of an object from its creation. In other terms, the intent of Dependency Injection is to make code maintainable by allowing us to develop loosely-coupled code. Technically saying, DI reduces the hard-coded dependencies among the classes by injecting those dependencies at run time instead of design time. Overview of Dependency Injection A dependency is any object, that another object requires. Dependency Injection allows the creation of dependent objects...