< 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: 148
Uncovered lines: 9
Coverable lines: 157
Total lines: 297
Line coverage: 94.2%
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.Expressions.Options;
 6using Elsa.Extensions;
 7using Elsa.Features.Abstractions;
 8using Elsa.Features.Attributes;
 9using Elsa.Features.Services;
 10using Elsa.Workflows.ActivationValidators;
 11using Elsa.Workflows.Activities.Flowchart.Models;
 12using Elsa.Workflows.Builders;
 13using Elsa.Workflows.CommitStates;
 14using Elsa.Workflows.Exceptions;
 15using Elsa.Workflows.IncidentStrategies;
 16using Elsa.Workflows.LogPersistence;
 17using Elsa.Workflows.LogPersistence.Strategies;
 18using Elsa.Workflows.Middleware.Activities;
 19using Elsa.Workflows.Middleware.Workflows;
 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 Newtonsoft.Json.Linq;
 35
 36namespace Elsa.Workflows.Features;
 37
 38/// <summary>
 39/// Adds workflow services to the system.
 40/// </summary>
 41[DependsOn(typeof(SystemClockFeature))]
 42[DependsOn(typeof(ExpressionsFeature))]
 43[DependsOn(typeof(MediatorFeature))]
 44[DependsOn(typeof(DefaultFormattersFeature))]
 45[DependsOn(typeof(MultitenancyFeature))]
 46[DependsOn(typeof(CommitStrategiesFeature))]
 47public class WorkflowsFeature : FeatureBase
 48{
 49    /// <inheritdoc />
 12750    public WorkflowsFeature(IModule module) : base(module)
 51    {
 12752    }
 53
 54    /// <summary>
 55    /// A factory that instantiates a concrete <see cref="IStandardInStreamProvider"/>.
 56    /// </summary>
 25457    public Func<IServiceProvider, IStandardInStreamProvider> StandardInStreamProvider { get; set; } = _ => new StandardI
 58
 59    /// <summary>
 60    /// A factory that instantiates a concrete <see cref="IStandardOutStreamProvider"/>.
 61    /// </summary>
 75462    public Func<IServiceProvider, IStandardOutStreamProvider> StandardOutStreamProvider { get; set; } = _ => new Standar
 63
 64    /// <summary>
 65    /// A factory that instantiates a concrete <see cref="IIdentityGenerator"/>.
 66    /// </summary>
 38067    public Func<IServiceProvider, IIdentityGenerator> IdentityGenerator { get; set; } = sp => new RandomLongIdentityGene
 68
 69    /// <summary>
 70    /// A handler for committing workflow execution state.
 71    /// </summary>
 38072    public Func<IServiceProvider, ICommitStateHandler> CommitStateHandler { get; set; } = sp => new NoopCommitStateHandl
 73
 74    /// <summary>
 75    /// A factory that instantiates a concrete <see cref="ILoggerStateGenerator{WorkflowExecutionContext}"/>.
 76    /// </summary>
 37877    public Func<IServiceProvider, ILoggerStateGenerator<WorkflowExecutionContext>> WorkflowLoggerStateGenerator { get; s
 78
 79    /// <summary>
 80    /// A factory that instantiates a concrete <see cref="ILoggerStateGenerator{ActivityExecutionContext}"/>.
 81    /// </summary>
 38082    public Func<IServiceProvider, ILoggerStateGenerator<ActivityExecutionContext>> ActivityLoggerStateGenerator { get; s
 83
 84    /// <summary>
 85    /// A delegate to configure the <see cref="IWorkflowExecutionPipeline"/>.
 86    /// </summary>
 74387    public Action<IWorkflowExecutionPipelineBuilder> WorkflowExecutionPipeline { get; set; } = builder => builder
 12788        .UseExceptionHandling()
 12789        .UseDefaultActivityScheduler();
 90
 91    /// <summary>
 92    /// A delegate to configure the <see cref="IActivityExecutionPipeline"/>.
 93    /// </summary>
 74394    public Action<IActivityExecutionPipelineBuilder> ActivityExecutionPipeline { get; set; } = builder => builder.UseDef
 95
 96    /// <summary>
 97    /// Fluent method to set <see cref="StandardInStreamProvider"/>.
 98    /// </summary>
 99    public WorkflowsFeature WithStandardInStreamProvider(Func<IServiceProvider, IStandardInStreamProvider> provider)
 100    {
 0101        StandardInStreamProvider = provider;
 0102        return this;
 103    }
 104
 105    /// <summary>
 106    /// Fluent method to set <see cref="StandardOutStreamProvider"/>.
 107    /// </summary>
 108    public WorkflowsFeature WithStandardOutStreamProvider(Func<IServiceProvider, IStandardOutStreamProvider> provider)
 109    {
 246110        StandardOutStreamProvider = provider;
 246111        return this;
 112    }
 113
 114    /// <summary>
 115    /// Fluent method to set <see cref="IdentityGenerator"/>.
 116    /// </summary>
 117    public WorkflowsFeature WithIdentityGenerator(Func<IServiceProvider, IIdentityGenerator> generator)
 118    {
 0119        IdentityGenerator = generator;
 0120        return this;
 121    }
 122
 123    /// <summary>
 124    /// Fluent method to set <see cref="ILoggerStateGenerator{WorkflowExecutionContext}"/>.
 125    /// </summary>
 126    public WorkflowsFeature WithWorkflowLoggerStateGenerator(Func<IServiceProvider, ILoggerStateGenerator<WorkflowExecut
 127    {
 0128        WorkflowLoggerStateGenerator = generator;
 0129        return this;
 130    }
 131
 132    /// <summary>
 133    /// Fluent method to set <see cref="ILoggerStateGenerator{ActivityExecutionContext}"/>.
 134    /// </summary>
 135    public WorkflowsFeature WithActivityLoggerStateGenerator(Func<IServiceProvider, ILoggerStateGenerator<ActivityExecut
 136    {
 0137        ActivityLoggerStateGenerator = generator;
 0138        return this;
 139    }
 140
 141    /// <summary>
 142    /// Fluent method to configure the <see cref="IWorkflowExecutionPipeline"/>.
 143    /// </summary>
 144    public WorkflowsFeature WithWorkflowExecutionPipeline(Action<IWorkflowExecutionPipelineBuilder> setup)
 145    {
 126146        WorkflowExecutionPipeline = setup;
 126147        return this;
 148    }
 149
 150    /// <summary>
 151    /// Fluent method to configure the <see cref="IActivityExecutionPipeline"/>.
 152    /// </summary>
 153    public WorkflowsFeature WithActivityExecutionPipeline(Action<IActivityExecutionPipelineBuilder> setup)
 154    {
 126155        ActivityExecutionPipeline = setup;
 126156        return this;
 157    }
 158
 159    /// <inheritdoc />
 160    public override void Apply()
 161    {
 127162        AddElsaCore(Services);
 127163    }
 164
 165    private void AddElsaCore(IServiceCollection services)
 166    {
 127167        services.Configure<ExpressionOptions>(options =>
 127168        {
 127169            options.RegisterTypeAlias(typeof(ExceptionState), nameof(ExceptionState));
 127170            options.RegisterTypeAlias(typeof(FaultException), nameof(FaultException));
 127171            options.RegisterTypeAlias(typeof(VariablesDictionary), nameof(VariablesDictionary));
 127172            options.RegisterTypeAlias(typeof(Token), nameof(Token));
 127173            options.RegisterTypeAlias(typeof(FlowJoinMode), "Elsa.Workflows.Core.Activities.Flowchart.Models.FlowJoinMod
 127174            options.RegisterTypeAlias(typeof(FlowJoinMode), typeof(FlowJoinMode).GetSimpleAssemblyQualifiedName());
 127175            options.RegisterTypeAlias(typeof(FlowJoinMode), nameof(FlowJoinMode));
 127176            options.RegisterTypeAlias(typeof(WorkflowStorageDriver), typeof(WorkflowStorageDriver).GetSimpleAssemblyQual
 127177            options.RegisterTypeAlias(typeof(WorkflowStorageDriver), nameof(WorkflowStorageDriver));
 127178            options.RegisterTypeAlias(typeof(WorkflowInstanceStorageDriver), typeof(WorkflowInstanceStorageDriver).GetSi
 127179            options.RegisterTypeAlias(typeof(WorkflowInstanceStorageDriver), nameof(WorkflowInstanceStorageDriver));
 127180            options.RegisterTypeAlias(typeof(MemoryStorageDriver), typeof(MemoryStorageDriver).GetSimpleAssemblyQualifie
 127181            options.RegisterTypeAlias(typeof(MemoryStorageDriver), nameof(MemoryStorageDriver));
 127182            options.RegisterTypeAlias(typeof(FaultStrategy), typeof(FaultStrategy).GetSimpleAssemblyQualifiedName());
 127183            options.RegisterTypeAlias(typeof(FaultStrategy), nameof(FaultStrategy));
 127184            options.RegisterTypeAlias(typeof(Exception), nameof(Exception));
 127185            options.RegisterTypeAlias(typeof(ArgumentException), nameof(ArgumentException));
 127186            options.RegisterTypeAlias(typeof(ArgumentNullException), nameof(ArgumentNullException));
 127187            options.RegisterTypeAlias(typeof(InvalidOperationException), nameof(InvalidOperationException));
 127188            options.RegisterTypeAlias(typeof(NullReferenceException), nameof(NullReferenceException));
 127189            options.RegisterTypeAlias(typeof(OperationCanceledException), nameof(OperationCanceledException));
 127190            options.RegisterTypeAlias(typeof(TaskCanceledException), nameof(TaskCanceledException));
 127191            options.RegisterTypeAlias(typeof(TimeoutException), nameof(TimeoutException));
 127192            options.RegisterTypeAlias(typeof(NotSupportedException), nameof(NotSupportedException));
 127193            options.RegisterTypeAlias(typeof(JObject), nameof(JObject));
 127194            options.RegisterTypeAlias(typeof(JArray), nameof(JArray));
 254195        });
 196
 127197        services
 127198
 127199            // Core.
 127200            .AddScoped<IActivityInvoker, ActivityInvoker>()
 127201            .AddScoped<IWorkflowRunner, WorkflowRunner>()
 127202            .AddScoped<IActivityTestRunner, ActivityTestRunner>()
 127203            .AddScoped<IActivityVisitor, ActivityVisitor>()
 127204            .AddScoped<IIdentityGraphService, IdentityGraphService>()
 127205            .AddScoped<IWorkflowGraphBuilder, WorkflowGraphBuilder>()
 127206            .AddScoped<IWorkflowStateExtractor, WorkflowStateExtractor>()
 127207            .AddScoped<IActivitySchedulerFactory, ActivitySchedulerFactory>()
 127208            .AddSingleton<IWorkflowExecutionContextSchedulerStrategy, WorkflowExecutionContextSchedulerStrategy>()
 127209            .AddSingleton<IActivityExecutionContextSchedulerStrategy, ActivityExecutionContextSchedulerStrategy>()
 127210            .AddScoped(CommitStateHandler)
 127211            .AddSingleton<IHasher, Hasher>()
 127212            .AddSingleton<IStimulusHasher, StimulusHasher>()
 127213            .AddSingleton(IdentityGenerator)
 0214            .AddSingleton<IBookmarkPayloadSerializer>(sp => ActivatorUtilities.CreateInstance<BookmarkPayloadSerializer>
 127215            .AddSingleton<IActivityDescriber, ActivityDescriber>()
 127216            .AddSingleton<IActivityRegistry, ActivityRegistry>()
 127217            .AddScoped<IActivityRegistryLookupService, ActivityRegistryLookupService>()
 127218            .AddSingleton<IPropertyDefaultValueResolver, PropertyDefaultValueResolver>()
 127219            .AddSingleton<IPropertyUIHandlerResolver, PropertyUIHandlerResolver>()
 127220            .AddSingleton<IActivityFactory, ActivityFactory>()
 127221            .AddTransient<WorkflowBuilder>()
 4359222            .AddScoped(typeof(Func<IWorkflowBuilder>), sp => () => sp.GetRequiredService<WorkflowBuilder>())
 127223            .AddScoped<IWorkflowBuilderFactory, WorkflowBuilderFactory>()
 127224            .AddScoped<IVariablePersistenceManager, VariablePersistenceManager>()
 127225            .AddScoped<IIncidentStrategyResolver, DefaultIncidentStrategyResolver>()
 127226            .AddScoped<IActivityStateFilterManager, DefaultActivityStateFilterManager>()
 127227            .AddScoped<IWorkflowInstanceVariableReader, DefaultWorkflowInstanceVariableReader>()
 127228            .AddScoped<IWorkflowInstanceVariableWriter, DefaultWorkflowInstanceVariableWriter>()
 127229            .AddScoped<DefaultActivityInputEvaluator>()
 127230
 127231            // Incident Strategies.
 127232            .AddTransient<IIncidentStrategy, FaultStrategy>()
 127233            .AddTransient<IIncidentStrategy, ContinueWithIncidentsStrategy>()
 127234
 127235            // Pipelines.
 490236            .AddScoped<IActivityExecutionPipeline>(sp => new ActivityExecutionPipeline(sp, ActivityExecutionPipeline))
 490237            .AddScoped<IWorkflowExecutionPipeline>(sp => new WorkflowExecutionPipeline(sp, WorkflowExecutionPipeline))
 127238
 127239            // Built-in activity services.
 127240            .AddScoped<IActivityResolver, PropertyBasedActivityResolver>()
 127241            .AddScoped<IActivityResolver, SwitchActivityResolver>()
 127242            .AddSerializationOptionsConfigurator<AdditionalConvertersConfigurator>()
 127243            .AddSerializationOptionsConfigurator<CustomConstructorConfigurator>()
 127244
 127245            // Domain event handlers.
 127246            .AddHandlersFrom<WorkflowsFeature>()
 127247
 127248            // Stream providers.
 127249            .AddScoped(StandardInStreamProvider)
 127250            .AddScoped(StandardOutStreamProvider)
 127251
 127252            // Storage drivers.
 127253            .AddScoped<IStorageDriverManager, StorageDriverManager>()
 127254            .AddStorageDriver<WorkflowStorageDriver>()
 127255            .AddStorageDriver<WorkflowInstanceStorageDriver>()
 127256            .AddStorageDriver<MemoryStorageDriver>()
 127257
 127258            // Serialization.
 127259            .AddSingleton<IWorkflowStateSerializer, JsonWorkflowStateSerializer>()
 127260            .AddSingleton<IPayloadSerializer, JsonPayloadSerializer>()
 127261            .AddSingleton<IActivitySerializer, JsonActivitySerializer>()
 127262            .AddSingleton<IApiSerializer, ApiSerializer>()
 127263            .AddSingleton<ISafeSerializer, SafeSerializer>()
 127264            .AddSingleton<IJsonSerializer, StandardJsonSerializer>()
 127265            .AddSingleton<SyntheticPropertiesWriter>()
 127266            .AddSingleton<ActivityWriter>()
 127267
 127268            // Instantiation strategies.
 127269            .AddScoped<IWorkflowActivationStrategy, AllowAlwaysStrategy>()
 127270
 127271            // UI.
 127272            .AddScoped<IUIHintHandler, DropDownUIHintHandler>()
 127273            .AddScoped<IUIHintHandler, CheckListUIHintHandler>()
 127274            .AddScoped<IUIHintHandler, RadioListUIHintHandler>()
 127275            .AddScoped<IUIHintHandler, JsonEditorUIHintHandler>()
 127276            .AddScoped<IPropertyUIHandler, StaticCheckListOptionsProvider>()
 127277            .AddScoped<IPropertyUIHandler, StaticRadioListOptionsProvider>()
 127278            .AddScoped<IPropertyUIHandler, StaticDropDownOptionsProvider>()
 127279            .AddScoped<IPropertyUIHandler, JsonCodeOptionsProvider>()
 127280            .AddScoped<DictionaryValueEvaluator>()
 127281            .AddSingleton<IActivityDescriptorModifier, DictionaryUIHintInputModifier>()
 127282
 127283            // Logger state generators.
 127284            .AddSingleton(WorkflowLoggerStateGenerator)
 127285            .AddSingleton(ActivityLoggerStateGenerator)
 127286
 127287            // Log Persistence Strategies.
 127288            .AddScoped<ILogPersistenceStrategyService, DefaultLogPersistenceStrategyService>()
 127289            .AddScoped<ILogPersistenceStrategy, Include>()
 127290            .AddScoped<ILogPersistenceStrategy, Exclude>()
 127291            .AddScoped<ILogPersistenceStrategy, Inherit>()
 127292            .AddScoped<ILogPersistenceStrategy, Configuration>()
 127293
 127294            // Logging
 127295            .AddLogging();
 127296    }
 297}