Ad Code

Responsive Advertisement

Dot Net Core

Dot Net Core Interview questions and answer

1. What is .NET Core?
Ans: .NET Core is a newer version of .NET, which is open source, cross-platform, supporting Windows, MacOS and Linux, and can be used in device, cloud, and embedded/IoT scenarios.

The following 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.
2. How is it different from existing .NET framework?
Ans:

.NET Framework is a better choice if you:

  • Do not have time to learn new technology.
  • Need a stable environment to work in.
  • Have nearer release schedules.
  • We are already working on an existing app and extending its functionality.
  • Already have an existing team with .NET expertise and building production-ready software.
  • Do not want to deal with continuous upgrades and changes.
  • Building Windows client applications using Windows Forms or WPF

.NET Core is a better choice if you:

  • Want to target your apps on Windows, Linux, and Mac operating systems.
  • Cross platform
  • Much faster.
  • Are not afraid of breaking and fixing things since .NET Core is not fully matured yet.
  • A student who is just learning .NET.
  • Love open source.

As you can see from the below diagram, the .NET ecosystem has three major high-level components - .NET Framework, .NET Core, and Xamarin. 
         Xamarin is not a debate at all. When you want to build a mobile (iOS, Android, and Windows Mobile) apps using C#, Xamarin is your only choice. 

        The .NET Framework supports Windows and Web applications. Today, you can use Windows Forms, WPF, and UWP to build Windows applications in .NET Framework. ASP.NET MVC is used to build Web applications in .NET Framework.

.NET Core is the new open-source and cross-platform framework to build applications for all operating systems including Windows, Mac, and Linux. .NET Core supports UWP and ASP.NET Core only. UWP is used to build Windows 10 targets Windows and mobile applications. ASP.NET Core is used to build browser-based web applications. 


3.What is .Net Standard? How it is different from .Net Core and Portable Class Library?
.Net Standard is a specification which dictates what the Base Class Libraries of different .Net platforms should implement to unify the Base Class Libraries of different .Net Platforms. Here, Platform means full .NetFramework, .Net Core, Xamarin, Silverlight, XBox etc. This also enables code sharing between applications that runs on these different platforms. For example, a library or a component that is developed on top of a platform that implements specific .Net Standard version can be shared by all the applications that runs on any of the platforms that implements same .Net Standard version.

4. What is BCL or base class library?
.NET Base Class Library is the sub part of the Framework that provides library support to Common Language Runtime to work properly. It includes the System namespace and core types of the .NET framework.

 
5. What is ASP.NET Core?
Ans: ASP.NET Core 1.0 is the next version of ASP.NET. It is open source and cross-platform framework (supports for Windows, Mac and Linux) suitable for building cloud based internet connected applications like web apps, IoT apps and mobile apps. ASP.NET Core apps can run on .NET Core or on the full .NET Framework.

6. Why Use ASP.NET Core for Web Application Development?

Ans: ASP.NET Core is an robust, and feature-rich framework that provides features to develop super-fast APIs for web apps. ASP.NET Core should be prefered for following reason:

  • ASP.NET Core is faster compare to traditional ASP.NET.
  • 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.
  • Flexible deployment: Can be included in your app or installed side-by-side user or machine-wide. Runs on IIS or can be self-hosted in your own process.
  • Built-in support for dependency injection.
  • ASP.NET Core is cloud-ready and has improved support for cloud deployment.
  • Provides Support for Popular JavaScript Frameworks.
  • Unification Of Development Models which allows the MVC and Web API development models to use the same base class Controller.
  • Razor Pages makes coding page-focused scenarios easier and more productive.
  • Environment based configuration supported for cloud deployment.
  • Built in logging support.
  • In ASP.NET we had modules and handlers to deal with requests. In ASP.NET Core we have middleware which provides more control how the request should be processed as they are executed in the order in which they are added.
7. What are the various JSON files in ASP.NET Core?
 Different configuration json files in ASP.net Core

There are mainly 6 configuration JSON files in ASP.net Core.

  • global.json
  • launchsettings.json
  • appsettings.json
  • bundleconfig.json
  • project.json
  • bower.json
