using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; namespace Lab505.Sim; public class OpcUaHostedService : IHostedService { private readonly OpcUaSimulationServer _server; private readonly ILogger _logger; public OpcUaHostedService( OpcUaSimulationServer server, ILogger 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(); } }