38 lines
1.1 KiB
C#
38 lines
1.1 KiB
C#
using Microsoft.Extensions.Hosting;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace Lab505.Sim;
|
|
|
|
public class OpcUaHostedService : IHostedService
|
|
{
|
|
private readonly OpcUaSimulationServer _server;
|
|
private readonly ILogger<OpcUaHostedService> _logger;
|
|
|
|
public OpcUaHostedService(
|
|
OpcUaSimulationServer server,
|
|
ILogger<OpcUaHostedService> logger)
|
|
{
|
|
_server = server;
|
|
_logger = logger;
|
|
}
|
|
|
|
public async Task StartAsync(CancellationToken cancellationToken)
|
|
{
|
|
_logger.LogInformation("Starting OPC UA Simulation Server on port 4840");
|
|
try
|
|
{
|
|
await _server.StartAsync();
|
|
_logger.LogInformation("OPC UA Simulation Server started on port 4840");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogWarning("OPC UA Simulation Server failed to start (port 4840 may already be in use): {Message}", ex.Message);
|
|
}
|
|
}
|
|
|
|
public async Task StopAsync(CancellationToken cancellationToken)
|
|
{
|
|
_logger.LogInformation("Stopping OPC UA Simulation Server");
|
|
await _server.StopAsync();
|
|
}
|
|
} |