< 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 />
 14451    public WorkflowsFeature(IModule module) : base(module)
 52    {
 14453    }
 54
 55    /// <summary>
 56    /// A factory that instantiates a concrete <see cref="IStandardInStreamProvider"/>.
 57    /// </summary>
 28858    public Func<IServiceProvider, IStandardInStreamProvider> StandardInStreamProvider { get; set; } = _ => new StandardI
 59
 60    /// <summary>
 61    /// A factory that instantiates a concrete <see cref="IStandardOutStreamProvider"/>.
 62    /// </summary>
 79663    public Func<IServiceProvider, IStandardOutStreamProvider> StandardOutStreamProvider { get; set; } = _ => new Standar
 64
 65    /// <summary>
 66    /// A factory that instantiates a concrete <see cref="IIdentityGenerator"/>.
 67    /// </summary>
 43168    public Func<IServiceProvider, IIdentityGenerator> IdentityGenerator { get; set; } = sp => new RandomLongIdentityGene
 69
 70    /// <summary>
 71    /// A handler for committing workflow execution state.
 72    /// </summary>
 43173    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>
 41578    public Func<IServiceProvider, ILoggerStateGenerator<WorkflowExecutionContext>> WorkflowLoggerStateGenerator { get; s
 79
 80    /// <summary>
 81    /// A factory that instantiates a concrete <see cref="ILoggerStateGenerator{ActivityExecutionContext}"/>.
 82    /// </summary>
 41783    public Func<IServiceProvider, ILoggerStateGenerator<ActivityExecutionContext>> ActivityLoggerStateGenerator { get; s
 84
 85    /// <summary>
 86    /// A delegate to configure the <see cref="IWorkflowExecutionPipeline"/>.
 87    /// </summary>
 78988    public Action<IWorkflowExecutionPipelineBuilder> WorkflowExecutionPipeline { get; set; } = builder => builder
 14489        .UseExceptionHandling()
 14490        .UseDefaultActivityScheduler();
 91
 92    /// <summary>
 93    /// A delegate to configure the <see cref="IActivityExecutionPipeline"/>.
 94    /// </summary>
 78595    public Action<IActivityExecutionPipelineBuilder> ActivityExecutionPipeline { get; set; } = builder => builder
 14496        .UseLogging()
 14497        .UseExceptionHandling()
 14498        .UseExecutionLogging()
 14499        .UseNotifications()
 144100        .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    {
 252116        StandardOutStreamProvider = provider;
 252117        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    {
 143152        WorkflowExecutionPipeline = setup;
 143153        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    {
 143161        ActivityExecutionPipeline = setup;
 143162        return this;
 163    }
 164
 165    /// <inheritdoc />
 166    public override void Apply()
 167    {
 144168        AddElsaCore(Services);
 144169    }
 170
 171    private void AddElsaCore(IServiceCollection services)
 172    {
 144173        services.Configure<SerializationTypeOptions>(options =>
 144174        {
 144175            options.AddTypeAlias<ExceptionState>(nameof(ExceptionState));
 144176            options.AddTypeAlias<FaultException>(nameof(FaultException));
 144177            options.AddTypeAlias<VariablesDictionary>(nameof(VariablesDictionary));
 144178            options.AddTypeAlias<Token>(nameof(Token));
 144179            options.RegisterLegacyTypeName(typeof(FlowJoinMode), "Elsa.Workflows.Core.Activities.Flowchart.Models.FlowJo
 144180            options.AddTypeAliasWithLegacyName<FlowJoinMode>(nameof(FlowJoinMode));
 144181            options.AddTypeAliasWithLegacyName<WorkflowStorageDriver>(nameof(WorkflowStorageDriver));
 144182            options.AddTypeAliasWithLegacyName<WorkflowInstanceStorageDriver>(nameof(WorkflowInstanceStorageDriver));
 144183            options.AddTypeAliasWithLegacyName<MemoryStorageDriver>(nameof(MemoryStorageDriver));
 144184            options.AddTypeAliasWithLegacyName<FaultStrategy>(nameof(FaultStrategy));
 144185            options.AddTypeAliasWithLegacyName<ContinueWithIncidentsStrategy>(nameof(ContinueWithIncidentsStrategy));
 144186            options.AddTypeAlias<Exception>(nameof(Exception));
 144187            options.AddTypeAlias<ArgumentException>(nameof(ArgumentException));
 144188            options.AddTypeAlias<ArgumentNullException>(nameof(ArgumentNullException));
 144189            options.AddTypeAlias<InvalidOperationException>(nameof(InvalidOperationException));
 144190            options.AddTypeAlias<NullReferenceException>(nameof(NullReferenceException));
 144191            options.AddTypeAlias<OperationCanceledException>(nameof(OperationCanceledException));
 144192            options.AddTypeAlias<TaskCanceledException>(nameof(TaskCanceledException));
 144193            options.AddTypeAlias<TimeoutException>(nameof(TimeoutException));
 144194            options.AddTypeAlias<NotSupportedException>(nameof(NotSupportedException));
 144195            options.AddTypeAlias<JObject>(nameof(JObject));
 144196            options.AddTypeAlias<JArray>(nameof(JArray));
 288197        });
 198
 144199        services.TryAddSingleton<IOutputConverterRegistry, OutputConverterRegistry>();
 200
 144201        services
 144202
 144203            // Core.
 144204            .AddScoped<IActivityInvoker, ActivityInvoker>()
 144205            .AddScoped<IWorkflowRunner, WorkflowRunner>()
 144206            .AddScoped<IActivityTestRunner, ActivityTestRunner>()
 144207            .AddScoped<IActivityVisitor, ActivityVisitor>()
 144208            .AddScoped<IIdentityGraphService, IdentityGraphService>()
 144209            .AddScoped<IWorkflowGraphBuilder, WorkflowGraphBuilder>()
 144210            .AddScoped<IWorkflowStateExtractor, WorkflowStateExtractor>()
 144211            .AddScoped<IActivitySchedulerFactory, ActivitySchedulerFactory>()
 144212            .AddSingleton<IWorkflowExecutionContextSchedulerStrategy, WorkflowExecutionContextSchedulerStrategy>()
 144213            .AddSingleton<IActivityExecutionContextSchedulerStrategy, ActivityExecutionContextSchedulerStrategy>()
 144214            .AddScoped(CommitStateHandler)
 144215            .AddSingleton<IHasher, Hasher>()
 144216            .AddSingleton<IStimulusHasher, StimulusHasher>()
 144217            .AddSingleton(IdentityGenerator)
 0218            .AddSingleton<IBookmarkPayloadSerializer>(sp => ActivatorUtilities.CreateInstance<BookmarkPayloadSerializer>
 144219            .AddSingleton<IActivityDescriber, ActivityDescriber>()
 144220            .AddSingleton<IActivityRegistry, ActivityRegistry>()
 144221            .AddScoped<IActivityRegistryLookupService, ActivityRegistryLookupService>()
 144222            .AddScoped<IOutputBindingDestinationResolver, OutputBindingDestinationResolver>()
 144223            .AddSingleton<IOutputConverterSettingsValidator, OutputConverterSettingsValidator>()
 144224            .AddScoped<IOutputConverterInvoker, OutputConverterInvoker>()
 144225            .AddSingleton<IPropertyDefaultValueResolver, PropertyDefaultValueResolver>()
 144226            .AddSingleton<IPropertyUIHandlerResolver, PropertyUIHandlerResolver>()
 144227            .AddSingleton<IActivityFactory, ActivityFactory>()
 144228            .AddTransient<WorkflowBuilder>()
 4373229            .AddScoped(typeof(Func<IWorkflowBuilder>), sp => () => sp.GetRequiredService<WorkflowBuilder>())
 144230            .AddScoped<IWorkflowBuilderFactory, WorkflowBuilderFactory>()
 144231            .AddScoped<IVariablePersistenceManager, VariablePersistenceManager>()
 144232            .AddScoped<IIncidentStrategyResolver, DefaultIncidentStrategyResolver>()
 144233            .AddScoped<IActivityStateFilterManager, DefaultActivityStateFilterManager>()
 144234            .AddScoped<IWorkflowInstanceVariableReader, DefaultWorkflowInstanceVariableReader>()
 144235            .AddScoped<IWorkflowInstanceVariableWriter, DefaultWorkflowInstanceVariableWriter>()
 144236            .AddScoped<DefaultActivityInputEvaluator>()
 144237
 144238            // Incident Strategies.
 144239            .AddTransient<IIncidentStrategy, FaultStrategy>()
 144240            .AddTransient<IIncidentStrategy, ContinueWithIncidentsStrategy>()
 144241
 144242            // Pipelines.
 498243            .AddScoped<IActivityExecutionPipeline>(sp => new ActivityExecutionPipeline(sp, builder =>
 498244            {
 498245                builder.UseActivityExecutionPipelineContributors(sp);
 498246                ActivityExecutionPipeline(builder);
 996247            }))
 498248            .AddScoped<IWorkflowExecutionPipeline>(sp => new WorkflowExecutionPipeline(sp, builder =>
 498249            {
 502250                builder.UseWorkflowExecutionPipelineContributors(sp);
 502251                WorkflowExecutionPipeline(builder);
 1000252            }))
 144253
 144254            // Built-in activity services.
 144255            .AddScoped<IActivityResolver, PropertyBasedActivityResolver>()
 144256            .AddScoped<IActivityResolver, SwitchActivityResolver>()
 144257            .AddSerializationOptionsConfigurator<AdditionalConvertersConfigurator>()
 144258            .AddSerializationOptionsConfigurator<CustomConstructorConfigurator>()
 144259
 144260            // Domain event handlers.
 144261            .AddHandlersFrom<WorkflowsFeature>()
 144262
 144263            // Stream providers.
 144264            .AddScoped(StandardInStreamProvider)
 144265            .AddScoped(StandardOutStreamProvider)
 144266
 144267            // Storage drivers.
 144268            .AddScoped<IStorageDriverManager, StorageDriverManager>()
 144269            .AddStorageDriver<WorkflowStorageDriver>()
 144270            .AddStorageDriver<WorkflowInstanceStorageDriver>()
 144271            .AddStorageDriver<MemoryStorageDriver>()
 144272
 144273            // Serialization.
 144274            .AddSingleton<ISerializationTypeRegistry, SerializationTypeRegistry>()
 144275            .AddSingleton<IWorkflowStateSerializer, JsonWorkflowStateSerializer>()
 144276            .AddSingleton<IPayloadSerializer, JsonPayloadSerializer>()
 144277            .AddSingleton<IActivitySerializer, JsonActivitySerializer>()
 144278            .AddSingleton<IApiSerializer, ApiSerializer>()
 144279            .AddSingleton<ISafeSerializer, SafeSerializer>()
 144280            .AddSingleton<IJsonSerializer, StandardJsonSerializer>()
 144281            .AddSingleton<SyntheticPropertiesWriter>()
 144282            .AddSingleton<ActivityWriter>()
 144283
 144284            // Instantiation strategies.
 144285            .AddScoped<IWorkflowActivationStrategy, AllowAlwaysStrategy>()
 144286
 144287            // UI.
 144288            .AddScoped<IUIHintHandler, DropDownUIHintHandler>()
 144289            .AddScoped<IUIHintHandler, CheckListUIHintHandler>()
 144290            .AddScoped<IUIHintHandler, RadioListUIHintHandler>()
 144291            .AddScoped<IUIHintHandler, JsonEditorUIHintHandler>()
 144292            .AddScoped<IPropertyUIHandler, StaticCheckListOptionsProvider>()
 144293            .AddScoped<IPropertyUIHandler, StaticRadioListOptionsProvider>()
 144294            .AddScoped<IPropertyUIHandler, StaticDropDownOptionsProvider>()
 144295            .AddScoped<IPropertyUIHandler, JsonCodeOptionsProvider>()
 144296            .AddScoped<DictionaryValueEvaluator>()
 144297            .AddSingleton<IActivityDescriptorModifier, DictionaryUIHintInputModifier>()
 144298
 144299            // Logger state generators.
 144300            .AddSingleton(WorkflowLoggerStateGenerator)
 144301            .AddSingleton(ActivityLoggerStateGenerator)
 144302
 144303            // Log Persistence Strategies.
 144304            .AddScoped<ILogPersistenceStrategyService, DefaultLogPersistenceStrategyService>()
 144305            .AddScoped<ILogPersistenceStrategy, Include>()
 144306            .AddScoped<ILogPersistenceStrategy, Exclude>()
 144307            .AddScoped<ILogPersistenceStrategy, Inherit>()
 144308            .AddScoped<ILogPersistenceStrategy, Configuration>()
 144309
 144310            // Logging
 144311            .AddLogging();
 144312    }
 313}