Net Core Interview

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

What is .NET Core? .NET Core is a newer version of .

NET, which is cross-


platform, supporting Windows, MacOS and Linux, and can be used in device,
cloud, and embedded/IoT scenarios. characteristics best define .NET Core:
 Flexible deployment: Can be included in your app or installed side-by-side user or
machine-wide.
 Cross-platform: Runs on Windows, MacOS and Linux; can be ported to other OSes.
The supported Operating Systems (OS), CPUs and application scenarios will grow
over time, provided by Microsoft, other companies, and individuals.
 Command-line tools: All product scenarios can be exercised at the command-line.
 Compatible: .NET Core is compatible with .NET Framework, Xamarin and Mono, via
the .NET Standard Library.

What are the benefits of using ASP.NET Core over ASP.NET?


 Cross platform, provide ability to develop and run on Windows, Linux and
MacOS.
 Open-source
 Unified Platform to develop Web UI and services.
 Built-in dependency injection.
 Ability to deploy on more than one server like IIS, Kestrel, Nginx, Docker,
Apache etc
 cloud enabled framework, provide support for environment based configuration
systems.
 Lightweight, High performance and modern HTTP request pipelines.
 well suited architecture for testability

"Dependency Injection" :

ASP.NET Core supports the dependency injection (DI) software design pattern, which is a technique for
achieving Inversion of Control (IoC) between classes and their dependencies
DI help us to create application which loosely coupled. This means that object should only have those
dependency that required during complete task. The main advantages of DI (Dependency Injection) is
our application loosely coupled and has provide greater maintainability, testability and also re-usability.
It is loosely coupled because dependency required by the class are injected from outer world rather
than created them self directly win-in code.
"Repository;"
The Repository Design Pattern in C# Mediates between the domain and the data mapping layers using a
collection-like interface for accessing the domain objects. In other words, we can say that a Repository
Design Pattern acts as a middleman or middle layer between the rest of the application and the data
access logic.

Why We use Repository pattern:


With the Repository pattern, we create an abstraction layer between the data access and
the business logic layer of an application. By using it, we are promoting a more loosely
coupled approach to access our data from the database. Also, the code is cleaner and easier to
maintain and reuse.
What is middleware in ASP.NET Core with example?
Middleware is a piece of code in an application pipeline used to handle requests and
responses. For example, we may have a middleware component to authenticate a
user, another piece of middleware to handle errors, and another middleware to serve
static files such as JavaScript files, CSS files, images

Transient :Transient objects are always different; a new instance is provided to every controller and
every service.
Scoped : Scoped objects are the same within a request, but different across different requests.
Singleton: Singleton objects are the same for every object and every request.

What ConfigureServices() method does in Startup.cs?


This method is optional. It is the place to add services required by the application.
For example, if you wish to use Entity Framework in your application then you can
add in this method.

What Configure() method does in Startup.cs?


The Configure method is used to specify how the ASP.NET application will respond to HTTP requests.
The request pipeline is configured by adding middleware components to an IApplicationBuilder instance
that is provided by dependency injection. There are some built-in middlewares for error handling,
authentication, routing, session and diagnostic purpose. Highlighted lines in below code, are built-in
Middleware with ASP.NET Core 1.0.

What is Kestral?
Kestrel is a cross-platform web server for ASP.NET Core based on libuv, a cross-platform asynchronous
I/O library. Kestrel is the web server that is included by default in ASP.NET Core new project templates. If
your application accepts requests only from an internal network, you can use Kestrel by itself.

How to enable Session in ASP.NET Core?


The middleware for the session is provided by the package Microsoft.AspNetCore.Session. To
use the session in ASP.NET Core application, we need to add this package to csproj file and
add the Session middleware to ASP.NET Core request pipeline.
public class Startup

{ public void ConfigureServices(IServiceCollection services)


{

services.AddSession();

services.AddMvc();

public void Configure(IApplicationBuilder app, IHostingEnvironment


env)

app.UseSession();

….

….

JWT Token: JWT (JSON web token) has become more and more popular in web development. It is an
open standard which allows transmitting data between parties as a JSON object in a secure and
compact way. The data transmitting using JWT between parties are digitally signed so that it can be
easily verified and trusted.

Way to implement JWT


1. Client sends a login request with username and password to server.
2. Server receives the username and password, authenticate the user.
3. If authentication is successful, then the server creates a JWT token called accessToken that
stores user public info's and sends it back to the client.
https://www.c-sharpcorner.com/article/implement-jwt-in-net-core-api/

Token Base Authentication processes,

1. The client sent a request to the server with credentials.


2. The server validates the credential and creates an Access token and sends it back to
the client.
3. All subsequence requests content this token until its expired.
https://www.c-sharpcorner.com/UploadFile/puranindia/ASP-NET-MVC-
Inter

SOLID Principles
There are five SOLID principles:
1. Single Responsibility Principle (SRP)
2. Open Closed Principle (OCP)
3. Liskov Substitution Principle (LSP)
4. Interface Segregation Principle (ISP)
5. Dependency Inversion Principle (DIP)
Single Responsibility Principle (SRP)
Definition: A class should have only one reason to change.

Open Closed Principle (OCP)


Definition: Software entities (classes, modules, functions, etc.) should
be open for extension, but closed for modification.

Liskov Substitution Principle (LSP)


Definition by Robert C. Martin: Functions that use pointers or
references to base classes must be able to use objects of derived
classes without knowing it.

Interface Segregation Principle (ISP)


Definition: No client should be forced to implement methods which it
does not use, and the contracts should be broken down to thin ones.

Dependency Inversion Principle (DIP)


1. High-level modules should not depend on low-level modules. Both
should depend on abstractions.
2. Abstractions should not depend on details. Details should depend
on abstractions.

DbContext
The DbContext class is an integral part of Entity Framework. An instance of DbContext
represents a session with the database which can be used to query and save instances of
your entities to a database. DbContext is a combination of the Unit Of Work and Repository
patterns.

What is the difference between IApplicationBuilder.Use() and


IApplicationBuilder.Run()?
We can use both the methods in Configure methods of startup class. Both are used
to add middleware delegate to the application request pipeline. The middleware
adds using IApplicationBuilder.Use may call the next middleware in the pipeline
whereas the middleware adds using IApplicationBuilder.Run method never calls the
subsequent ore next middleware. After IApplicationBuilder.Run method, system
stop adding middleware in request pipeline.

How to enable Session in ASP.NET Core?


The middleware for the session is provided by the package
Microsoft.AspNetCore.Session. To use the session in ASP.NET Core
application, we need to add this package to csproj file and add the Session
middleware to ASP.NET Core request pipeline.

public class Startup

public void ConfigureServices(IServiceCollection services)

….

….

services.AddSession();

services.AddMvc();

public void Configure(IApplicationBuilder app, IHostingEnvironment


env)

….

….

app.UseSession();

….

….

}
}

