Skip to main content

· 26 min read
Adnan Rafiq
Understand Middleware Intuitively

What will you learn?

It is a long blog post, but it builds the foundation and deep understanding of the middleware in the ASP.NET 8 or previous versions starting with ASP.NET 6.

It explores middleware, introduces you to HTTP middleware and ASP.NET middleware. It deep dives how the ASP.NET middleware works & how it is actually built. Then you will learn different ways to create middleware in .NET and their pros and cons. Finally, you will build your own middleware pipeline using the ASP.NET approach. Do not miss the copy & paste console sample to debug the .NET like middleware pipeline.

The Middleware

What comes to your mind when you hear the word middleware?

Middle of nowhere. 🃏😆 But that's not the case here.

In the middle of two soft(wares), right? But in the context of the ASP.NET 8 why do you need it? What does it do? How does it work?

· 9 min read
Adnan Rafiq
ASP.NET 8 Configurations With no magic strings

ASP.NET 8 Configurations

Do you love magic strings to get the configuration values? No. Me neither. It was a serious question except you are in JS/TS land.

The amazing .NET supports strongly typed configurations. After all, it is a typed language.

Let me show you the complete usage of strongly typed configurations in ASP.NET 8.

· 16 min read
Adnan Rafiq
ASP.NET 8 Configurations Simple but YET Complex Story Image

ASP.NET 8 Configurations Think about a piece of software; it could be a web or mobile application or an API. What's its purpose? Well, it's here to solve a single or multiple problems for you by offering a suite of unique features.

Now, let's say some of you are fans of a dark theme, while others love the bright hues of a light theme. How can one software cater to both tastes?

Enter Configurations!

· 7 min read
Adnan Rafiq
Title Image of the blog

Records - A Reference Type were introduced in C#9 and extended in C#10 to allow record struct - A Value Type. What is so special about records that the .NET team shipped them in two consecutive releases. In nutshell C# lacked a immutable type with true value equality semantics with a short syntax. Records solves this problem. In this post you will learn:

  • Concise Syntax - Positional Properties
  • Value Equality
  • Immutability
  • Non-Destructive Mutation
  • Deconstruction
  • DDD Value Object using records only

Does this make you curious? I am excited to share this with you.

Youtube Video

I have recorded a detailed Youtube video, if you prefer the video content.

Unlock the Powers of C# Record

· 5 min read
Adnan Rafiq
Start and Finish Image

Image by JetBrains Linkedin

The refactoring of legacy applications is the most valuable skill, you must continuously learn. The ASP.NET Framework 4.x applications are considered legacy, and the dependency injection was not part of the framework. In recent survey done by JetBrains on Linkedin, 46% developers voted the legacy applications as their biggest challenge. In this blog post, you will learn how to incrementally enable DI in ASP.NET 4.8 Web Api application which does not use any sort of DI mechanism, not even poor man DI.

· 4 min read
Adnan Rafiq
Start and Finish Image

Image by @claybanks

Overview

I am working on migrating the .NET Framework application to the .NET6. Since the application was initially written in the .NET Framework 2.0 thus it contains the legacy approaches to get the data from the database. We were using the old version of Microsoft Enterprise Library Data Access package to get the data from the database which is not compatible with the .NET Standard 2.0. So I decided to generate the code for stored procedures using the Dapper and Handlebars templates.

I faced two problems:

  • SQL Server system tables does not know about stored procedure parameter nullability
  • When you are using conditional logic inside the stored procedure, sql server does not give you correct count of the result sets.

· 2 min read
Adnan Rafiq
Start and Finish Image

Image by @claybanks

Authorization Policy

Authorizing the resource access is essential part of any API. The .NET provides you a perfect mental model which is easier to reason about. It has this flow:

  1. What is the name of your policy as string.
  2. What requirement the user must satisfy to qualify which implements the IAuthorizationRequirement interface.
  3. What is your handler responsible to evaluate, which inherits the AuthorizationHandler<UniqueIdHeaderRequirement> and register it.

Then Authorize attribute allows you to set a policy name when used on controller or action method. But if you are fan of Minimal API then fluent style is the way to go using RequireAuthorization.

· 3 min read
Adnan Rafiq
Start and Finish Image

Image by awcreativeut

CORS are best described on MDN

Cross-Origin Resource Sharing (CORS) is an HTTP-header based mechanism that allows a server to indicate any origins (domain, scheme, or port) other than its own from which a browser should permit loading resources. CORS also relies on a mechanism by which browsers make a "preflight" request to the server hosting the cross-origin resource, in order to check that the server will permit the actual request. In that preflight, the browser sends headers that indicate the HTTP method and headers that will be used in the actual request.

How to configure CORS in .NET6 API?

CORS in .NET6 API can be configured using CORS policies.