< Summary

Information
Class: Elsa.Workflows.Features.WorkflowsFeature
Assembly: Elsa.Workflows.Core
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Core/Features/WorkflowsFeature.cs
Line coverage
94%
Covered lines: 162
Uncovered lines: 9
Coverable lines: 171
Total lines: 313
Line coverage: 94.7%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Core/Features/WorkflowsFeature.cs

#LineLine coverage
 1using Elsa.Common;
 2using Elsa.Common.Features;
 3using Elsa.Common.Serialization;
 4using Elsa.Expressions.Features;
 5using Elsa.Extensions;
 6using Elsa.Features.Abstractions;
 7using Elsa.Features.Attributes;
 8using Elsa.Features.Services;
 9using Elsa.Workflows.ActivationValidators;
 10using Elsa.Workflows.Activities.Flowchart.Models;
 11using Elsa.Workflows.Builders;
 12using Elsa.Workflows.CommitStates;
 13using Elsa.Workflows.Exceptions;
 14using Elsa.Workflows.IncidentStrategies;
 15using Elsa.Workflows.LogPersistence;
 16using Elsa.Workflows.LogPersistence.Strategies;
 17using Elsa.Workflows.Middleware.Activities;
 18using Elsa.Workflows.Middleware.Workflows;
 19using Elsa.Workflows.Options;
 20using Elsa.Workflows.Pipelines.ActivityExecution;
 21using Elsa.Workflows.Pipelines.WorkflowExecution;
 22using Elsa.Workflows.PortResolvers;
 23using Elsa.Workflows.Serialization.Configurators;
 24using Elsa.Workflows.Serialization.Helpers;
 25using Elsa.Workflows.Serialization.Serializers;
 26using Elsa.Workflows.Services;
 27using Elsa.Workflows.State;
 28using Elsa.Workflows.UIHints.CheckList;
 29using Elsa.Workflows.UIHints.Dictionary;
 30using Elsa.Workflows.UIHints.Dropdown;
 31using Elsa.Workflows.UIHints.JsonEditor;
 32using Elsa.Workflows.UIHints.RadioList;
 33using Microsoft.Extensions.DependencyInjection;
 34using Microsoft.Extensions.DependencyInjection.Extensions;
 35using Newtonsoft.Json.Linq;
 36
 37namespace Elsa.Workflows.Features;
 38
 39/// <summary>
 40/// Adds workflow services to the system.
 41/// </summary>
 42[DependsOn(typeof(SystemClockFeature))]
 43[DependsOn(typeof(ExpressionsFeature))]
 44[DependsOn(typeof(MediatorFeature))]
 45[DependsOn(typeof(DefaultFormattersFeature))]
 46[DependsOn(typeof(MultitenancyFeature))]
 47[DependsOn(typeof(CommitStrategiesFeature))]
 48public class WorkflowsFeature : FeatureBase
 49{
 50    /// <inheritdoc />
 12851    public WorkflowsFeature(IModule module) : base(module)
 52    {
 12853    }
 54
 55    /// <summary>
 56    /// A factory that instantiates a concrete <see cref="IStandardInStreamProvider"/>.
 57    /// </summary>
 25658    public Func<IServiceProvider, IStandardInStreamProvider> StandardInStreamProvider { get; set; } = _ => new StandardI
 59
 60    /// <summary>
 61    /// A factory that instantiates a concrete <see cref="IStandardOutStreamProvider"/>.
 62    /// </summary>
 75863    public Func<IServiceProvider, IStandardOutStreamProvider> StandardOutStreamProvider { get; set; } = _ => new Standar
 64
 65    /// <summary>
 66    /// A factory that instantiates a concrete <see cref="IIdentityGenerator"/>.
 67    /// </summary>
 38368    public Func<IServiceProvider, IIdentityGenerator> IdentityGenerator { get; set; } = sp => new RandomLongIdentityGene
 69
 70    /// <summary>
 71    /// A handler for committing workflow execution state.
 72    /// </summary>
 38373    public Func<IServiceProvider, ICommitStateHandler> CommitStateHandler { get; set; } = sp => new NoopCommitStateHandl
 74
 75    /// <summary>
 76    /// A factory that instantiates a concrete <see cref="ILoggerStateGenerator{WorkflowExecutionContext}"/>.
 77    /// </summary>
 38178    public Func<IServiceProvider, ILoggerStateGenerator<WorkflowExecutionContext>> WorkflowLoggerStateGenerator { get; s
 79
 80    /// <summary>
 81    /// A factory that instantiates a concrete <see cref="ILoggerStateGenerator{ActivityExecutionContext}"/>.
 82    /// </summary>
 38383    public Func<IServiceProvider, ILoggerStateGenerator<ActivityExecutionContext>> ActivityLoggerStateGenerator { get; s
 84
 85    /// <summary>
 86    /// A delegate to configure the <see cref="IWorkflowExecutionPipeline"/>.
 87    /// </summary>
 75388    public Action<IWorkflowExecutionPipelineBuilder> WorkflowExecutionPipeline { get; set; } = builder => builder
 12889        .UseExceptionHandling()
 12890        .UseDefaultActivityScheduler();
 91
 92    /// <summary>
 93    /// A delegate to configure the <see cref="IActivityExecutionPipeline"/>.
 94    /// </summary>
 74995    public Action<IActivityExecutionPipelineBuilder> ActivityExecutionPipeline { get; set; } = builder => builder
 12896        .UseLogging()
 12897        .UseExceptionHandling()
 12898        .UseExecutionLogging()
 12899        .UseNotifications()
 128100        .UseDefaultActivityInvoker();
 101
 102    /// <summary>
 103    /// Fluent method to set <see cref="StandardInStreamProvider"/>.
 104    /// </summary>
 105    public WorkflowsFeature WithStandardInStreamProvider(Func<IServiceProvider, IStandardInStreamProvider> provider)
 106    {
 0107        StandardInStreamProvider = provider;
 0108        return this;
 109    }
 110
 111    /// <summary>
 112    /// Fluent method to set <see cref="StandardOutStreamProvider"/>.
 113    /// </summary>
 114    public WorkflowsFeature WithStandardOutStreamProvider(Func<IServiceProvider, IStandardOutStreamProvider> provider)
 115    {
 248116        StandardOutStreamProvider = provider;
 248117        return this;
 118    }
 119
 120    /// <summary>
 121    /// Fluent method to set <see cref="IdentityGenerator"/>.
 122    /// </summary>
 123    public WorkflowsFeature WithIdentityGenerator(Func<IServiceProvider, IIdentityGenerator> generator)
 124    {
 0125        IdentityGenerator = generator;
 0126        return this;
 127    }
 128
 129    /// <summary>
 130    /// Fluent method to set <see cref="ILoggerStateGenerator{WorkflowExecutionContext}"/>.
 131    /// </summary>
 132    public WorkflowsFeature WithWorkflowLoggerStateGenerator(Func<IServiceProvider, ILoggerStateGenerator<WorkflowExecut
 133    {
 0134        WorkflowLoggerStateGenerator = generator;
 0135        return this;
 136    }
 137
 138    /// <summary>
 139    /// Fluent method to set <see cref="ILoggerStateGenerator{ActivityExecutionContext}"/>.
 140    /// </summary>
 141    public WorkflowsFeature WithActivityLoggerStateGenerator(Func<IServiceProvider, ILoggerStateGenerator<ActivityExecut
 142    {
 0143        ActivityLoggerStateGenerator = generator;
 0144        return this;
 145    }
 146
 147    /// <summary>
 148    /// Fluent method to configure the <see cref="IWorkflowExecutionPipeline"/>.
 149    /// </summary>
 150    public WorkflowsFeature WithWorkflowExecutionPipeline(Action<IWorkflowExecutionPipelineBuilder> setup)
 151    {
 127152        WorkflowExecutionPipeline = setup;
 127153        return this;
 154    }
 155
 156    /// <summary>
 157    /// Fluent method to configure the <see cref="IActivityExecutionPipeline"/>.
 158    /// </summary>
 159    public WorkflowsFeature WithActivityExecutionPipeline(Action<IActivityExecutionPipelineBuilder> setup)
 160    {
 127161        ActivityExecutionPipeline = setup;
 127162        return this;
 163    }
 164
 165    /// <inheritdoc />
 166    public override void Apply()
 167    {
 128168        AddElsaCore(Services);
 128169    }
 170
 171    private void AddElsaCore(IServiceCollection services)
 172    {
 128173        services.Configure<SerializationTypeOptions>(options =>
 128174        {
 128175            options.AddTypeAlias<ExceptionState>(nameof(ExceptionState));
 128176            options.AddTypeAlias<FaultException>(nameof(FaultException));
 128177            options.AddTypeAlias<VariablesDictionary>(nameof(VariablesDictionary));
 128178            options.AddTypeAlias<Token>(nameof(Token));
 128179            options.RegisterLegacyTypeName(typeof(FlowJoinMode), "Elsa.Workflows.Core.Activities.Flowchart.Models.FlowJo
 128180            options.AddTypeAliasWithLegacyName<FlowJoinMode>(nameof(FlowJoinMode));
 128181            options.AddTypeAliasWithLegacyName<WorkflowStorageDriver>(nameof(WorkflowStorageDriver));
 128182            options.AddTypeAliasWithLegacyName<WorkflowInstanceStorageDriver>(nameof(WorkflowInstanceStorageDriver));
 128183            options.AddTypeAliasWithLegacyName<MemoryStorageDriver>(nameof(MemoryStorageDriver));
 128184            options.AddTypeAliasWithLegacyName<FaultStrategy>(nameof(FaultStrategy));
 128185            options.AddTypeAliasWithLegacyName<ContinueWithIncidentsStrategy>(nameof(ContinueWithIncidentsStrategy));
 128186            options.AddTypeAlias<Exception>(nameof(Exception));
 128187            options.AddTypeAlias<ArgumentException>(nameof(ArgumentException));
 128188            options.AddTypeAlias<ArgumentNullException>(nameof(ArgumentNullException));
 128189            options.AddTypeAlias<InvalidOperationException>(nameof(InvalidOperationException));
 128190            options.AddTypeAlias<NullReferenceException>(nameof(NullReferenceException));
 128191            options.AddTypeAlias<OperationCanceledException>(nameof(OperationCanceledException));
 128192            options.AddTypeAlias<TaskCanceledException>(nameof(TaskCanceledException));
 128193            options.AddTypeAlias<TimeoutException>(nameof(TimeoutException));
 128194            options.AddTypeAlias<NotSupportedException>(nameof(NotSupportedException));
 128195            options.AddTypeAlias<JObject>(nameof(JObject));
 128196            options.AddTypeAlias<JArray>(nameof(JArray));
 256197        });
 198
 128199        services.TryAddSingleton<IOutputConverterRegistry, OutputConverterRegistry>();
 200
 128201        services
 128202
 128203            // Core.
 128204            .AddScoped<IActivityInvoker, ActivityInvoker>()
 128205            .AddScoped<IWorkflowRunner, WorkflowRunner>()
 128206            .AddScoped<IActivityTestRunner, ActivityTestRunner>()
 128207            .AddScoped<IActivityVisitor, ActivityVisitor>()
 128208            .AddScoped<IIdentityGraphService, IdentityGraphService>()
 128209            .AddScoped<IWorkflowGraphBuilder, WorkflowGraphBuilder>()
 128210            .AddScoped<IWorkflowStateExtractor, WorkflowStateExtractor>()
 128211            .AddScoped<IActivitySchedulerFactory, ActivitySchedulerFactory>()
 128212            .AddSingleton<IWorkflowExecutionContextSchedulerStrategy, WorkflowExecutionContextSchedulerStrategy>()
 128213            .AddSingleton<IActivityExecutionContextSchedulerStrategy, ActivityExecutionContextSchedulerStrategy>()
 128214            .AddScoped(CommitStateHandler)
 128215            .AddSingleton<IHasher, Hasher>()
 128216            .AddSingleton<IStimulusHasher, StimulusHasher>()
 128217            .AddSingleton(IdentityGenerator)
 0218            .AddSingleton<IBookmarkPayloadSerializer>(sp => ActivatorUtilities.CreateInstance<BookmarkPayloadSerializer>
 128219            .AddSingleton<IActivityDescriber, ActivityDescriber>()
 128220            .AddSingleton<IActivityRegistry, ActivityRegistry>()
 128221            .AddScoped<IActivityRegistryLookupService, ActivityRegistryLookupService>()
 128222            .AddScoped<IOutputBindingDestinationResolver, OutputBindingDestinationResolver>()
 128223            .AddSingleton<IOutputConverterSettingsValidator, OutputConverterSettingsValidator>()
 128224            .AddScoped<IOutputConverterInvoker, OutputConverterInvoker>()
 128225            .AddSingleton<IPropertyDefaultValueResolver, PropertyDefaultValueResolver>()
 128226            .AddSingleton<IPropertyUIHandlerResolver, PropertyUIHandlerResolver>()
 128227            .AddSingleton<IActivityFactory, ActivityFactory>()
 128228            .AddTransient<WorkflowBuilder>()
 4367229            .AddScoped(typeof(Func<IWorkflowBuilder>), sp => () => sp.GetRequiredService<WorkflowBuilder>())
 128230            .AddScoped<IWorkflowBuilderFactory, WorkflowBuilderFactory>()
 128231            .AddScoped<IVariablePersistenceManager, VariablePersistenceManager>()
 128232            .AddScoped<IIncidentStrategyResolver, DefaultIncidentStrategyResolver>()
 128233            .AddScoped<IActivityStateFilterManager, DefaultActivityStateFilterManager>()
 128234            .AddScoped<IWorkflowInstanceVariableReader, DefaultWorkflowInstanceVariableReader>()
 128235            .AddScoped<IWorkflowInstanceVariableWriter, DefaultWorkflowInstanceVariableWriter>()
 128236            .AddScoped<DefaultActivityInputEvaluator>()
 128237
 128238            // Incident Strategies.
 128239            .AddTransient<IIncidentStrategy, FaultStrategy>()
 128240            .AddTransient<IIncidentStrategy, ContinueWithIncidentsStrategy>()
 128241
 128242            // Pipelines.
 494243            .AddScoped<IActivityExecutionPipeline>(sp => new ActivityExecutionPipeline(sp, builder =>
 494244            {
 494245                builder.UseActivityExecutionPipelineContributors(sp);
 494246                ActivityExecutionPipeline(builder);
 988247            }))
 494248            .AddScoped<IWorkflowExecutionPipeline>(sp => new WorkflowExecutionPipeline(sp, builder =>
 494249            {
 498250                builder.UseWorkflowExecutionPipelineContributors(sp);
 498251                WorkflowExecutionPipeline(builder);
 992252            }))
 128253
 128254            // Built-in activity services.
 128255            .AddScoped<IActivityResolver, PropertyBasedActivityResolver>()
 128256            .AddScoped<IActivityResolver, SwitchActivityResolver>()
 128257            .AddSerializationOptionsConfigurator<AdditionalConvertersConfigurator>()
 128258            .AddSerializationOptionsConfigurator<CustomConstructorConfigurator>()
 128259
 128260            // Domain event handlers.
 128261            .AddHandlersFrom<WorkflowsFeature>()
 128262
 128263            // Stream providers.
 128264            .AddScoped(StandardInStreamProvider)
 128265            .AddScoped(StandardOutStreamProvider)
 128266
 128267            // Storage drivers.
 128268            .AddScoped<IStorageDriverManager, StorageDriverManager>()
 128269            .AddStorageDriver<WorkflowStorageDriver>()
 128270            .AddStorageDriver<WorkflowInstanceStorageDriver>()
 128271            .AddStorageDriver<MemoryStorageDriver>()
 128272
 128273            // Serialization.
 128274            .AddSingleton<ISerializationTypeRegistry, SerializationTypeRegistry>()
 128275            .AddSingleton<IWorkflowStateSerializer, JsonWorkflowStateSerializer>()
 128276            .AddSingleton<IPayloadSerializer, JsonPayloadSerializer>()
 128277            .AddSingleton<IActivitySerializer, JsonActivitySerializer>()
 128278            .AddSingleton<IApiSerializer, ApiSerializer>()
 128279            .AddSingleton<ISafeSerializer, SafeSerializer>()
 128280            .AddSingleton<IJsonSerializer, StandardJsonSerializer>()
 128281            .AddSingleton<SyntheticPropertiesWriter>()
 128282            .AddSingleton<ActivityWriter>()
 128283
 128284            // Instantiation strategies.
 128285            .AddScoped<IWorkflowActivationStrategy, AllowAlwaysStrategy>()
 128286
 128287            // UI.
 128288            .AddScoped<IUIHintHandler, DropDownUIHintHandler>()
 128289            .AddScoped<IUIHintHandler, CheckListUIHintHandler>()
 128290            .AddScoped<IUIHintHandler, RadioListUIHintHandler>()
 128291            .AddScoped<IUIHintHandler, JsonEditorUIHintHandler>()
 128292            .AddScoped<IPropertyUIHandler, StaticCheckListOptionsProvider>()
 128293            .AddScoped<IPropertyUIHandler, StaticRadioListOptionsProvider>()
 128294            .AddScoped<IPropertyUIHandler, StaticDropDownOptionsProvider>()
 128295            .AddScoped<IPropertyUIHandler, JsonCodeOptionsProvider>()
 128296            .AddScoped<DictionaryValueEvaluator>()
 128297            .AddSingleton<IActivityDescriptorModifier, DictionaryUIHintInputModifier>()
 128298
 128299            // Logger state generators.
 128300            .AddSingleton(WorkflowLoggerStateGenerator)
 128301            .AddSingleton(ActivityLoggerStateGenerator)
 128302
 128303            // Log Persistence Strategies.
 128304            .AddScoped<ILogPersistenceStrategyService, DefaultLogPersistenceStrategyService>()
 128305            .AddScoped<ILogPersistenceStrategy, Include>()
 128306            .AddScoped<ILogPersistenceStrategy, Exclude>()
 128307            .AddScoped<ILogPersistenceStrategy, Inherit>()
 128308            .AddScoped<ILogPersistenceStrategy, Configuration>()
 128309
 128310            // Logging
 128311            .AddLogging();
 128312    }
 313}