NET Core Interview Question & Answers - ByteHide Blog
NET Core Interview Question & Answers - ByteHide Blog
Index
Dive into the world of contemporary programming as we untangle some of the most
commonly asked dotnet core interview questions and answers!
Staying up-to-date with the evolution of technology can be a demanding task, but not
if you’re provided with clear explanations and useful insights. In this chunk of
knowledge, we’ll delve deeply into the key aspects of .NET Core, allowing you to
showcase your expertise and ace your next interview.
Answer
In .NET Core, Dependency Injection (DI) is a technique whereby one class (or module)
outsources the creation of its dependencies to an external framework instead of
handling them itself.
Built-in DI is a significant aspect of .NET Core. It comes with integrated support for
dependency injection, unlike the previous versions, where we had to rely on external
libraries like AutoFac, Ninject, Unity, etc.
Answer
In terms of performance, both .NET Core and Node.js have their strengths and
weaknesses. Performance can also be relative based on the specific workload and
requirements of the application, but here are some general insights:
Response Time: .NET Core generally holds an edge over Node.js in raw response
time. .NET Core has been designed to deal with high-performance servers and
optimized accordingly.
Concurrent Request Handling: Node.js uses a single-threaded, event-driven
architecture that is efficient for handling high volumes of concurrent, IO-intensive
workloads. In contrast, .NET Core uses a multi-threaded approach, which can lead
to higher resource consumption under heavy loads but can handle CPU-intensive
workloads more efficiently.
Throughput: In most cases, .NET Core exhibits high throughput rates compared
to Node.js due to its inherent design and the optimization done by the JIT
compiler.
Remember, while these are some of the general observations, the real-life
performance of an application will depend on various factors, including how well the
code is written and optimized.
How do you manage cross-platform
targeting in a .NET Core project?
Answer
.NET Core projects are capable of targeting multiple platforms including Windows,
Linux, and macOS. This can be managed through the project file (*.csproj).
First, the Target Framework Moniker (TFM) determines the APIs that you have access
to. It is possible to specify multiple target frameworks and libraries depending on your
application needs. The selected framework will dictate which APIs your code can call.
Here is an example where the project targets .NET Core 3.1 and .NET Standard 2.0:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netcoreapp3.1;netstandard2.0</TargetFrameworks>
</PropertyGroup>
</Project>
You need to be aware of any breaking changes or incompatible APIs when targeting
multiple platforms. It is also possible to handle divergent code with the help of
preprocessor directives.
Answer
Entity Framework Core (EF Core) is a lightweight, extensible, open-source, and cross-
platform version of the popular Entity Framework data access technology. Here are
the main advantages of using EF Core:
Cross-Platform: EF Core works with .NET Core, which makes it possible to run on
any platform that .NET Core supports.
Improved Performance: Compared to EF 6.x, EF Core provides better
performance because of its optimized query generation and processing.
Modular Design: EF Core is more modular, which means you can include only
the necessary packages your application needs, leading to less clutter and
overhead.
Built-In Dependency Injection: EF Core supports the new .NET Core
dependency injection feature out of the box.
Flexibility and Extensibility: EF Core is designed to be flexible and extensible,
allowing developers to adapt the framework to their specific needs.
Answer
Kestrel is a cross-platform web server built for .NET Core based applications. It is
based on a libuv library, an asynchronous I/O-based model used by Node.js. Kestrel
can also be used in combination with a reverse proxy server such as Apache, Nginx, or
IIS, which provides an additional layer of configuration, security, and load balancing.
Here are the key differences between Kestrel and other web servers such as IIS and
Http.sys:
Cross-Platform: Unlike IIS and Http.sys, which are Windows-specific, Kestrel can
run on multiple operating systems thanks to its integration with .NET Core.
Performance: Kestrel is designed to be fast and has been heavily optimized for
performance. It demonstrates high throughput and has a lower request latency
than traditional IIS.
Stand-Alone or with Reverse Proxy: Kestrel can be used as a stand-alone web
server or with a reverse proxy. Using it with a reverse proxy can help protect the
application from potential vulnerabilities.
In-Process or Out-Of-Process Models: With the advent of .NET Core 2.2 and 3.0,
Kestrel can be used in both in-process and out-of-process hosting models,
providing flexibility in how your application is hosted.
Overall, when considering a web server for hosting .NET Core applications, Kestrel
provides a high-performance, cross-platform, and flexible option that aligns well with
the objectives of .NET Core.
Certainly, transitioning from the way Kestrel servers operate in .NET Core to the
application of patterns in Entity Framework Core might seem like a leap. But don’t fret,
as we’ll guide you through the interconnected world of .NET Core with simple
explanations and concise examples.
Answer
Here is a simple example of what the repository class might look like:
Answer
Global exception handling in ASP.NET Core can be achieved using middleware. The
ideal place to handle exceptions is in the middleware pipeline so that it is separate
from your application logic, and can trap any unhandled exceptions.
In the above code, when an exception is raised it is handled by the “/api/Error” route.
Answer
There are a number of significant differences between .NET Core 2.x and .NET Core 3.x:
Desktop Development: .NET Core 3 introduced support for Windows Desktop
Applications (Windows Forms and WPF). This feature was not available in .NET Core 2.
New JSON APIs: .NET Core 3.0 introduced new built-in JSON APIs that are high-
performance and low allocation, making them significantly faster than JSON.NET.
HTTP/2 Support: In .NET Core 3, built-in support for HTTP/2 was added.
gRPC Services: .NET Core 3.x introduced support for gRPC, a modern open-source
high-performance RPC framework.
Support for ARM64: .NET Core 3.0 added support for ARM64-based processors.
Answer
Azure Active Directory (Azure AD) provides an easy way for businesses to manage
identity and access, both in the cloud and on-premises. You can use it to add sign-in
functionality and regulate access to your .NET Core applications. Here are the essential
steps to configure and work with Azure AD in a .NET Core application:
Open your application settings in the Azure portal, copy the Directory (tenant) ID and
Application (client) ID, and then use them to configure the authentication in your .NET
Core application.
services.AddControllers();
}
[Authorize]
public class SecureController : Controller
{
//... controller logic here
}
You can use Azure AD to store and manage users for your .NET Core application.
Entity Framework Core (EF Core) includes a feature called Migrations that allows you
to make changes to your model and then propagate those changes to your database
schema. Here’s a basic overview of performing a migration:
2. Create a new Migration: Use the Add-Migration command and provide a name
for your migration. This creates a new class in your Migrations folder that contains the
changes to be made to the database schema.
Add-Migration MyFirstMigration
3. Update the database: Use the Update-Database command. This applies any
pending migrations on the database. EF Core creates the database if it does not exist.
Update-Database
You may opt to provide a specific migration name if you want to update the database
to a specific migration rather than the latest.
Remember to treat migrations as a part of your codebase. They need to be kept under
source control and applied as part of your deployment process. Well-managed
migrations are key to dealing with evolving database schemas.
Answer
SignalR uses WebSockets, Long Polling, Server-Sent Events, and others to maintain
this connection, choosing the best available transport method based on the client and
server capabilities.
Answer
IdentityServer4 is an OpenID Connect and OAuth 2.0 framework for .NET Core, which
can be used to secure your .NET Core Web API.
To do this, you would need to install and configure IdentityServer4 in your application.
This will involve creating the necessary client configurations, defining scopes, and
securing your API endpoints.
It’s important to remember that securing your API requires a clear understanding of
authentication and authorization concepts, as well as the protocols used by
IdentityServer4 (OpenID Connect and OAuth 2.0).
Answer
For managing configuration, you can use the appsettings.json file, which is the default
file read by .NET Core application. Moreover, you can have dedicated configuration
files for each environment, such as appsettings.Development.json or
appsettings.Production.json, giving you the chance to override the default settings.
Remember to exclude sensitive data like connection strings and application secrets
from source control by using secure methods like user secrets or Azure Key Vault.
Answer
With .NET Core, you get an in-built, easy-to-use logging API which supports a variety
of built-in and third-party logging providers. It provides various logging levels like
Trace, Debug, Information, Warning, Error, and Critical.
The steps to add logs to a .NET Core application using this framework are as follows:
Inject the ILogger<T> dependency in your class, where T is the class into which
ILogger<T> is injected.
Use the provided methods (LogDebug, LogInformation, LogWarning,
LogError, LogCritical) to log messages at different levels.
You can configure these log levels and logging output targets in the appsettings.json
or in your code. The framework automatically reads from the configuration settings at
runtime.
Answer
Create two separate models: a write model (for commands) and a read model
(for queries).
Define distinct interfaces/classes for your commands and queries.
Use the MediatR library, which provides a straightforward way to implement the
CQRS pattern. You define your queries and commands as Request/Notification
handlers.
For the read operations, you can design the models according to the needs of
the client application, while the write model can be closer to the
business/domain model.
Remember, while CQRS can provide performance and scalability benefits, it adds
complexity to the application and should be used wisely, preferentially in more
complex scenarios where these benefits are significant.
Answer
There are several differences in how routing is handled in ASP.NET Core MVC
compared to ASP.NET MVC.
In ASP.NET MVC, routing was handled through a central RouteConfig file that
includes rules for each controller and action method. These routing rules can be
either attribute-based or convention-based.
On the other hand, ASP.NET Core MVC uses middleware for routing. It provides
two types of routing methodologies – convention-based routing and attribute
routing. All the routing information is configured in the Startup.cs file.
In ASP.NET Core MVC, we also have a new feature, Routing to Razor Pages. Razor
Pages are a new aspect of ASP.NET Core MVC that allow you to create page-
based programming model.
Additionally, ASP.NET Core MVC supports route constraints, which weren’t
available in ASP.NET MVC. Route constraints let you restrict how the parameters
in the route template are matched.
Answer
Caching: It’s about storing data somewhere for future use to speed up the
application. .NET Core provides support for several different types of caching
including in-memory caching, distributed caching, and response caching.
Asynchronous Programming: Use the async and await keywords to write
asynchronous code that’s easier to read, write and manage.
Pooling: Techniques such as Object Pooling or Connection Pooling can be used
to recycle objects or connections instead of creating and destroying them
frequently.
Optimization of Data Access: Minimize the amount of data that you send over
the network and reduce the number of server round trips.
Use of Middleware: Middleware components are used to handle requests and
responses. Ensure that unnecessary middleware components are not registered.
Answer
Managing session state in a distributed .NET Core application generally involves either
a server-side or client-side session management approach.
Handling errors and debugging in a distributed .NET Core application with multiple
microservices can be challenging. Some of the strategies include:
Wrapping up our deep dive into net core questions for interviews, we hope you
found the thorough explanations and code examples enlightening.
Remember, the journey of a thousand miles begins with a single step, and every
complex problem can be broken down into simple, easy-to-understand blocks. And
that’s precisely what our exploration of the .NET Core environment aimed to do.
So whether you’re preparing for your next interview or simply seeking to solidify your
.NET Core understanding, keep this guide in your arsenal!