What is microservice in .NET core?


Microservice is an approach to create small services each running in their own
space and can communicate via messaging. These are independent services
directly calling their own database.

Advantages of Microservices
 Services can be written in different programming language and
can be accessed by using any framework.
 Independently develop, deploy, redeploy, version and scale
component services in seconds without compromising the
integrity of an application
 Better fault isolation keeps other services to work even though
on got failed.
 Zero downtime upgrades.
 Services can be of from different servers or even different
datacenters.
 Interaction with other services in a well-defined protocol
 Monitor, capture, and report health diagnostics
 Reliable and self-healing
 Supports continuous integration and delivery
 Easy to transfer knowledge to the new team member
 Easy to integrate with third parties

Disadvantages of Microservices
 The additional complexity for implementation of an inter-
process communication mechanism between services.
 Writing automated tests involving multiple services is
challenging and It can be difficult to create consistent testing
environments.
 Requires high level of automation to manage multiple
instances of different types of services in production.
 Everyone has to manage eventual consistency as maintaining
string consistency becomes extremely difficult.
 Managing multiple databases and their transactions are
difficult.
 Inter-process calls are slow.
 Debugging will become difficult.
 Complexity in DevOps.
 Production monitoring cost is higher.
 Formal documentation overhead.
 Lack of governance.

.Net Core Application Optimization:

Always use the latest version of ASP.NET Core

Avoid synchronous calls at any level


While developing ASP.NET Core applications, try to avoid creating blocking
calls. Blocking call means a call that blocks the next execution until it is not
completed. Blocking call or Synchronous call can be anything, either you are
fetching the data from an API or performing some internal operations. You should
always execute the call in an asynchronous manner.

Always use Asynchronous Programming (ASYNC-AWAIT)


ASP.NET Core uses the same Asynchronous programming paradigm to make an
application more reliable, faster, and responsive.

AVOID TASK.WAIT OR TAST.RESULT WITH ASYNCHRONOUS


PROGRAMMING

While working with Asynchronous Programming we can avoid the use of


Task.Wait and Task.Result and try to use await because -

1. These block the thread until the task is completed and wait for the
completion of the task. Wait blocks the thread synchronously until the
task completion.
2. Wait and Task.Result both wrap any type of exception in
AggregateException and create complexity while doing exception
handling. If you use the await instead
of Task.Wait and Task.Result then you don’t have to worry about
exception handling.
3. Sometimes, these both block the current thread and
create DEADLOCKS
4. Wait and Task.Result only can be used if parallel task execution is
going on. We recommend that you don’t use it
with Async programming.
PERFORM I/O OPERATIONS ASYNCHRONOUSLY

While performing I/O operations, you should perform them


asynchronously so it won't affect other processes. I/O operations mean, doing
some execution with a file like uploading or retrieving files. It could be
anything like image uploading, file uploading or anything else. If you try to
accomplish it in a synchronous manner then it blocks the main thread and
stops the other background execution till the I/O does not complete. So, as
per the performance point of view, you should always use the Asynchronous
execution for I/O operations.

ALWAYS USE CACHE

We can increase the performance of the application if we can reduce the number of
requests to the server every time. The first time, you will make the call to the server
and get the response and this response is stored somewhere for the future for some
time (there will be some expiry) and next time when you will make the call for the
same response then first you will check if you have already got data in the first
request and stored somewhere and if it is yes then you will use the stored data
rather than making the call to the server.

Keeping the data in that location from where we can get it frequently without
making the call to the server is a good practice. Here, we can use the
Cache. Caching the content helps us to reduce server calls again and again and it
helps us to increase the performance of the application. We can do the cache at any
point of a location like on client-side caching, server-side caching or client/server-
side caching.

OPTIMIZE DATA ACCESS

We can also improve the performance of an application by optimizing data


access logic and database tables, and queries. As we all know, most of the
applications use some kind of data source and every time we fetch data from
a database, it affects the application performance. If your database load is
slow, the entire application will be slow. Here are some tips:

1. Reduce the number of HTTP calls, which means you should always try
to reduce the number of network round trips.
2. Try to get all the data in one go. This means rather than making
multiple calls to the server, just make one or two calls which can bring
all the required data.
3. Frequently set the cache on data which is not being changed.
4. Don’t try to get the data in advance which is not required, it will
increase the load on the response and your application will load slower.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy