Middleware in .Net core

Image
Middleware In ASP.NET Core, middleware is software components that are added to the request pipeline to handle requests and responses. Middleware components are executed in the order they are added to the pipeline, and each component can process the request or response, perform some action, or modify the request or response before passing it to the next middleware in the pipeline. Middleware components are added in the Startup.cs file in the Configure method. Here's a basic example of how middleware is added in an ASP.NET Core application: public class Startup { // Other configuration methods... public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler("/Home/Error"); app.UseHsts(); } app.UseHttpsRedirection(); app.UseStaticF...

Add Transient vs Add Scoped vs Add Singleton Object lifetime in .Net Core

In the context of dependency injection in .NET Core (or ASP.NET Core), AddScoped and AddTransient are methods used to register services with the built-in dependency injection container. These methods are part of the IServiceCollection interface and are typically used in the ConfigureServices method of the Startup class. Lifetime: AddScoped: This method registers a service with a scoped lifetime. A new instance of the service is created for each scope, and it is reused within that scope. AddTransient: This method registers a service with a transient lifetime. A new instance of the service is created each time it is requested. Usage: AddScoped: Suitable for services that should be shared within the scope of a single request or operation. The same instance will be used throughout the entire scope. AddTransient: Suitable for lightweight, stateless services where a new instance can be created every time the service is requested. Now, let's demonstrate these differences with a...