8.What is global.json file?
Ans: You can define the solution level settings in global.json file. If you will open the global.json file, you will see below code:
{
  "projects": [ "src", "test" ],
  "sdk": {
    "version": "1.0.0-preview2-003121"
  }
}
projects: projects property defines the location of source code for your solution. It specifies two location for projects in the solution: src and test.src contains actual application and test contains any test.

9.What is launchsettings.json file?
Ans: In launchsettings.json file, You can define project specific settings associated with each profile Visual Studio is configured to launch the application, including any environment variables that should be used. You can define framework for your project for compilation and debugging for specific profiles.

10. What is appsettings.json file?
Ans: ASP.NET stores application configuration settings in Web.config. ASP.NET Core uses AppSettings.json to store custom application setting, DB connection strings,Logging etc..

Below is a sample of Appsettings.json:



11. What is bundleconfig.json file?
You can define the configuration for bundling and minification for the project.


12. What is project.json file?
Asp.net Core uses Project.JSON file for storing all project level configuration settings.The Project.json file stores configuration information in JSON format.
{
  "dependencies": {
    "Microsoft.NETCore.App": {
      "version": "1.0.0",
      "type": "platform"
    },
    "Microsoft.ApplicationInsights.AspNetCore": "1.0.0",
    "Microsoft.AspNetCore.Diagnostics": "1.0.0",
    "Microsoft.AspNetCore.Mvc": "1.0.0",
    "Microsoft.AspNetCore.Razor.Tools": {
      "version": "1.0.0-preview2-final",
      "type": "build"
    },
    "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
    "Microsoft.AspNetCore.Server.Kestrel": "1.0.0",
    "Microsoft.AspNetCore.StaticFiles": "1.0.0",
    "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0",
    "Microsoft.Extensions.Configuration.Json": "1.0.0",
    "Microsoft.Extensions.Logging": "1.0.0",
    "Microsoft.Extensions.Logging.Console": "1.0.0",
    "Microsoft.Extensions.Logging.Debug": "1.0.0",
    "Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0",
    "Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0"
  },
  "tools": {
    "BundlerMinifier.Core": "2.0.238",
    "Microsoft.AspNetCore.Razor.Tools": "1.0.0-preview2-final",
    "Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final"
  },
  "frameworks": {
    "netcoreapp1.0": {
      "imports": [
        "dotnet5.6",
        "portable-net45+win8"
      ]
    }
  },
  "buildOptions": {
    "emitEntryPoint": true,
    "preserveCompilationContext": true
  },
  "runtimeOptions": {
    "configProperties": {
      "System.GC.Server": true
    }
  },
  "publishOptions": {
    "include": [
      "wwwroot",
      "Views",
      "Areas/**/Views",
      "appsettings.json",
      "web.config"
    ]
  },
  "scripts": {
    "prepublish": [ "bower install", "dotnet bundle" ],
    "postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
  }
} 

13. What is Startup.cs file in ASP.NET Core?
Ans: ASP.NET Core application must include Startup class. It is like Global.asax in the traditional .NET application. As the name suggests, it is executed first when the application starts.
The startup class can be configured using UseStartup<T>() method at the time of configuring the host in the Main() method of Program class as shown below.
The name "Startup" is by ASP.NET Core convention. However, we can give any name to the Startup class, just specify it as the generic parameter in the UseStartup<T>() method. For example, to name the Startup class as MyStartup, specify it as .UseStartup<MyStartup>().

Open Startup class in Visual Studio by clicking on the Startup.cs in the solution explorer. The following is a default Startup class in ASP.NET Core 2.x.


14.What ConfigureServices() method does in Startup.cs?
Ans: 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.

1

2

3

4

5

6

7

8

9

public void ConfigureServices(IServiceCollection services) 

{

    services.Configure<AppSettings>(Configuration.GetSubKey("AppSettings"));

    

    services.AddEntityFramework()

            .AddSqlServer()

            .AddDbContext<SchoolContext>();

 

    // Add MVC services to the services container.

    services.AddMvc();

}


