refactor(ClientOptions): add AfterHttpInit-property and bind to dependency injection
This commit is contained in:
parent
3eacbd89f7
commit
32b631a6c2
@ -9,4 +9,6 @@ public class ClientOptions
|
|||||||
public TimeSpan? Timeout { get; set; }
|
public TimeSpan? Timeout { get; set; }
|
||||||
|
|
||||||
public Dictionary<string, string?>? QueryStrings { get; set; }
|
public Dictionary<string, string?>? QueryStrings { get; set; }
|
||||||
|
|
||||||
|
public Action<HttpClient>? AfterHttpInit { get; set; }
|
||||||
}
|
}
|
||||||
@ -9,7 +9,7 @@ public static class DependencyInjection
|
|||||||
{
|
{
|
||||||
internal static readonly string EConnectClientName = Guid.NewGuid().ToString();
|
internal static readonly string EConnectClientName = Guid.NewGuid().ToString();
|
||||||
|
|
||||||
internal static IServiceCollection ConfigureEConnectClient(this IServiceCollection services)
|
internal static IServiceCollection AddEConnectHttpClient(this IServiceCollection services)
|
||||||
{
|
{
|
||||||
services.AddHttpClient(EConnectClientName, (provider, client) => {
|
services.AddHttpClient(EConnectClientName, (provider, client) => {
|
||||||
var opt = provider.GetRequiredService<IOptions<ClientOptions>>().Value;
|
var opt = provider.GetRequiredService<IOptions<ClientOptions>>().Value;
|
||||||
@ -20,7 +20,10 @@ public static class DependencyInjection
|
|||||||
|
|
||||||
if (opt.Timeout is TimeSpan timeout)
|
if (opt.Timeout is TimeSpan timeout)
|
||||||
client.Timeout = timeout;
|
client.Timeout = timeout;
|
||||||
|
|
||||||
|
opt.AfterHttpInit?.Invoke(client);
|
||||||
});
|
});
|
||||||
|
|
||||||
return services;
|
return services;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -28,7 +31,7 @@ public static class DependencyInjection
|
|||||||
{
|
{
|
||||||
Config config = new(services);
|
Config config = new(services);
|
||||||
options?.Invoke(config);
|
options?.Invoke(config);
|
||||||
services.ConfigureEConnectClient();
|
services.AddEConnectHttpClient();
|
||||||
services.AddScoped(typeof(IEConnectClient<>), typeof(EConnectClient<>));
|
services.AddScoped(typeof(IEConnectClient<>), typeof(EConnectClient<>));
|
||||||
return services;
|
return services;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user