Zzdats.Lgp.Common.Web
10.0.10
Prefix Reserved
dotnet add package Zzdats.Lgp.Common.Web --version 10.0.10
NuGet\Install-Package Zzdats.Lgp.Common.Web -Version 10.0.10
<PackageReference Include="Zzdats.Lgp.Common.Web" Version="10.0.10" />
<PackageVersion Include="Zzdats.Lgp.Common.Web" Version="10.0.10" />
<PackageReference Include="Zzdats.Lgp.Common.Web" />
paket add Zzdats.Lgp.Common.Web --version 10.0.10
#r "nuget: Zzdats.Lgp.Common.Web, 10.0.10"
#:package Zzdats.Lgp.Common.Web@10.0.10
#addin nuget:?package=Zzdats.Lgp.Common.Web&version=10.0.10
#tool nuget:?package=Zzdats.Lgp.Common.Web&version=10.0.10
Common-Web
Zzdats.Lgp.Common.Web satur kopējo infrastruktūru web projektiem (API, MVC): logošanu, health check,
Prometheus metrikas, APM un autentifikācijas apstrādātājus.
Bibliotēka ir daļa no LGP kopīgo komponenšu kopas (Zzdats.Lgp.*).
Prasības
- .NET 10
Uzstādīšana
dotnet add package Zzdats.Lgp.Common.Web
Izmantošana
Program.cs:
var builder = WebApplication.CreateBuilder(args);
builder.Host.UseLgpCore();
builder.WebHost.UseLgpCore();
builder.Services.AddLgpCore(builder.Configuration.GetSection("Lgp"));
var app = builder.Build();
app.UseLgpCore();
app.Run();
Projektos, kas vēl izmanto Startup klasi, izsaukumi ir tie paši — ConfigureServices pievieno
services.AddLgpCore(Configuration.GetSection("Lgp")), Configure pievieno app.UseLgpCore(), un host builder
ķēdē pievieno .UseLgpCore() gan IHostBuilder, gan IWebHostBuilder līmenī:
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseLgpCore()
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder
.UseLgpCore()
.UseStartup<Startup>();
});
Iespējas, kas tiek pievienotas pēc noklusējuma
- Health check (
/healthz, atbild tikai par procesu — ārējās atkarības netiek pārbaudītas) - Prometheus metriku eksporta URL
/metrics - Elastic APM (
HttpDiagnosticsSubscriberunAspNetCoreDiagnosticSubscriber) - Log ierakstu veidošana ECS formātā
Konfigurācija
{
"Lgp": {
"Log": {
"File": "./logs/log.log",
"Console": true,
"ConsoleTemplate": "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}",
"ExcludePaths": "/healthz,/metrics"
},
"Infrastructure": {
"Health": {
"Enabled": true
},
"Metrics": {
"Enabled": true,
"Authorization": "Bearer <token>"
}
}
}
}
Papildus Elastic APM mērījumu pievienošana
services.AddLgpCore(Configuration.GetSection("Lgp"), opt =>
{
opt.ElasticApmDiagnosticsSubscribers.Add(new ElasticsearchDiagnosticsSubscriber());
});
Health check
/healthz atbild uz vienu jautājumu: vai process spēj apkalpot HTTP. Tas atgriež {"status":"pass"} ar HTTP 200 un
neveic nevienu datu bāzes, autentifikācijas servisa vai cita tīkla resursa izsaukumu.
Nepievienojiet šim galapunktam ārējo atkarību pārbaudes. Konteinera veselības pārbaude var tikai pārstartēt procesu, un nesasniedzamu atkarību restarts nesalabo — tā vietā tas pārvērš atkarības pārtraukumu restartu ciklā. Ja atkarība neatbild, kļūdu saņem tie pieprasījumi, kuriem tā bija vajadzīga; pieejamību mēra monitorings.
Prometheus eksporteris
Lai ieslēgtu metriku eksportēšanu uz /metrics, Lgp:Infrastructure:Metrics:Enabled jābūt true.
Lai aizsargātu šo galapunktu, Lgp:Infrastructure:Metrics:Authorization jānorāda, kāds Authorization header tiks
padots pieprasījumā no Prometheus — pretējā gadījumā /metrics atbild ar 401 Unauthorized. Tas pats header
jānorāda arī Prometheus metriku ievācējam (scraper).
Autorizēta izsaukuma piemērs:
GET /metrics HTTP/1.1
Host: localhost:5000
Authorization: Bearer <token>
Atbilde satur ASP.NET Core pieprasījumu metrikas un .NET izpildlaika skaitītājus, piemēram:
# HELP http_requests_received_total Provides the count of HTTP requests that have been processed by the ASP.NET Core pipeline.
# TYPE http_requests_received_total counter
http_requests_received_total{code="200",method="GET",controller="Example",action="Get",endpoint="api/example"} 1
# HELP http_request_duration_seconds The duration of HTTP requests processed by an ASP.NET Core application.
# TYPE http_request_duration_seconds histogram
http_request_duration_seconds_sum{code="200",method="GET",controller="Example",action="Get",endpoint="api/example"} 0.0123
http_request_duration_seconds_count{code="200",method="GET",controller="Example",action="Get",endpoint="api/example"} 1
# HELP dotnet_gc_heap_size_bytes The current size of all heaps (only updated after a garbage collection)
# TYPE dotnet_gc_heap_size_bytes gauge
dotnet_gc_heap_size_bytes{gc_generation="0"} 8388600
Pilns skaitītāju saraksts ir prometheus-net un prometheus-net.DotNetRuntime dokumentācijā.
IDAuth adaptera autentifikācija
Bibliotēka satur IDAuth autentifikācijas apstrādātāju. Apstrādātājs noraida talonu, ja IDAuth sesija nav aktīva
(active: false), un izlaiž cauri JWT talonus (talonus, kas satur punktu), lai tos apstrādātu JWT shēma.
Apstrādātāja adresi padod options.AuthUrl — bibliotēka pati nenolasa nevienu konfigurācijas atslēgu, tāpēc ceļš
Lgp:Infrastructure:IdAuth:Url (vides mainīgais LGP__INFRASTRUCTURE__IDAUTH__URL) ir tikai ieteicamā vienošanās:
services.AddAuthentication(opt =>
{
opt.DefaultScheme = IDAuthDefaults.AuthenticationScheme;
})
.AddScheme<IDAuthAuthenticationOptions, IDAuthAuthenticationHandler>(IDAuthDefaults.AuthenticationScheme, options =>
{
options.AuthUrl = Configuration.GetValue<string>("Lgp:Infrastructure:IdAuth:Url");
});
Var reģistrēt arī vairākus apstrādātājus, lai neapstrādātu IDAuth talonu gadījumā notiktu atkāpšanās (fallback) uz JWT taloniem:
services.AddAuthentication(opt =>
{
opt.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(opt =>
{
opt.Authority = Configuration.GetValue<string>("Lgp:Infrastructure:Authority:Url");
opt.RequireHttpsMetadata = !Environment.IsDevelopment();
opt.TokenValidationParameters = new TokenValidationParameters()
{
ValidateAudience = false,
};
})
.AddScheme<IDAuthAuthenticationOptions, IDAuthAuthenticationHandler>(IDAuthDefaults.AuthenticationScheme, options =>
{
options.AuthUrl = Configuration.GetValue<string>("Lgp:Infrastructure:IdAuth:Url");
});
Kontroliera klasē tad norāda abas shēmas:
[ApiController]
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme + "," + IDAuthDefaults.AuthenticationScheme)]
public class ExampleController : ApiControllerBase
Būvēšana
dotnet build
dotnet test
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- Destructurama.Attributed (>= 5.3.0)
- prometheus-net (>= 8.2.1)
- prometheus-net.AspNetCore (>= 8.2.1)
- prometheus-net.DotNetRuntime (>= 4.4.1)
- Serilog.AspNetCore (>= 10.0.0)
- Serilog.Extensions.Hosting (>= 10.0.0)
- Serilog.Sinks.File (>= 7.0.0)
- Zzdats.Lgp.Common.Apm (>= 10.0.5)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Zzdats.Lgp.Common.Web:
| Package | Downloads |
|---|---|
|
Zzdats.Lgp.Common.Mvc
MVC LGP common components and infrastructure integration |
GitHub repositories
This package is not used by any popular GitHub repositories.