feat(options): eager startup validation for edge/management options (InboundAPI, ESG, Notification, ManagementService) (arch-review 08 §1.5)
Claude-Session: https://claude.ai/code/session_01MtdgwpEeCUn6cUA5f1LMPj
This commit is contained in:
+33
@@ -0,0 +1,33 @@
|
||||
using ZB.MOM.WW.Configuration;
|
||||
|
||||
namespace ZB.MOM.WW.ScadaBridge.ExternalSystemGateway;
|
||||
|
||||
/// <summary>
|
||||
/// Validates <see cref="ExternalSystemGatewayOptions"/> at startup.
|
||||
/// <see cref="ExternalSystemGatewayOptions.DefaultHttpTimeout"/> bounds each outbound call,
|
||||
/// <see cref="ExternalSystemGatewayOptions.MaxConcurrentConnectionsPerSystem"/> is fed into
|
||||
/// <c>SocketsHttpHandler.MaxConnectionsPerServer</c> (a non-positive value throws in the handler
|
||||
/// constructor), and <see cref="ExternalSystemGatewayOptions.MaxResponseBodyBytes"/> caps buffered
|
||||
/// response bodies. Registered with <c>ValidateOnStart()</c> so a bad
|
||||
/// <c>ScadaBridge:ExternalSystemGateway</c> section fails fast at boot with a clear, key-naming
|
||||
/// message rather than crashing the HTTP client factory or silently disabling the gateway.
|
||||
/// </summary>
|
||||
public sealed class ExternalSystemGatewayOptionsValidator : OptionsValidatorBase<ExternalSystemGatewayOptions>
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void Validate(ValidationBuilder builder, ExternalSystemGatewayOptions options)
|
||||
{
|
||||
builder.RequireThat(options.DefaultHttpTimeout > TimeSpan.Zero,
|
||||
$"ScadaBridge:ExternalSystemGateway:DefaultHttpTimeout must be a positive duration " +
|
||||
$"(was {options.DefaultHttpTimeout}); it bounds each outbound external-system HTTP call.");
|
||||
|
||||
builder.RequireThat(options.MaxConcurrentConnectionsPerSystem > 0,
|
||||
$"ScadaBridge:ExternalSystemGateway:MaxConcurrentConnectionsPerSystem must be greater than zero " +
|
||||
$"(was {options.MaxConcurrentConnectionsPerSystem}); it is fed into " +
|
||||
"SocketsHttpHandler.MaxConnectionsPerServer, which rejects non-positive values.");
|
||||
|
||||
builder.RequireThat(options.MaxResponseBodyBytes > 0,
|
||||
$"ScadaBridge:ExternalSystemGateway:MaxResponseBodyBytes must be greater than zero " +
|
||||
$"(was {options.MaxResponseBodyBytes}); it caps the outbound response body buffered into memory.");
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||
using Microsoft.Extensions.Http;
|
||||
using Microsoft.Extensions.Options;
|
||||
using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Services;
|
||||
@@ -21,7 +22,11 @@ public static class ServiceCollectionExtensions
|
||||
public static IServiceCollection AddExternalSystemGateway(this IServiceCollection services)
|
||||
{
|
||||
services.AddOptions<ExternalSystemGatewayOptions>()
|
||||
.BindConfiguration("ScadaBridge:ExternalSystemGateway");
|
||||
.BindConfiguration("ScadaBridge:ExternalSystemGateway")
|
||||
.ValidateOnStart();
|
||||
services.TryAddEnumerable(
|
||||
ServiceDescriptor.Singleton<IValidateOptions<ExternalSystemGatewayOptions>,
|
||||
ExternalSystemGatewayOptionsValidator>());
|
||||
|
||||
services.AddHttpClient();
|
||||
|
||||
|
||||
+1
@@ -13,6 +13,7 @@
|
||||
<PackageReference Include="Microsoft.Extensions.Http" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" />
|
||||
<PackageReference Include="Microsoft.Extensions.Options" />
|
||||
<PackageReference Include="ZB.MOM.WW.Configuration" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
Reference in New Issue
Block a user