15.What Configure() method does in Startup.cs?
Ans: 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.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)

{

    loggerFactory.AddConsole(Configuration.GetSection("Logging"));

    loggerFactory.AddDebug();

  

    app.UseApplicationInsightsRequestTelemetry();

  

    if (env.IsDevelopment())

    {

        app.UseBrowserLink();

        app.UseDeveloperExceptionPage();

    }

    else

    {

        app.UseExceptionHandler("/Home/Error");

  

        // For more details on creating database during deployment see http://go.microsoft.com/fwlink/?LinkID=615859

        try

        {

            using (var serviceScope = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>()

                .CreateScope())

            {

                serviceScope.ServiceProvider.GetService<ApplicationDbContext>()

                        .Database.Migrate();

            }

        }

        catch { }

    }

  

    app.UseIISPlatformHandler(options => options.AuthenticationDescriptions.Clear());

    app.UseStaticFiles();

    app.UseIdentity();

  

    // To configure external authentication please see http://go.microsoft.com/fwlink/?LinkID=532715

  

    app.UseMvc(routes =>

    {

        routes.MapRoute(

            name: "default",

            template: "{controller=Home}/{action=Index}/{id?}");

    });

}


16.What is the difference between services.AddTransient and service.AddScope methods are Asp.Net Core?
Ans: ASP.NET Core out of the box supports dependency injection. These 3 methods allow to register the dependency. However, they offer different lifetime for registered service. Transient objects are created for every request (when requested). This lifetime works best for lightweight, stateless services. Scoped objects are the same within a request, but different across different requests. Singleton objects created the first time they’re requested (or when ConfigureServices is run, an instance is specified with the service registration).

17.What is Kestral?
Ans: 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.
If you expose your application to the Internet, you must use IIS, Nginx, or Apache as a reverse proxy server. A reverse proxy server receives HTTP requests from the Internet and forwards them to Kestrel after some preliminary handling. The most important reason for using a reverse proxy for edge deployments (exposed to traffic from the Internet) is security. Kestrel is relatively new and does not yet have a full complement of defenses against attacks.

18.What is WebListener?
Ans: 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.

19.What is ASP.NET Core Module (ANCM)?
Ans: ASP.NET Core Module (ANCM) lets you run ASP.NET Core applications behind IIS and it works only with Kestrel; it isn’t compatible with WebListener. ANCM is a native IIS module that hooks into the IIS pipeline and redirects traffic to the backend ASP.NET Core application. ASP.NET Core applications run in a process separate from the IIS worker process, ANCM also does process management. ANCM starts the process for the ASP.NET Core application when the first request comes in and restarts it when it crashes. In short, it sits in IIS and routes the request for ASP.NET Core application to Kestral.

20.What is a Host and what’s the importance of Host in ASP.NET Core application?
Ans: ASP.NET Core apps require a host in which to execute. The host is responsible for application startup and lifetime management. Other responsibility of host’s includes ensuring the application’s services and the server are available and properly configured. Don’t confuse yourself with a Server. The host is responsible for starting the app and its management, where the server is responsible for accepting HTTP requests. The host is configured to use a particular server; the server is unaware of its host.
The host is typically created using an instance of a WebHostBuilder, which builds and returns a WebHost instance. The WebHost references the server that will handle requests.

public class Program

    {

        public static void Main(string[] args)

        {

            var host = new WebHostBuilder()

                .UseKestrel()

                .UseContentRoot(Directory.GetCurrentDirectory())

                .UseIISIntegration()

                .UseStartup<Startup>()

                .Build();

 

            host.Run();

        }

    }

In ASP.NET Core 2.0, it is changed. It looks like this:

public class Program

{

    public static void Main(string[] args)

    {

        BuildWebHost(args).Run();

    }

 

    public static IWebHost BuildWebHost(string[] args) =>

        WebHost.CreateDefaultBuilder(args)

            .UseStartup<Startup>()

            .Build();

}

21. What does WebHost. CreateDefaultBuilder() do?

