This commit is contained in:
2025-04-15 14:19:26 +02:00
parent a702cef0cf
commit 1f30be648d
10 changed files with 222 additions and 0 deletions

View File

@@ -0,0 +1,36 @@
using Microsoft.Extensions.Hosting;
using Serilog;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TemplateNETService
{
internal class BaseWorker : BackgroundService
{
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
OnStart();
while (!stoppingToken.IsCancellationRequested)
{
Log.Debug("Worker läuft...");
await Task.Delay(1000, stoppingToken);
}
OnStop();
}
public void OnStart()
{
Log.Debug("Der Service wurde gestartet.");
}
public void OnStop()
{
Log.Information("Der Service wird gestoppt.");
}
}
}

View File

@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TemplateNETService.Models;
namespace TemplateNETService.Business
{
public class Config
{
public static General general { get; set; }
}
}

View File

@@ -0,0 +1,8 @@
SET InstallUtilPath=C:\Windows\Microsoft.NET\Framework64\v4.0.30319
SET ServiceExePath=C:\CompControl\CompControl.ProductControl\ProductControl\ProductControl.Service\bin\Debug\net8.0
SET ServiceName=TestService
SET ExeFileName=CompControl.ProductControl.Service.exe
CD /d %InstallUtilPath%
sc create %ServiceName% binPath= %ServiceExePath%\%ExeFileName%
PAUSE

View File

@@ -0,0 +1,4 @@
SET ServiceName=TestService
sc stop %ServiceName%
sc delete %ServiceName%
PAUSE

View File

@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TemplateNETService.Models
{
public class General
{
public int ThisPort { get; set; }
}
}

View File

@@ -0,0 +1,55 @@
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Serilog;
using Log = Serilog.Log;
using Serilog.Events;
using Microsoft.AspNetCore.Hosting;
using TemplateNETService.Models;
namespace TemplateNETService.Business
{
public class Program
{
public static void Main(string[] args)
{
Directory.CreateDirectory("_LOG");
Log.Logger = new LoggerConfiguration()
.WriteTo.Console()
.MinimumLevel.Debug()
.MinimumLevel.Override("Microsoft", LogEventLevel.Error) // Für Microsoft-Komponenten nur Warning+
.WriteTo.File(Path.Combine(AppContext.BaseDirectory, "_LOG/TEST.log"), rollingInterval: RollingInterval.Day)
.Filter.ByExcluding(logEvent => logEvent.Level == LogEventLevel.Information)
.CreateLogger();
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseWindowsService()
.ConfigureAppConfiguration((context, config) =>
{
config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
})
.UseSerilog()
.ConfigureWebHostDefaults(webBuilder =>
{
var config = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.Build();
Config.general = config.GetSection("General").Get<General>();
webBuilder.UseStartup<Startup>();
webBuilder.UseKestrel(options =>
{
options.ListenAnyIP(Config.general.ThisPort);
})
.ConfigureServices(services =>
{
services.AddHostedService<BaseWorker>();
});
});
}
}

View File

@@ -0,0 +1,28 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
namespace TemplateNETService
{
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
app.UseDeveloperExceptionPage();
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
}

View File

@@ -0,0 +1,33 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore" Version="2.3.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.4" />
<PackageReference Include="Microsoft.Extensions.Hosting.WindowsServices" Version="9.0.4" />
<PackageReference Include="Serilog" Version="4.2.0" />
<PackageReference Include="Serilog.AspNetCore" Version="9.0.0" />
<PackageReference Include="Serilog.Extensions.Hosting" Version="9.0.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="6.0.0" />
<PackageReference Include="Serilog.Sinks.File" Version="6.0.0" />
</ItemGroup>
<ItemGroup>
<None Update="appsettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Install\InstallService.bat">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Install\UnInstallService.bat">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>

View File

@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.9.34728.123
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TemplateNETService", "TemplateNETService.csproj", "{BBF90686-16D8-4FE0-8867-6065F0AFD2AC}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{BBF90686-16D8-4FE0-8867-6065F0AFD2AC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BBF90686-16D8-4FE0-8867-6065F0AFD2AC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BBF90686-16D8-4FE0-8867-6065F0AFD2AC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BBF90686-16D8-4FE0-8867-6065F0AFD2AC}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {3A39DCF0-8EA1-4982-81F7-B1A96E5A6F49}
EndGlobalSection
EndGlobal

View File

@@ -0,0 +1,5 @@
{
"General": {
"ThisPort": 7654
}
}