Net Core Interview Questions
Net Core Interview Questions
Net Core Interview Questions
The .NET Core platform is a new .NET stack that is optimized for open
source development and agile delivery on NuGet.
.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.
.NET Core also includes the base class libraries. These libraries are largely
the same code as the .NET Framework class libraries, but have been
factored (removal of dependencies) to enable to ship a smaller set of
libraries. These libraries are shipped as System.* NuGet packages on
NuGet.org.
What is CTS?
The Common Type System (CTS) standardizes the data types of all
programming languages using .NET under the umbrella of .NET to a
common data type for easy and smooth communication among these .NET
languages.
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:
The Task class from the Task Parallel Library offers the best of both
worlds. Like the ThreadPool, a task does not create its own OS
thread. Instead, tasks are executed by a TaskScheduler; the default
scheduler simply runs on the ThreadPool. Unlike the ThreadPool,
Task also allows you to find out when it finishes, and (via the generic
Task) to return a result.
Explain what is included in .NET Core?
A .NET runtime, which provides a type system, assembly loading, a
garbage collector, native interop and other basic services.
A set of framework libraries, which provide primitive data types, app
composition types and fundamental utilities.
A set of SDK tools and language compilers that enable the base
developer experience, available in the .NET Core SDK.
The 'dotnet' app host, which is used to launch .NET Core apps. It
selects the runtime and hosts the runtime, provides an assembly
loading policy and launches the app. The same host is also used to
launch SDK tools in much the same way.
What are the benefits of Explicit Compilation (AOT)?
Ahead of time (AOT) delivers faster start-up time, especially in large
applications where much code executes on startup. But it requires more
disk space and more memory/virtual address space to keep both the IL and
precompiled images. In this case the JIT Compiler has to do a lot of disk
I/O actions, which are quite expensive.
What is CoreCLR?
CoreCLR is the .NET execution engine in .NET Core, performing functions
such as garbage collection and compilation to machine code.
Consider:
What is Kestrel?
Kestrel is a cross-platform web server built for ASP.NET Core based
on lib uv – a cross-platform asynchronous I/O library.
It is a default web server pick since it is used in all ASP.NET Core
templates.
It is really fast.
.NET Framework
.NET Core
.NET Core and the .NET Framework have (for the most part) a subset-
superset relationship. .NET Core is named “Core” since it contains the core
features from the .NET Framework, for both the runtime and framework
libraries. For example, .NET Core and the .NET Framework share the GC,
the JIT and types such as String and List.
.NET Core was created so that .NET could be open source, cross platform
and be used in more resource-constrained environments.
When should we use .NET Core and .NET Standard Class
Library project types?
Use a .NET Standard library when you want to increase the number
of apps that will be compatible with your library, and you are okay
with a decrease in the .NET API surface area your library can access.
Use a .NET Core library when you want to increase the .NET API
surface area your library can access, and you are okay with allowing
only .NET Core apps to be compatible with your library.
Build web applications, IoT (Internet of things) apps, services and mobile
Back ends.
Run on .Net Core.
You can do your development on Linux, Windows and MacOS.
Deploy your code to cloud or on-premises.
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
Integration of many client-side frameworks like Angular any version.
Blazor allow you to use C# code in browser with JavaScript code.
app.UseHttpsRedirection();
// other middleware components
}
}
Startup class is specified inside the 'CreateHostBuilder' method when the
host is created. Multiple Startup classes can also be defined for different
environments, At run time appropriate startup class is used.
What is the role of ConfigureServices and Configure method?
ConfigureServices method is optional and defined inside startup class as
mentioned in above code. It gets called by the host before the 'Configure' method
to configure the app's services.
You can configure the services and middleware components without the Startup
class and it's methods, by defining this configuration inside the Program class
in CreateHostBuilder method.
services.AddScoped();
Public class A {
dep.SomeMethod();
But these direct dependencies can be problematic for the following reasons.
• Transient - Services with transient lifetime are created each time they are
requested from service container. So it's best suited for stateless, light weight
services.
• Scoped - Services with scoped lifetime are created once per connection or
client request. When using scoped service in middleware then inject the service
via invoke or invokeAsync() method. You should not inject the service via
constructor injection as it treats the service behavior like Singleton.
• Singleton - Service with singleton lifetime is created once when first time
the service is requested. For subsequent requests same instance is served by
service container.
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
• Dependency Injection
• Configuration
• Logging
• Middleware components
.NET Generic Host is recommended and ASP.NET Core template builds a .NET
Generic Host on app startup.
// Host creation
{
CreateWebHostBuilder(args).Build().Run();
WebHost.CreateDefaultBuilder(args)
.UseStartup();
ASP.NET Core use the Kestrel web server by default. ASP.NET Core comes with:
• HTTP.sys server that's a Windows-only HTTP server and it's based on the
HTTP.sys kernel driver and HTTP Server API.
• Custom Providers
class Test{
Configuration = configuration;
}
}
Default configuration provider first load the values from appsettings.json and
then from appsettings.Environment.json file.
You can also read the appsettings.json values using options pattern described
Read values from appsettings.json file.
app.UseEndpoints(endpoints =>
});
});
if (env.IsDevelopment())
app.UseDeveloperExceptionPage();
else
app.UseExceptionHandler("/Error");
app.UseHsts();
You can configure error code specific pages in Startup class Configure method as
below.
await next();
if (context.Response.StatusCode == 404)
context.Request.Path = "/not-found";
await next();
}
context.Request.Path = "/Home/Error";
await next();
});
How ASP.NET Core serve static files?
In ASP.NET Core, Static files such as CSS, images, JavaScript files, HTML are the
served directly to the clients. ASP.NET Core template provides a root folder called
wwwroot which contains all these static files. UseStaticFiles() method inside
Startup.Configure enables the static files to be served to client.
You can serve files outside of this webroot folder by configuring Static File
Middleware as following.
app.UseStaticFiles(new StaticFileOptions
});
• Cookies
• Session State
• TempData
• Query strings
• Hidden fields
• HttpContext.Items
• Cache
Apps running on multiple server should ensure that sessions are sticky if they are
using in-memory cache. Sticky Sessions responsible to redirect subsequent client
requests to same server. In-memory cache can store any object but distributed
cache only stores byte[].
For example a user visits some site 'www.abc.com' then browser performs
authentication successfully and stores the user information in cookie and perform
some actions, In between user visits some other malicious site 'www.bad-
user.com' and this site contains some code to make a request to vulnerable site
(www.abc.com). It's called cross site part of CSRF.
• For more you can visit Prevent Cross-Site Request Forgery (XSRF/CSRF)
What is the main reason of using .Net core rather than
MVC ?
Net core has following benefits over .NET framework
Cross platform support
Best suited for developing scalable & independent micro services
Built in Dependency injection
Open source
CLI support
Self contained - Does not require any software to be installed on target
machine/server
Low cost- Runs on VS code.
This Program.cs creates the application Host. It configures the setiings file like
appsettings.json,logging using CreateDefaultBuilder(args) and Startup.cs through
webBuilder.UseStartup() which further helps in building dependencies.