What is the difference between Ho...
What is the difference between Ho...
Definition:
Hosted services in .NET Core are background services that run within the context of a .NET Core application. They are typically used to perform background tasks that do not require user interaction and can be managed by the application's host.
Key Characteristics:
IHostedService
interface. This interface requires the implementation of two methods: StartAsync
and StopAsync
, which handle the initialization and cleanup of the service, respectively.Example:
public class MyHostedService : IHostedService
{
private readonly ILogger<MyHostedService> _logger;
public MyHostedService(ILogger<MyHostedService> logger)
{
_logger = logger;
}
public Task StartAsync(CancellationToken cancellationToken)
{
_logger.LogInformation("Hosted Service is starting.");
// Perform start-up tasks here
return Task.CompletedTask;
}
public Task StopAsync(CancellationToken cancellationToken)
{
_logger.LogInformation("Hosted Service is stopping.");
// Perform clean-up tasks here
return Task.CompletedTask;
}
}
Definition:
Windows Services are long-running executable applications that run in their own Windows sessions. They can be automatically started when the computer boots, can run without user interaction, and can be managed using the Windows Service Control Manager.
Key Characteristics:
senior
Gợi ý câu hỏi phỏng vấn
Chưa có bình luận nào