Câu hỏi phỏng vấn .NET Core
Câu hỏi

What is Generic Host in .NET Core?

Câu trả lời

In .NET Core, the Generic Host is a hosting mechanism provided by the Microsoft.Extensions.Hosting namespace. It is designed to simplify the process of building and configuring applications, particularly for services like console applications, Windows services, and ASP.NET Core applications. The Generic Host is built on top of the ASP.NET Core hosting infrastructure and provides a consistent way to configure and run applications regardless of the application type[1][2].

Key Features of the Generic Host

  1. Encapsulation of Common Application Concerns:

    • Configuration: The Generic Host loads configuration from various sources such as environment variables, command-line arguments, and configuration files like appsettings.json[2][5].
    • Dependency Injection (DI): It integrates with the DI container, allowing services to be registered and resolved throughout the application[1][2].
    • Logging: It includes built-in support for logging, which can be configured to use various logging providers[5].
    • Application Lifetime Management: It manages the application’s startup and graceful shutdown processes[1][2].
  2. Flexibility and Consistency:

    • The Generic Host can be used with different types of .NET applications, including console apps, Windows services, and web apps, providing a unified hosting model[1][3].
    • It promotes best practices such as dependency injection, configuration, and logging, making it easier to develop robust and maintainable applications[1].

Setting Up a Generic Host

The host is typically configured, built, and run by code in the Program.cs file. Here is an example of how to create and run a Generic Host:

public class Program
{
    public static void Main(string[] args)
    {
        CreateHostBuilder(args).Build().Run();
    }

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(arg...
junior

junior

Gợi ý câu hỏi phỏng vấn

middle

What's is BCL?

senior

What is the difference between CIL and MSIL (IL)?

junior

What is IoC (DI) Container?

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào