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

What is the difference between IHost...

Câu trả lời

In .NET Core, IHost, IHostBuilder, and IHostedService are key components used for building and managing applications, particularly those that require background processing or complex initialization. Here’s a detailed explanation of the differences between them:

IHost

IHost is an interface that represents the host for an application. It encapsulates all the resources needed to run the application, such as dependency injection (DI), configuration, and logging. The IHost interface provides methods to start and stop the application and manage its lifecycle.

Key Features:

  • Dependency Injection: Manages the lifetime of services.
  • Configuration: Loads configuration settings.
  • Logging: Provides logging capabilities.
  • Application Lifetime: Manages the application’s start and stop events.

Example:

csharp Copy
public static async Task Main(string[] args)
{
    var host = Host.CreateDefaultBuilder(args)
                   .ConfigureServices((context, services) =>
                   {
                       services.AddHostedService<MyBackgroundService>();
                   })
                   .Build();

    await host.RunAsync();
}

In this example, Host.CreateDefaultBuilder sets up the default configuration and services, and Build creates the IHost instance which is then run asynchronously[3][9].

IHostBuilder

IHostBuilder is an interface used to configure and create an IHost instance. It provides methods to set up the host configuration, add services to the DI container, and configure logging. Essentially, IHostBuilder is used during the application startup to build the IHost.

Key Features:

  • Configuration: Allows adding configuration sources.
  • Service Registration: Adds services to the DI container.
  • Logging Configuration: Configures logging providers.
  • Host Options: Configures options related to the host, such as shutdown timeout.

Example:

csharp Copy
public static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
        .ConfigureServices((context, services) =>
        {
            services.AddHostedService<MyBackgroundService>();
        });

In this example, CreateHostBuilder configures the services and returns an IHostBuilder instance which can be used to build the IHost[6][12].

IHostedService

IHostedService is an interface for implementing background tasks in .NET applications. It defines two methods: StartAsync and StopAsync, which are called by the host to start and stop the background service, respectively. This interface is typically used for tasks that need to run in the background, such as processing messages from a queue or performing periodic work.

Key Features:

  • StartAsync: Called when the host starts, used to initialize the background task.
  • StopAsync: Called when the host is shutting down, used to clean up resources.

Example:

csharp Copy
public class MyBackgroundService : IHo...
senior

senior

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

junior

What is CTS?

middle

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

expert

Could you name the difference between .Net Core, Portable, Standard, Compact, UWP, and PCL?

Bình luận

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

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