diff --git a/aspnet-core/modules/webhooks/LINGYUN.Abp.WebHooks/LINGYUN/Abp/Webhooks/AbpWebhooksModule.cs b/aspnet-core/modules/webhooks/LINGYUN.Abp.WebHooks/LINGYUN/Abp/Webhooks/AbpWebhooksModule.cs index deabfcd79..768eacd41 100644 --- a/aspnet-core/modules/webhooks/LINGYUN.Abp.WebHooks/LINGYUN/Abp/Webhooks/AbpWebhooksModule.cs +++ b/aspnet-core/modules/webhooks/LINGYUN.Abp.WebHooks/LINGYUN/Abp/Webhooks/AbpWebhooksModule.cs @@ -17,11 +17,23 @@ namespace LINGYUN.Abp.Webhooks; [DependsOn(typeof(AbpHttpClientModule))] public class AbpWebhooksModule : AbpModule { + internal const string WebhooksClient = "Abp.Webhooks.HttpClient"; + public override void PreConfigureServices(ServiceConfigurationContext context) { AutoAddDefinitionProviders(context.Services); } + public override void ConfigureServices(ServiceConfigurationContext context) + { + var options = context.Services.ExecutePreConfiguredActions(); + + context.Services.AddHttpClient(WebhooksClient, client => + { + client.Timeout = options.TimeoutDuration; + }); + } + private static void AutoAddDefinitionProviders(IServiceCollection services) { var definitionProviders = new List(); diff --git a/aspnet-core/modules/webhooks/LINGYUN.Abp.WebHooks/LINGYUN/Abp/Webhooks/AbpWebhooksOptions.cs b/aspnet-core/modules/webhooks/LINGYUN.Abp.WebHooks/LINGYUN/Abp/Webhooks/AbpWebhooksOptions.cs index 7ac2ec457..072a76bcc 100644 --- a/aspnet-core/modules/webhooks/LINGYUN.Abp.WebHooks/LINGYUN/Abp/Webhooks/AbpWebhooksOptions.cs +++ b/aspnet-core/modules/webhooks/LINGYUN.Abp.WebHooks/LINGYUN/Abp/Webhooks/AbpWebhooksOptions.cs @@ -5,12 +5,21 @@ namespace LINGYUN.Abp.Webhooks; public class AbpWebhooksOptions { + /// + /// 默认超时时间 + /// public TimeSpan TimeoutDuration { get; set; } - + /// + /// 默认最大发送次数 + /// public int MaxSendAttemptCount { get; set; } - + /// + /// 是否达到最大连续失败次数时自动取消订阅 + /// public bool IsAutomaticSubscriptionDeactivationEnabled { get; set; } - + /// + /// 取消订阅前最大连续失败次数 + /// public int MaxConsecutiveFailCountBeforeDeactivateSubscription { get; set; } public ITypeList DefinitionProviders { get; } diff --git a/aspnet-core/modules/webhooks/LINGYUN.Abp.WebHooks/LINGYUN/Abp/Webhooks/DefaultWebhookSender.cs b/aspnet-core/modules/webhooks/LINGYUN.Abp.WebHooks/LINGYUN/Abp/Webhooks/DefaultWebhookSender.cs index 29ce6cb96..d400800bf 100644 --- a/aspnet-core/modules/webhooks/LINGYUN.Abp.WebHooks/LINGYUN/Abp/Webhooks/DefaultWebhookSender.cs +++ b/aspnet-core/modules/webhooks/LINGYUN.Abp.WebHooks/LINGYUN/Abp/Webhooks/DefaultWebhookSender.cs @@ -15,15 +15,18 @@ namespace LINGYUN.Abp.Webhooks private readonly AbpWebhooksOptions _options; private readonly IWebhookManager _webhookManager; + private readonly IHttpClientFactory _httpClientFactory; private const string FailedRequestDefaultContent = "Webhook Send Request Failed"; public DefaultWebhookSender( IOptions options, - IWebhookManager webhookManager) + IWebhookManager webhookManager, + IHttpClientFactory httpClientFactory) { _options = options.Value; _webhookManager = webhookManager; + _httpClientFactory = httpClientFactory; Logger = NullLogger.Instance; } @@ -116,19 +119,15 @@ namespace LINGYUN.Abp.Webhooks protected virtual async Task<(bool isSucceed, HttpStatusCode statusCode, string content)> SendHttpRequest(HttpRequestMessage request) { - using (var client = new HttpClient - { - Timeout = _options.TimeoutDuration - }) - { - var response = await client.SendAsync(request); + var client = _httpClientFactory.CreateClient(AbpWebhooksModule.WebhooksClient); - var isSucceed = response.IsSuccessStatusCode; - var statusCode = response.StatusCode; - var content = await response.Content.ReadAsStringAsync(); + var response = await client.SendAsync(request); - return (isSucceed, statusCode, content); - } + var isSucceed = response.IsSuccessStatusCode; + var statusCode = response.StatusCode; + var content = await response.Content.ReadAsStringAsync(); + + return (isSucceed, statusCode, content); } } }