Ans: This method does the following things.

  • Configure the app to use Kestrel as web server.
  • Specify to use the current project directory as root directory for the application.
  • Setup the configuration sub-system to read setting from appsettings.json and appsettings.{env}.json to environment specific configuration.
  • Set Local user secrets storage only for the development environment.
  • Configure environment variables to allow for server-specific settings.
  • Configure command line arguments (if any).
  • Configure logging to read from the Logging section of the appsettings.json file and log to the Console and Debug window.
  • Configure integration with IIS
  • Configure the default service provider.

22.What is the role of IHostingEnvironment interface in ASP.NET Core?
Ans: ASP.NET Core offers an interface named IHostingEnvironment, allows you to programmatically retrieve the current environment so you can have an environment-specific behaviour. By default, ASP.NET Core has 3 environments Development, Staging, and Production. Previously, the developers have to build the application differently for each environment (Staging, UAT, Production) due to dependency on config file sections and the preprocessor directive applicable at compile time. ASP.NET Core takes a different approach and uses IHostingEnvironment to retrieve the current environment. You can also define a custom environment like QA or UAT.

23.What is the use of UseIISIntegration?
Ans: This tells ASP.NET that IIS will be working as a reverse proxy in front of Kestrel. As if you expose your application to the Internet, you must use IIS, Nginx, or Apache as a reverse proxy server. When you wish to deploy your ASP.NET Core application on windows, you need to tell ASP.NET Core Host to use IIS integration.
UseKestrel and UseIISIntegration must be used in conjunction as UseKestrel creates the web server and hosts the code. UseIISIntegration specifies IIS as the reverse proxy server.

1

2

3

4

5

6

var host = new WebHostBuilder()

         .UseKestrel()

         .UseContentRoot(Directory.GetCurrentDirectory())

         .UseIISIntegration()

         .UseStartup<Startup>()

         .Build();

Note that code making call to UseIISIntegration() does not affect code portability.

24.What are the different ways for bundling and minification in ASP.NET Core?

Ans: There are different ways for doing bundling and minification in ASP.NET Core.

  • Gulp – was the default choice for ASP.NET Core till beta versions. Later it was removed due to performance and speed issue and replaced with BundlerMinifier. Read this post to find out the reasons of this decision.
  • BundlerMinifier – is a Visual Studio extension and it’s default choice now. You should see bundleconfig.json file in your default template. Read this post to know more about bundling and minification in ASP.NET Core.
  • ASP.NET Core Web Optimizer – ASP.NET Core middleware for bundling and minification of CSS and JavaScript files at runtime.
  • Grunt – can also be used with ASP.NET Core.

25.What is wwwroot folder in ASP.NET Core?

Ans: In ASP.NET Core, all the static resources, such as CSS, JavaScript, and image files are kept under the wwwroot folder (default). You can also change the name of this folder.

26.What is “Razor pages” in ASP.NET Core?

Ans: “Razor Pages” is a new feature of ASP.NET Core and it was released with ASP.NET Core 2.0 release. Razor pages are simple pages or views without controllers and introduced with the intent of creating page focused scenarios where there is no real logic is involved. You will find razor pages similar to ASP.NET Web Forms. They work on the convention and need to be placed in Pages folder and the extension is  .cshtml. Razor pages uses handler methods to deal with incoming HTTP request. Read this post to find out more about handler methods.


27.What is tag helper in ASP.NET Core?

Ans: Tag Helpers enable server-side code to participate in creating and rendering HTML elements in Razor files. For example, the built-in ImageTagHelper can append a version number to the image name. There are many built-in tag helpers like From, Image, Anchor and many others.

28.What is the difference between IApplicationBuilder.Use() and IApplicationBuilder.Run() method?

IApplicationBuilder.Use() adds a middleware to the application's request pipeline. The next parameter tells the runtime which middleware to call next.
IApplicationBuilder.Run() adds a terminal middleware to the application's request pipeline.

27.What is the use of UseDeveloperExceptionPage()?

The UseDeveloperExceptionPage extension method adds middleware into the request pipeline which displays developer friendly exception detail page. This helps developers in tracing errors that occur during development phase.





















 


Post a Comment

0 Comments