using System; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; namespace ZB.MOM.WW.LmxProxy.Client { /// /// Extension methods for registering LmxProxyClient with dependency injection /// public static class ServiceCollectionExtensions { /// /// Adds LmxProxyClient services to the service collection /// /// The service collection /// Application configuration /// The service collection for chaining public static IServiceCollection AddLmxProxyClient(this IServiceCollection services, IConfiguration configuration) { return services.AddLmxProxyClient(configuration, "LmxProxy"); } /// /// Adds LmxProxyClient services to the service collection with a specific configuration section /// /// The service collection /// Application configuration /// Name of the configuration section /// The service collection for chaining public static IServiceCollection AddLmxProxyClient( this IServiceCollection services, IConfiguration configuration, string configurationSection) { services.AddSingleton(); // Register a singleton client with default configuration services.AddSingleton(provider => { ILmxProxyClientFactory factory = provider.GetRequiredService(); return factory.CreateClient(configurationSection); }); return services; } /// /// Adds LmxProxyClient services to the service collection with custom configuration /// /// The service collection /// Action to configure the client builder /// The service collection for chaining public static IServiceCollection AddLmxProxyClient( this IServiceCollection services, Action configureClient) { services.AddSingleton(); // Register a singleton client with custom configuration services.AddSingleton(provider => { ILmxProxyClientFactory factory = provider.GetRequiredService(); return factory.CreateClient(configureClient); }); return services; } /// /// Adds LmxProxyClient services to the service collection with scoped lifetime /// /// The service collection /// Application configuration /// The service collection for chaining public static IServiceCollection AddScopedLmxProxyClient( this IServiceCollection services, IConfiguration configuration) { services.AddSingleton(); // Register a scoped client services.AddScoped(provider => { ILmxProxyClientFactory factory = provider.GetRequiredService(); return factory.CreateClient(); }); return services; } /// /// Adds named LmxProxyClient services to the service collection /// /// The service collection /// Name for the client /// Action to configure the client builder /// The service collection for chaining public static IServiceCollection AddNamedLmxProxyClient( this IServiceCollection services, string name, Action configureClient) { services.AddSingleton(); // Register a keyed singleton services.AddKeyedSingleton(name, (provider, _) => { ILmxProxyClientFactory factory = provider.GetRequiredService(); return factory.CreateClient(configureClient); }); return services; } } /// /// Configuration options for LmxProxyClient /// public class LmxProxyClientOptions { /// /// Gets or sets the host address /// public string Host { get; set; } = "localhost"; /// /// Gets or sets the port number /// public int Port { get; set; } = 5050; /// /// Gets or sets the API key /// public string? ApiKey { get; set; } /// /// Gets or sets the timeout duration /// public TimeSpan Timeout { get; set; } = TimeSpan.FromSeconds(30); /// /// Gets or sets whether to use SSL /// public bool UseSsl { get; set; } /// /// Gets or sets the certificate path for SSL /// public string? CertificatePath { get; set; } /// /// Gets or sets whether to enable metrics /// public bool EnableMetrics { get; set; } /// /// Gets or sets the correlation ID header name /// public string? CorrelationIdHeader { get; set; } /// /// Gets or sets the retry configuration /// public RetryOptions? Retry { get; set; } } /// /// Retry configuration options /// public class RetryOptions { /// /// Gets or sets the maximum number of retry attempts /// public int MaxAttempts { get; set; } = 3; /// /// Gets or sets the delay between retries /// public TimeSpan Delay { get; set; } = TimeSpan.FromSeconds(1); } }