diff --git a/src/activities/webhooks/Elsa.Activities.Webhooks/ActivityTypes/WebhookActivityTypeProvider.cs b/src/activities/webhooks/Elsa.Activities.Webhooks/ActivityTypes/WebhookActivityTypeProvider.cs index 3acb89472..b339ec016 100644 --- a/src/activities/webhooks/Elsa.Activities.Webhooks/ActivityTypes/WebhookActivityTypeProvider.cs +++ b/src/activities/webhooks/Elsa.Activities.Webhooks/ActivityTypes/WebhookActivityTypeProvider.cs @@ -14,6 +14,7 @@ using Elsa.Webhooks.Models; using Elsa.Webhooks.Persistence; using Humanizer; using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.DependencyInjection; namespace Elsa.Activities.Webhooks.ActivityTypes { @@ -23,23 +24,23 @@ namespace Elsa.Activities.Webhooks.ActivityTypes private const string WebhooksActivityTypeSuffix = "Webhook"; private const string WebhooksActivityCategory = "Webhooks"; - private readonly IWebhookDefinitionStore _webhookDefinitionStore; private readonly IActivityActivator _activityActivator; + private readonly IServiceScopeFactory _serviceScopeFactory; - public WebhookActivityTypeProvider( - IWebhookDefinitionStore webhookDefinitionStore, - IActivityActivator activityActivator) + public WebhookActivityTypeProvider(IActivityActivator activityActivator, IServiceScopeFactory serviceScopeFactory) { - _webhookDefinitionStore = webhookDefinitionStore; _activityActivator = activityActivator; + _serviceScopeFactory = serviceScopeFactory; } public async ValueTask> GetActivityTypesAsync(CancellationToken cancellationToken = default) { + using var scope = _serviceScopeFactory.CreateScope(); + var webhookDefinitionStore = scope.ServiceProvider.GetRequiredService(); var specification = Specification.Identity; - var definitions = await _webhookDefinitionStore.FindManyAsync(specification, cancellationToken: cancellationToken); - + var definitions = await webhookDefinitionStore.FindManyAsync(specification, cancellationToken: cancellationToken); var activityTypes = new List(); + foreach (var definition in definitions) { var activity = CreateWebhookActivityType(definition); @@ -53,7 +54,7 @@ namespace Elsa.Activities.Webhooks.ActivityTypes { var activityTypeName = webhook.Name.EndsWith(WebhooksActivityTypeSuffix) ? webhook.Name : $"{webhook.Name}{WebhooksActivityTypeSuffix}"; var activityDisplayName = activityTypeName.Humanize(); - + ValueTask CreateDescriptorAsync() { var descriptor = new ActivityDescriptor @@ -105,7 +106,7 @@ namespace Elsa.Activities.Webhooks.ActivityTypes }; return new ValueTask(descriptor); - }; + } async ValueTask ActivateActivityAsync(ActivityExecutionContext context) { diff --git a/src/activities/webhooks/Elsa.Activities.Webhooks/Extensions/WebhookOptionsBuilderExtensions.cs b/src/activities/webhooks/Elsa.Activities.Webhooks/Extensions/WebhookOptionsBuilderExtensions.cs index 20e7d029f..5a28365af 100644 --- a/src/activities/webhooks/Elsa.Activities.Webhooks/Extensions/WebhookOptionsBuilderExtensions.cs +++ b/src/activities/webhooks/Elsa.Activities.Webhooks/Extensions/WebhookOptionsBuilderExtensions.cs @@ -1,22 +1,40 @@ +using System; using Elsa.Activities.Webhooks.ActivityTypes; using Elsa.Activities.Webhooks.Bookmarks; using Elsa.Activities.Webhooks.Handlers; +using Elsa.Activities.Webhooks.Options; +using Elsa.Activities.Webhooks.Persistence.Decorators; using Elsa.Options; -using Elsa.Providers.Activities; +using Elsa.Webhooks.Persistence; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Options; namespace Elsa.Activities.Webhooks.Extensions { public static class WebhookOptionsBuilderExtensions { - public static ElsaOptionsBuilder AddWebhooks(this ElsaOptionsBuilder elsaOptions) + public static ElsaOptionsBuilder AddWebhooks(this ElsaOptionsBuilder elsaOptions, Action? configureOptions = default) { - elsaOptions.Services - .AddScoped() + var services = elsaOptions.Services; + + var optionsBuilder = new WebhookOptionsBuilder(services); + + services.Configure(webhookOptions => + { + configureOptions?.Invoke(optionsBuilder); + optionsBuilder.ApplyTo(webhookOptions); + }); + + services + .AddScoped(sp => sp.GetRequiredService>().Value.WebhookDefinitionStoreFactory(sp)) + .AddActivityTypeProvider() .AddBookmarkProvider() .AddNotificationHandlersFrom(); + services.Decorate(); + services.Decorate(); + return elsaOptions; } } -} +} \ No newline at end of file diff --git a/src/activities/webhooks/Elsa.Activities.Webhooks/WebhookOptionsBuilder.cs b/src/activities/webhooks/Elsa.Activities.Webhooks/WebhookOptionsBuilder.cs index 145c06b42..cf5e49549 100644 --- a/src/activities/webhooks/Elsa.Activities.Webhooks/WebhookOptionsBuilder.cs +++ b/src/activities/webhooks/Elsa.Activities.Webhooks/WebhookOptionsBuilder.cs @@ -8,12 +8,14 @@ namespace Elsa.Activities.Webhooks { public class WebhookOptionsBuilder { - public WebhookOptionsBuilder(IServiceCollection services) + public WebhookOptionsBuilder(IServiceCollection services) : this(services, new WebhookOptions()) + { + } + + public WebhookOptionsBuilder(IServiceCollection services, WebhookOptions webhookOptions) { - WebhookOptions = new WebhookOptions(); Services = services; - services.AddMemoryCache(); - services.AddSingleton(); + WebhookOptions = webhookOptions; } public WebhookOptions WebhookOptions { get; } @@ -24,5 +26,10 @@ namespace Elsa.Activities.Webhooks WebhookOptions.WebhookDefinitionStoreFactory = factory; return this; } + + public void ApplyTo(WebhookOptions webhookOptions) + { + webhookOptions.WebhookDefinitionStoreFactory = WebhookOptions.WebhookDefinitionStoreFactory; + } } -} +} \ No newline at end of file diff --git a/src/activities/webhooks/Elsa.Webhooks.Persistence.EntityFramework.Core/EntityFrameworkWebhookStartupBase.cs b/src/activities/webhooks/Elsa.Webhooks.Persistence.EntityFramework.Core/EntityFrameworkWebhookStartupBase.cs index 99333d9f2..c92f48f49 100644 --- a/src/activities/webhooks/Elsa.Webhooks.Persistence.EntityFramework.Core/EntityFrameworkWebhookStartupBase.cs +++ b/src/activities/webhooks/Elsa.Webhooks.Persistence.EntityFramework.Core/EntityFrameworkWebhookStartupBase.cs @@ -1,6 +1,6 @@ using System; using Elsa.Activities.Webhooks; -using Elsa.Activities.Webhooks.Persistence.Decorators; +using Elsa.Activities.Webhooks.Options; using Elsa.Options; using Elsa.Services.Startup; using Elsa.Webhooks.Persistence.EntityFramework.Core.Extensions; @@ -16,33 +16,26 @@ namespace Elsa.Webhooks.Persistence.EntityFramework.Core public override void ConfigureElsa(ElsaOptionsBuilder elsa, IConfiguration configuration) { - var services = elsa.Services; var section = configuration.GetSection($"Elsa:Features:Webhooks"); var connectionStringName = section.GetValue("ConnectionStringIdentifier"); var connectionString = section.GetValue("ConnectionString"); - if (string.IsNullOrWhiteSpace(connectionString)) - { - if (string.IsNullOrWhiteSpace(connectionStringName)) - connectionStringName = ProviderName; + if (string.IsNullOrWhiteSpace(connectionStringName)) + connectionStringName = ProviderName; + if (string.IsNullOrWhiteSpace(connectionString)) connectionString = configuration.GetConnectionString(connectionStringName); - } if (string.IsNullOrWhiteSpace(connectionString)) connectionString = GetDefaultConnectionString(); - var webhookOptionsBuilder = new WebhookOptionsBuilder(services); - webhookOptionsBuilder.UseEntityFrameworkPersistence(options => Configure(options, connectionString)); + var optionsBuilder = new WebhookOptionsBuilder(elsa.Services); + optionsBuilder.UseEntityFrameworkPersistence(ef => Configure(ef, connectionString)); - services.AddScoped(sp => webhookOptionsBuilder.WebhookOptions.WebhookDefinitionStoreFactory(sp)); - - services.Decorate(); - services.Decorate(); + elsa.Services.Configure(options => optionsBuilder.ApplyTo(options)); } protected virtual string GetDefaultConnectionString() => throw new Exception($"No connection string specified for the {ProviderName} provider"); protected abstract void Configure(DbContextOptionsBuilder options, string connectionString); - } } \ No newline at end of file