diff --git a/Data_Coupler/BackgrounServices/BackgroundServices.cs b/Data_Coupler/BackgrounServices/BackgroundServices.cs new file mode 100644 index 0000000..f44aeb6 --- /dev/null +++ b/Data_Coupler/BackgrounServices/BackgroundServices.cs @@ -0,0 +1,31 @@ +using System; + +namespace Data_Coupler.BackgrounServices; + +public class BackgroundServices : BackgroundService +{ + protected override async Task ExecuteAsync(CancellationToken stoppingToken) + { + while (!stoppingToken.IsCancellationRequested) + { + try + { + // Qui puoi inserire il codice che vuoi eseguire in background + // Ad esempio, puoi chiamare un metodo per eseguire operazioni periodiche + + // Simula un'attività di lunga durata + await Task.Delay(TimeSpan.FromMinutes(1), stoppingToken); + } + catch (OperationCanceledException) + { + // Gestisci l'eccezione se il task viene cancellato + break; + } + catch (Exception ex) + { + // Gestisci altre eccezioni + Console.WriteLine($"Errore: {ex.Message}"); + } + } + } +} diff --git a/Data_Coupler/Data_Coupler.csproj b/Data_Coupler/Data_Coupler.csproj index 157525b..45dfb68 100644 --- a/Data_Coupler/Data_Coupler.csproj +++ b/Data_Coupler/Data_Coupler.csproj @@ -14,6 +14,7 @@ + diff --git a/Data_Coupler/Program.cs b/Data_Coupler/Program.cs index 8fdc433..efb4e83 100644 --- a/Data_Coupler/Program.cs +++ b/Data_Coupler/Program.cs @@ -10,6 +10,7 @@ using CredentialManager; using Data_Coupler.Services; using System; using System.Threading.Tasks; +using Data_Coupler.BackgrounServices; // Registra il provider di encoding per ExcelDataReader (necessario per file .xls) System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance); @@ -19,6 +20,8 @@ var builder = WebApplication.CreateBuilder(args); // Add services to the container. builder.Services.AddRazorPages(); builder.Services.AddServerSideBlazor(); +builder.Services.AddWindowsService(); +builder.Services.AddHostedService(); // Add CredentialManager services var contentRoot = builder.Environment.ContentRootPath; @@ -32,6 +35,7 @@ builder.Services.AddHttpClient(); // Register Data Connection Factory builder.Services.AddScoped(); +builder.WebHost.UseUrls("http://*:7550"); var app = builder.Build();