Compare commits
9 Commits
117d6fea10
...
feature/re
| Author | SHA1 | Date | |
|---|---|---|---|
| 0b5a963c11 | |||
| 67627fe227 | |||
| a3c2dfc3eb | |||
| 2762a7c0a4 | |||
| f69a2865a2 | |||
| 9c162e1752 | |||
| e834ba073f | |||
| 8a1eb23f05 | |||
| 64d78a7cd5 |
@@ -1,15 +1,8 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using TemplateNETService.Models;
|
||||
using TemplateNETService.Models;
|
||||
|
||||
namespace TemplateNETService.Business
|
||||
namespace TemplateNETService.Business;
|
||||
|
||||
public class Config
|
||||
{
|
||||
public class Config
|
||||
{
|
||||
public static General general { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,16 +1,14 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace TemplateNETService.Controllers
|
||||
namespace TemplateNETService.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[Route("api")]
|
||||
public class ServiceController : ControllerBase
|
||||
{
|
||||
[ApiController]
|
||||
[Route("api")]
|
||||
public class ServiceController : ControllerBase
|
||||
{
|
||||
[HttpGet]
|
||||
[Route("ping")]
|
||||
[HttpGet("ping")]
|
||||
public IActionResult GetStatus()
|
||||
{
|
||||
return Ok();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,13 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
namespace TemplateNETService.Models;
|
||||
|
||||
namespace TemplateNETService.Models
|
||||
public class General
|
||||
{
|
||||
public class General
|
||||
{
|
||||
public int ThisPort { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -1,55 +1,101 @@
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using System.Reflection;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json.Serialization;
|
||||
using Serilog;
|
||||
using Serilog.Events;
|
||||
using TemplateNETService.Business;
|
||||
using TemplateNETService.Models;
|
||||
using Log = Serilog.Log;
|
||||
|
||||
namespace TemplateNETService;
|
||||
|
||||
public class Program
|
||||
{
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
RunWebApp(args);
|
||||
}
|
||||
|
||||
private static void RunWebApp(string[] args)
|
||||
{
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
ConfigureConfiguration(builder);
|
||||
ConfigureLogging(builder);
|
||||
ConfigureServices(builder.Services);
|
||||
|
||||
var app = builder.Build();
|
||||
ConfigureApp(app);
|
||||
app.Run();
|
||||
}
|
||||
|
||||
private static void ConfigureConfiguration(WebApplicationBuilder builder)
|
||||
{
|
||||
builder.Configuration
|
||||
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
|
||||
.AddEnvironmentVariables();
|
||||
var generalConfig = builder.Configuration.GetSection("General").Get<General>();
|
||||
if (generalConfig == null)
|
||||
{
|
||||
throw new InvalidOperationException("Configuration section 'General' is missing or invalid in appsettings.json");
|
||||
}
|
||||
Config.general = generalConfig;
|
||||
}
|
||||
|
||||
private static void ConfigureLogging(WebApplicationBuilder builder)
|
||||
{
|
||||
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)
|
||||
.MinimumLevel.Override("Microsoft", LogEventLevel.Error)
|
||||
.WriteTo.File(
|
||||
Path.Combine(AppContext.BaseDirectory, "_LOG/TemplateNETService.log"),
|
||||
rollingInterval: RollingInterval.Day,
|
||||
fileSizeLimitBytes: 100000000,
|
||||
rollOnFileSizeLimit: true,
|
||||
retainedFileTimeLimit: TimeSpan.FromDays(14)
|
||||
)
|
||||
.WriteTo.File(
|
||||
Path.Combine(AppContext.BaseDirectory, "_LOG/TemplateNETService.error.log"),
|
||||
rollingInterval: RollingInterval.Day,
|
||||
restrictedToMinimumLevel: LogEventLevel.Error
|
||||
)
|
||||
.CreateLogger();
|
||||
|
||||
CreateHostBuilder(args).Build().Run();
|
||||
builder.Logging.ClearProviders();
|
||||
builder.Logging.AddSerilog(Log.Logger);
|
||||
builder.Host.UseSerilog();
|
||||
}
|
||||
|
||||
public static IHostBuilder CreateHostBuilder(string[] args) =>
|
||||
Host.CreateDefaultBuilder(args)
|
||||
.UseWindowsService()
|
||||
.ConfigureAppConfiguration((context, config) =>
|
||||
private static void ConfigureServices(IServiceCollection services)
|
||||
{
|
||||
config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
|
||||
})
|
||||
.UseSerilog()
|
||||
.ConfigureWebHostDefaults(webBuilder =>
|
||||
{
|
||||
var config = new ConfigurationBuilder()
|
||||
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
|
||||
.Build();
|
||||
services.AddControllers().AddNewtonsoftJson(options => { options.SerializerSettings.ContractResolver = new DefaultContractResolver(); });
|
||||
|
||||
Config.general = config.GetSection("General").Get<General>();
|
||||
services.AddEndpointsApiExplorer();
|
||||
services.AddSwaggerGen(c =>
|
||||
{
|
||||
var xmlFilename = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
|
||||
c.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, xmlFilename));
|
||||
});
|
||||
|
||||
webBuilder.UseStartup<Startup>();
|
||||
webBuilder.UseKestrel(options =>
|
||||
{
|
||||
options.ListenAnyIP(Config.general.ThisPort);
|
||||
})
|
||||
.ConfigureServices(services =>
|
||||
{
|
||||
services.AddWindowsService();
|
||||
services.AddHostedService<BaseWorker>();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
private static void ConfigureApp(WebApplication app)
|
||||
{
|
||||
app.UseSwagger();
|
||||
app.UseSwaggerUI();
|
||||
|
||||
app.UseHttpsRedirection();
|
||||
app.UseAuthorization();
|
||||
app.MapControllers();
|
||||
|
||||
app.Urls.Add($"http://*:{Config.general.ThisPort}");
|
||||
}
|
||||
}
|
||||
@@ -1,27 +0,0 @@
|
||||
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();
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
@@ -5,17 +5,21 @@
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
||||
</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" />
|
||||
<PackageReference Include="Microsoft.AspNetCore" Version="2.3.9" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="8.0.23" />
|
||||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="10.0.2" />
|
||||
<PackageReference Include="Microsoft.Extensions.Hosting.WindowsServices" Version="10.0.2" />
|
||||
<PackageReference Include="Serilog" Version="4.3.0" />
|
||||
<PackageReference Include="Serilog.AspNetCore" Version="10.0.0" />
|
||||
<PackageReference Include="Serilog.Extensions.Hosting" Version="10.0.0" />
|
||||
<PackageReference Include="Serilog.Sinks.Console" Version="6.1.1" />
|
||||
<PackageReference Include="Serilog.Sinks.File" Version="7.0.0" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="10.1.0" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="10.1.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
Reference in New Issue
Block a user