In previous article, I have mentioned OOPS interview questions in C# (With Answers) and ASP.NET MVC Interview Questions and Answers but now in this article, I have mentioned ASP.NET Core interview questions with answers, which covers basic to advanced skills questions.

What is ASP.NET Core?

ASP.NET Core is a cross-platform, high-performance, and open-source web application framework. Microsoft released the first version of ASP.NET Core in 2016. It enables developers to create modern, cloud-enabled applications.

.NET Core has two major components. It includes a small runtime that is built from the same codebase as the .NET Framework CLR.

The .NET Core runtime includes the same GC and JIT (RyuJIT), but doesn't include features like Application Domains or Code Access Security. The runtime is delivered via NuGet, as part of the ASP.NET Core package.

What are the features of ASP.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.
  • Open-Source: .NET Core platform is open source, using MIT and Apache 2 licenses

Can ASP.NET Core work with the .NET framework?

Yes, ASP.NET Core can work with .NET framework and .NET core framework.

What is CTS in .Net core?

Common Type System (CTS) describes the datatypes that can be used by managed code.
The CTS defines the predefined data types available in IL so that all languages that target the .NET Framework can produce compiled code ultimately based on these types.

CTS is designed as a singly rooted object hierarchy with System.Object as the base type from which all other types are derived. CTS supports two different kinds of types:

  • Value Types: Contain the values that need to be stored directly on the stack or allocated inline in a structure. They can be built-in (standard primitive types), user-defined (defined in source code) or enumerations (sets of enumerated values that are represented by labels but stored as a numeric type).
  • Reference Types: Store a reference to the value‘s memory address and are allocated on the heap. Reference types can be any of the pointer types, interface types or self-describing types (arrays and class types such as user-defined classes, boxed value types and delegates).

What is difference between .NET Core and .NET framework?

Here are the key differences between .NET Core and .NET:

difference-net-and-net-core-min.png

.NET Framework

  • The .NET Framework is the first implementation of .NET which works on Windows only
  • Its source code is public but Microsoft doesn't accept third party contributions for it
  • It has a very rich desktop top development framework for windows which include Windows Forms and WPF
  • A huge third-party packages library is also available for it
  • It doesn't support the in-app deployment model
  • Although it can be used with a docker container, its image size is large and can only be deployed on Windows containers

.NET Core

  • .NET Core is the latest implementation of .NET which runs on Windows, Linux, and macOS
  • Its open-source and Microsoft accepts third party contributions to .NET Core
  • It supports desktop frameworks like Windows Forms and WPF from version 3.0
  • The .NET Core also has support for a large number of third party packages as well but still, it doesn’t compete with .NET Framework in this area
  • It does support in-app deployment model
  • It is the best choice to work with docker containers

What is Kestrel?

Kestrel is an event-driven, I/O-based, open-source, cross-platform, and asynchronous server which hosts .NET applications. It is provided as a default server for .NET Core therefore, it is compatible with all the platforms and their versions which .NET Core supports.

Advantages of Kestrel are:

  • Lightweight and fast.
  • Cross-platform and supports all versions of .NET Core.
  • Supports HTTPS
  • Easy configuration

What is CoreCLR?

CoreCLR is the .NET execution engine in .NET Core, performing functions such as garbage collection and compilation to machine code.

What are complex and supporting methods?

  • The System.Numerics.Complex type represents a complex number, i.e., a number with a real number part and an imaginary number part
  • It supports a standard set of arithmetic, comparison, equality, explicit conversion, and implicit conversion operators, as well as mathematical, algebraic, and trigonometric methods.

What are service lifetimes in .NET Core?

.NET Core supports a design pattern called ‘Dependency Injection’ which helps in the implementation of IoC(Inversion of Control). During registration, dependencies require their lifetime to be defined. The lifetime of service decides under what condition the instance of the service will be created and till what time it will be live.

There are three types of service lifetimes supported by .NET Core:

  • Transient Service: Instance is created each time it is requested.
  • Scoped Service: User-specific instance is created once per user and shared across all the requests.
  • Singleton Service: Single Instance is created once a lifetime of the application.

What is a generic host in .NET Core?

The generic host was previously present as 'Web Host', in .NET Core for web applications. Later, the 'Web Host' was deprecated and a generic host was introduced to cater to the web, Windows, Linux, and console applications.

Whenever a new application is started we are required to take care of the below points:

  • Dependency Injection
  • Configuration
  • Logging
  • Service lifetime management

.NET generic host called 'HostBuilder' helps us to manage all the above tasks since it is built on the original abstraction of these tools.

What is routing in .NET Core?

It is a process through which the incoming requests are mapped to the corresponding controllers and actions. The .NET Core MVC has a routing middleware to perform this task. This middleware matches the incoming HTTP requests to the executable request-handling code. We can define the routing in the middleware pipeline in the 'Startup.Configure' file.

As we can see in the below code snippet, there are two methods or pair of middleware to define routing:

  • UseRouting : Adds route which matches the middleware pipeline.
  • UseEndpoints : Adds end execution point to the middleware pipeline and runs the delegate of the endpoint.

What is the use of ConfigureServices() method 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.

public void ConfigureServices(IServiceCollection services)  
{
    services.Configure<AppSettings>(Configuration.GetSubKey("AppSettings"));
    services.AddEntityFramework()
            .AddSqlServer()
            .AddDbContext<MyDBContext>();
    // Add MVC services to the services container.
    services.AddMvc();
}

What is WebListener?

ASP.NET Core ships two server implementations Kestral and WebListener. WebListener is also a web server for ASP.NET Core that runs only on Windows. It’s built on the Http.Sys kernel mode driver. WebListener is an alternative to Kestrel that can be used for direct connection to the Internet without relying on IIS as a reverse proxy server.