initial commit
This commit is contained in:
53
Lab505.Sim/VdaSimulationServer.cs
Normal file
53
Lab505.Sim/VdaSimulationServer.cs
Normal file
@@ -0,0 +1,53 @@
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Lab505.Sim;
|
||||
|
||||
/// <summary>
|
||||
/// Hosted Service để chạy VDA Mock Server ở background
|
||||
/// </summary>
|
||||
public class VdaSimulationServer : IHostedService
|
||||
{
|
||||
private readonly VdaSimulationHostedService _server;
|
||||
private readonly ILogger<VdaSimulationServer> _logger;
|
||||
|
||||
public VdaSimulationServer(
|
||||
VdaSimulationHostedService server,
|
||||
ILogger<VdaSimulationServer> logger)
|
||||
{
|
||||
_server = server ?? throw new ArgumentNullException(nameof(server));
|
||||
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
||||
}
|
||||
|
||||
public Task StartAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
_logger.LogInformation("Starting VDA Simulation Hosted Service");
|
||||
_server.Start();
|
||||
_logger.LogInformation("VDA Simulation Hosted Service started successfully");
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Failed to start VDA Simulation Hosted Service");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public Task StopAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
_logger.LogInformation("Stopping VDA Simulation Hosted Service");
|
||||
_server.Stop();
|
||||
_logger.LogInformation("VDA Simulation Hosted Service stopped successfully");
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error stopping VDA Simulation Hosted Service");
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user