Files
ScadaBridge/src/ScadaLink.ExternalSystemGateway/ServiceCollectionExtensions.cs
T

46 lines
1.9 KiB
C#

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using ScadaLink.Commons.Interfaces.Services;
namespace ScadaLink.ExternalSystemGateway;
public static class ServiceCollectionExtensions
{
public static IServiceCollection AddExternalSystemGateway(this IServiceCollection services)
{
services.AddOptions<ExternalSystemGatewayOptions>()
.BindConfiguration("ScadaLink:ExternalSystemGateway");
services.AddHttpClient();
// ExternalSystemGateway-013: wire MaxConcurrentConnectionsPerSystem into the
// primary handler of every per-system named client ("ExternalSystem_{name}"),
// so the option an operator configures actually bounds concurrent connections
// instead of being silently ignored. ConfigureHttpClientDefaults applies to
// the dynamically-named clients created by ExternalSystemClient.
services.ConfigureHttpClientDefaults(builder =>
builder.ConfigurePrimaryHttpMessageHandler(sp =>
{
var options = sp.GetRequiredService<IOptions<ExternalSystemGatewayOptions>>().Value;
return new SocketsHttpHandler
{
MaxConnectionsPerServer = options.MaxConcurrentConnectionsPerSystem,
};
}));
services.AddScoped<ExternalSystemClient>();
services.AddScoped<IExternalSystemClient>(sp => sp.GetRequiredService<ExternalSystemClient>());
services.AddScoped<DatabaseGateway>();
services.AddScoped<IDatabaseGateway>(sp => sp.GetRequiredService<DatabaseGateway>());
return services;
}
public static IServiceCollection AddExternalSystemGatewayActors(this IServiceCollection services)
{
// WP-10: Actor registration happens in AkkaHostedService.
// Script Execution Actors run on dedicated blocking I/O dispatcher.
return services;
}
}