30 April 2024

Kestrel Server in Dotnet

 Kestrel Server in Dotnet

Kestrel is a cross-platform web server for ASP.NET Core. Kestrel is the recommended server for ASP.NET Core, and it's configured by default in ASP.NET Core project templates.  

ASP.NET Core project templates use Kestrel by default when not hosted with IIS. In the following template-generated Program.cs, the WebApplication.CreateBuilder method calls UseKestrel internally:

var builder = WebApplication.CreateBuilder(args);

When to use which Service, Singleton, Scoped and Transient

 

When to use which Service, Singleton, Scoped and Transient

Singleton approach => We can use this for logging service, feature flag (to on and off module while deployment), and email service,

Scoped approach => This is a better option when you want to maintain a state within a request,

Transient approach =>  Use this approach for the lightweight service with little or no state.

Scoped Approach in detail
The same instance lives for the entire scope of that request, for example; let's suppose one controller has two parameters, both are the objects of the same "Sample" class, then both the objects will share the same instance across the request,

Partitioning in SQL Server(Performance Improvement)

 Partitioning in SQL Server(Performance Improvement)

Used to divide large tables or indexes into smaller , more manageable pieces, yet treat them as single entity.

Types
  1. Partition Function
  2. Partition Scheme
  3. Table and Index partitioning

Await example in C#

Await example in C# 

using System;
using System.Threading.Tasks;

class Program {
 private static string result="test123";
 
 static void Main() {
   SaySomething();
   Console.WriteLine(result);
 }
 
 static async Task<string> SaySomething() {
   await Task.Delay(5);
   Console.WriteLine("test");
   result = "Hello world!";
    Console.WriteLine(result);
   return “Something”;
 }
}

Output

test123
test
Hello world!

Middleware vs Filters

 Middleware vs Filters

Use Middleware When
      You need to handle every request or response in a consistent manner accross the application 

Use Filters When
      Suppose if you want to implement certain logic to particular API/Controller

C# Web API Flow

 C# Web API Flow

  1. Receive Request
  2. Middleware Pipeline(Authentication, authorization, Logging, Error handling, Routing)
  3. Routing
  4. Controller Activation
  5. Model Binding
  6. Action Filters
  7. Execute Actions
  8. Result Execution
  9. Response Sent
  10. Logging and Cleanup

OAuth and C# Integration

 OAuth and C# Integration

Authenticate users with Microsoft Accounts
      
Steps
  1. Register your application - Microsoft Identity Platform(Azure Active Directory)
  2. Install required Nuget packages.
    1. Microsoft.Identity.Client
  3. Configure OAuth Settings - including the ClientID, tenantId, Client Secret
  4. Implementing Authentication
  5. Handling Token acquistion - method AcquireTokenByAuthorization Code
  6. Refresh Token

Implementing OAuth validation in a Web API

 I mplementing OAuth validation in a Web API Implementing OAuth validation in a Web API using C# typically involves several key steps to sec...