Skip to main content

ASP.NET Core Advanced Interview Questions - Part -1

 1)         In ASP.NET Core what CreateHostBuilder will do?

In Program.cs, this is the very first thing happening in the application, this function will create object for the host and based on the host object the subsequent processes will happen like creating a scope, creating services objects, etc.

2)         What is host in ASP.NET Core?

As a first step the ASP.NET Core apps configure and launch a host. The host is responsible for app startup and lifetime management. Ideally, the host configures a server and a request processing pipeline. The host can also set up logging, dependency injection, and configuration.

3)         What is the purpose of Program.cs in ASP.NET Core?

This is the entry point of the ASP.NET Core application where the program will build the host object and create scope of the application

4)         What is Scope in ASP.NET Core?

In ASP.NET Core, we can create and manage custom scopes by calling CreateScope() when for the need of custom services that live outside of a HttpRequest, the interface used for this is IServiceProvider

5)         What is Startup class?

This is the 2nd thing called after the Program.cs in the ASP.NET Core application, now Startup.cs file is not present from version 6.0

6)         What are ConfigureServices and Configure methods?

Both the methods were present until version 5.0 and no longer present from the version 6.0,

Configure Services method:  This method gets called by the runtime. Use this method to add services to the container.

Example: 

Configure method: This method gets called by the runtime. Use this method to configure the HTTP request pipeline.

Example:


 

 

As mentioned earlier from version 6.0 we don’t have the Startup.cs file, all the configurationservices and configure items has been moved to Program.cs and there will be no dedicated method to categorize the both, programmers will use with comments like this.

7)         What is the process in Request Pipeline?

8)         While creating services how will you determine the lifetime and scope of the objects and how can you specify this?

There are 3 ways to create the services objects and set the scope of them

a)         Transient: Creates fresh new objects for every request, example: A new sale order object, new employee object

b)         Scoped: These will be same for each request or session, but different for different sessions, say for example user session details will be provided in a single object for a given user and a different user session object will be built for another user

c)          Singleton: Only one object will be created throughout the application (not user specific), example: making log entries of the application requires only single object for the application lifetime.

9)         What is the difference between IOC and DI?

IOC (Inversion of control) is the design target intended to achieve between the classes and their dependencies

 

DI (Dependency Injection) is one of the design patterns used to achieve this target

 

There are other ways to achieve IOC which is subjected to the application

 

As the name suggests the flow of controls is inverted to improve the performance, maintainability and scalability.

 

10)  What is the difference between app.run and app.use

Both will do the task of adding the middleware to the request pipeline, but the basic difference is app.run will terminate the request and does not process the further requests

 

Where as the app.use will add the middleware to the request pipeline and transfer the process to the next middleware.

Comments

Popular posts from this blog

Using of global variables in C# - Drawbacks & Solutions

How using global variables can have implications on the design, maintainability, and test-ability of C# code: Harder to understand and reason about the code:       class Program     {         public static int globalCounter = 0;         static void Main()         {             globalCounter++;             Console.WriteLine(globalCounter);         }     }   In this example, the global variable globalCounter is accessible from anywhere in the program, including the Main method. It's not clear where the value of the globalCounter is updated, it could be updated in other methods or classes, making it harder to trace the flow of data and understand the source of bugs.   More prone to errors:       class Program     {         public static string globalString;         static void Main()         {             globalString = "Hello" ;             Method1();             Method2();         }         static void Method1()         {

Task Parallel Library (TPL) and Akka.NET Alternatives

Task Parallel Library (TPL) and Akka.NET are among the most commonly used libraries for parallel and concurrent programming in the .NET ecosystem. However, there are also several other options available, depending on your specific needs: Parallel Language Integrated Query (PLINQ) is a parallel programming feature of .NET that provides an easy and efficient way to perform operations on collections in parallel. LINQ (Language Integrated Query) is a powerful feature in .NET that allows developers to work with data in a more declarative and language-integrated manner. While LINQ queries are inherently sequential, PLINQ extends LINQ by providing parallel versions of the query operators, allowing some queries to execute faster by utilizing multiple processors or cores on a machine. PLINQ is great when you are working with large collections where operations might be CPU-intensive or I/O-bound and could potentially be sped up by parallel execution. Here is a simple example of a PLI

SOLID Principles with Real World examples in C#

  SOLID Principles with Real World examples in C#   SOLID principles are formed by using S Single Responsibility Principles (SRP) O Open Closed Principle (OCP) L Liskov’s Substitution Principle (LCP) I Interface Segregation Principle (ISP) D Dependency Inversion Principle (DIP)   S Single Responsibility Principles (SRP) There should never be more than one reason for a class to change, to be precise one class should have only one responsibility Single Responsibility Principles (SRP) Real world example, A perfect match for SRP is Microservices , a Microservice will not contain functionalities other than the one it is designated to do,  Example ·                   Order Processing Service, ·                   Shipment Management Service, ·                   User Authentication Service, ·                   Catalogue List Service       class OrderProcessor     {         public void Process(Order order)         {             // Check inven