diff --git a/Directory.Packages.props b/Directory.Packages.props index ff650c7..d8c3f6c 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -20,7 +20,7 @@ - + diff --git a/src/Zyga.HikIsapiClient/ExtensionMethods/RegistrationExtensions.cs b/src/Zyga.HikIsapiClient/ExtensionMethods/RegistrationExtensions.cs index b5f36b7..7f69dce 100644 --- a/src/Zyga.HikIsapiClient/ExtensionMethods/RegistrationExtensions.cs +++ b/src/Zyga.HikIsapiClient/ExtensionMethods/RegistrationExtensions.cs @@ -28,11 +28,10 @@ public static IHostBuilder AddHikIsapi(this IHostBuilder hostBuilder, string con .GetRequiredSection(configSection) .Get() ?? throw new Exception($"Section [{configSection}] not found in config") ) - .AddSeriRestClient(Isapi.ServiceKey, (sp, key) => - { - return sp.GetRequiredService() - .GetRestClientOptions(); - }) + .AddSeriRestClient(Isapi.ServiceKey) + .Configure((sp, _, options) => sp.GetRequiredService() + .ApplyRestClientOptions(options)) + .Services .AddTransient((sp) => new Isapi( sp.GetRequiredService(), sp.GetRequiredService(), @@ -53,13 +52,13 @@ public static IHostBuilder AddHikIsapi(this IHostBuilder hostBuilder, string con public static IServiceCollection AddKeyedHikIsapi(this IServiceCollection services, object isapiKey, Func implementationFactory) where TConfig : class, IIsapiConfig { - return services + services .AddKeyedSingleton(isapiKey, implementationFactory) - .AddSeriRestClient($"{Isapi.ServiceKey}_{isapiKey}", (sp, seriRestKey) => - { - return sp.GetRequiredKeyedService(isapiKey) - .GetRestClientOptions(); - }) + .AddSeriRestClient($"{Isapi.ServiceKey}_{isapiKey}") + .Configure((sp, _, options) => sp.GetRequiredKeyedService(isapiKey) + .ApplyRestClientOptions(options)); + + return services .AddKeyedScoped(isapiKey, (sp, _) => new Isapi( sp.GetRequiredKeyedService(isapiKey), sp.GetRequiredService(), @@ -74,11 +73,20 @@ public static IServiceCollection AddKeyedHikIsapi(this IServiceCollecti /// Configured pointing at the device's ISAPI endpoint. public static RestClientOptions GetRestClientOptions(this IIsapiConfig config) { - return new RestClientOptions() - { - CookieContainer = new CookieContainer(), - BaseUrl = new Uri($"{config.Address}/ISAPI"), - Timeout = TimeSpan.FromMilliseconds(config.MaxTimeout ?? 5000) - }; + var options = new RestClientOptions(); + config.ApplyRestClientOptions(options); + return options; + } + + /// + /// Applies a device's configuration onto an existing instance. + /// + /// The device configuration. + /// The options instance to configure. + private static void ApplyRestClientOptions(this IIsapiConfig config, RestClientOptions options) + { + options.CookieContainer = new CookieContainer(); + options.BaseUrl = new Uri($"{config.Address}/ISAPI"); + options.Timeout = TimeSpan.FromMilliseconds(config.MaxTimeout ?? 5000); } }