• Register
300 points
8 4 2

What is the role of IHostingEnvironment interface in ASP.NET Core?

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.


 

What is the use of UseIISIntegration?

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.

 

var host = new WebHostBuilder()

         .UseKestrel()

         .UseContentRoot(Directory.GetCurrentDirectory())

         .UseIISIntegration()

         .UseStartup<Startup>()

         .Build();

 

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



 

How can we do automatic model binding in Razor pages?

The Razor pages provide the option to bind property automatically when posted the data using BindProperty attribute. By default, it only binds the properties only with non-GET verbs. we need to set SupportsGet property to true to bind a property on getting request.

 

Example

 public class Test1Model : PageModel

 {

 [BindProperty]

 public string Name { get; set; }

 }


 

How to specify service lifetime for register service that added as a dependency?

ASP.NET Core allows us to specify the lifetime for registered services. The service instance gets disposed of automatically based on a specified lifetime. So, we do not care about the cleaning these dependencies, it will take care by ASP.NET Core framework. There is three type of lifetimes.

 

Singleton

ASP.NET Core will create and share a single instance of the service through the application life. The service can be added as a singleton using AddSingleton method of IServiceCollection. ASP.NET Core creates service instance at the time of registration and subsequence request use this service instance. Here, we do not require to implement Singleton design pattern and single instance maintained by the ASP.NET Core itself.

 

Example

 services.AddSingleton<IHelloWorldService, HelloWorldService>();

Transient

ASP.NET Core will create and share an instance of the service every time to the application when we ask for it. The service can be added as Transient using AddTransient method of IServiceCollection. This lifetime can be used in stateless service. It is a way to add lightweight service.

 

Example

 services.AddTransient<IHelloWorldService, HelloWorldService>();

Scoped

ASP.NET Core will create and share an instance of the service per request to the application. It means that a single instance of service available per request. It will create a new instance in the new request. The service can be added as scoped using an AddScoped method of IServiceCollection. We need to take care while, service registered via Scoped in middleware and inject the service in the Invoke or InvokeAsync methods. If we inject dependency via the constructor, it behaves like singleton object.

 

services.AddScoped<IHelloWorldService, HelloWorldService>();


 

What are the features provided by ASP.NET Core?

Following are the core features that are provided by the ASP.NET Core

 

  • Built-in supports for

  • Dependency Injection

  •  
  • Built-in supports for the logging framework and it can be extensible

  •  
  • Introduced new, fast and cross-platform web server - Kestrel. So, a web application can run without IIS, Apache, and Nginx.

  •  
  • Multiple hosting ways are supported

  •  
  • It supports modularity, so the developer needs to include the module required by the application. However, .NET Core framework is also providing the meta package that includes the libraries

  •  
  • Command-line supports to create, build and run the application

  •  
  • There is no web.config file. We can store the custom configuration into an appsettings.json file

  •  
  • There is no Global.asax file. We can now register and use the services into startup class

  •  
  • It has good support for asynchronous programming

  •  
  • Support WebSocket and SignalR

  •  
  • Provide protection against CSRF (Cross-Site Request Forgery)


 

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.

 

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.


 

What is the difference between .NET Core and .NET Framework?

Dotnet Core and the DotnetFramework have a subset-superset relationship. Dotnet Core is named "Core" as it contains the core features from the Dotnet Framework, for both the runtime and framework libraries.  For example, Dotnet Core and the Dotnet Framework share the GC, the JIT, and types such as String and List.

 

Dotnet Core is an open-source, cross-platform, a high-performance framework for building modern, cloud-based, Internet-connected applications.



 

What are the advantages of ASP.NET Core over ASP.NET?

There are following advantages of ASP.NET Core over ASP.NET :

 

  • It is cross-platform, so it can be run on Windows, Linux, and Mac.

  •  
  • There is no dependency on framework installation because all the required dependencies are ship with our application

  •  
  • ASP.NET Core can handle more request than the ASP.NET

  •  
  • Multiple deployment options available withASP.NET Core


 

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.

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();

}



 

What is the ConfigureServices method of the Startup.cs file?

ConfigureServices method gets call runtime to register services to DI container. After registering the dependent classes, you can use those classes anywhere in the application. The ConfigureServices method includes the IServiceCollection parameter to register services to the DI container.



 

What is the startup class in ASP.NET core?

Startup class is the entry point of the ASP.NET Core application. Every .NET Core application must have this class. This class contains the application configuration rated items. It is not necessary that class name must "Startup", it can be anything, we can configure startup class in Program class.

public class Program

 {

 public static void Main(string[] args)

 {

 CreateWebHostBuilder(args).Build().Run();

 }

 

 public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>

 WebHost.CreateDefaultBuilder(args)

 .UseStartup<TestClass>();

 }


 

300 points
8 4 2