< 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: 158
Uncovered lines: 9
Coverable lines: 167
Total lines: 307
Line coverage: 94.6%
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 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>
 74787    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
 12795        .UseLogging()
 12796        .UseExceptionHandling()
 12797        .UseExecutionLogging()
 12798        .UseNotifications()
 12799        .UseDefaultActivityInvoker();
 100
 101    /// <summary>
 102    /// Fluent method to set <see cref="StandardInStreamProvider"/>.
 103    /// </summary>
 104    public WorkflowsFeature WithStandardInStreamProvider(Func<IServiceProvider, IStandardInStreamProvider> provider)
 105    {
 0106        StandardInStreamProvider = provider;
 0107        return this;
 108    }
 109
 110    /// <summary>
 111    /// Fluent method to set <see cref="StandardOutStreamProvider"/>.
 112    /// </summary>
 113    public WorkflowsFeature WithStandardOutStreamProvider(Func<IServiceProvider, IStandardOutStreamProvider> provider)
 114    {
 246115        StandardOutStreamProvider = provider;
 246116        return this;
 117    }
 118
 119    /// <summary>
 120    /// Fluent method to set <see cref="IdentityGenerator"/>.
 121    /// </summary>
 122    public WorkflowsFeature WithIdentityGenerator(Func<IServiceProvider, IIdentityGenerator> generator)
 123    {
 0124        IdentityGenerator = generator;
 0125        return this;
 126    }
 127
 128    /// <summary>
 129    /// Fluent method to set <see cref="ILoggerStateGenerator{WorkflowExecutionContext}"/>.
 130    /// </summary>
 131    public WorkflowsFeature WithWorkflowLoggerStateGenerator(Func<IServiceProvider, ILoggerStateGenerator<WorkflowExecut
 132    {
 0133        WorkflowLoggerStateGenerator = generator;
 0134        return this;
 135    }
 136
 137    /// <summary>
 138    /// Fluent method to set <see cref="ILoggerStateGenerator{ActivityExecutionContext}"/>.
 139    /// </summary>
 140    public WorkflowsFeature WithActivityLoggerStateGenerator(Func<IServiceProvider, ILoggerStateGenerator<ActivityExecut
 141    {
 0142        ActivityLoggerStateGenerator = generator;
 0143        return this;
 144    }
 145
 146    /// <summary>
 147    /// Fluent method to configure the <see cref="IWorkflowExecutionPipeline"/>.
 148    /// </summary>
 149    public WorkflowsFeature WithWorkflowExecutionPipeline(Action<IWorkflowExecutionPipelineBuilder> setup)
 150    {
 126151        WorkflowExecutionPipeline = setup;
 126152        return this;
 153    }
 154
 155    /// <summary>
 156    /// Fluent method to configure the <see cref="IActivityExecutionPipeline"/>.
 157    /// </summary>
 158    public WorkflowsFeature WithActivityExecutionPipeline(Action<IActivityExecutionPipelineBuilder> setup)
 159    {
 126160        ActivityExecutionPipeline = setup;
 126161        return this;
 162    }
 163
 164    /// <inheritdoc />
 165    public override void Apply()
 166    {
 127167        AddElsaCore(Services);
 127168    }
 169
 170    private void AddElsaCore(IServiceCollection services)
 171    {
 127172        services.Configure<SerializationTypeOptions>(options =>
 127173        {
 127174            options.AddTypeAlias<ExceptionState>(nameof(ExceptionState));
 127175            options.AddTypeAlias<FaultException>(nameof(FaultException));
 127176            options.AddTypeAlias<VariablesDictionary>(nameof(VariablesDictionary));
 127177            options.AddTypeAlias<Token>(nameof(Token));
 127178            options.RegisterLegacyTypeName(typeof(FlowJoinMode), "Elsa.Workflows.Core.Activities.Flowchart.Models.FlowJo
 127179            options.AddTypeAliasWithLegacyName<FlowJoinMode>(nameof(FlowJoinMode));
 127180            options.AddTypeAliasWithLegacyName<WorkflowStorageDriver>(nameof(WorkflowStorageDriver));
 127181            options.AddTypeAliasWithLegacyName<WorkflowInstanceStorageDriver>(nameof(WorkflowInstanceStorageDriver));
 127182            options.AddTypeAliasWithLegacyName<MemoryStorageDriver>(nameof(MemoryStorageDriver));
 127183            options.AddTypeAliasWithLegacyName<FaultStrategy>(nameof(FaultStrategy));
 127184            options.AddTypeAliasWithLegacyName<ContinueWithIncidentsStrategy>(nameof(ContinueWithIncidentsStrategy));
 127185            options.AddTypeAlias<Exception>(nameof(Exception));
 127186            options.AddTypeAlias<ArgumentException>(nameof(ArgumentException));
 127187            options.AddTypeAlias<ArgumentNullException>(nameof(ArgumentNullException));
 127188            options.AddTypeAlias<InvalidOperationException>(nameof(InvalidOperationException));
 127189            options.AddTypeAlias<NullReferenceException>(nameof(NullReferenceException));
 127190            options.AddTypeAlias<OperationCanceledException>(nameof(OperationCanceledException));
 127191            options.AddTypeAlias<TaskCanceledException>(nameof(TaskCanceledException));
 127192            options.AddTypeAlias<TimeoutException>(nameof(TimeoutException));
 127193            options.AddTypeAlias<NotSupportedException>(nameof(NotSupportedException));
 127194            options.AddTypeAlias<JObject>(nameof(JObject));
 127195            options.AddTypeAlias<JArray>(nameof(JArray));
 254196        });
 197
 127198        services
 127199
 127200            // Core.
 127201            .AddScoped<IActivityInvoker, ActivityInvoker>()
 127202            .AddScoped<IWorkflowRunner, WorkflowRunner>()
 127203            .AddScoped<IActivityTestRunner, ActivityTestRunner>()
 127204            .AddScoped<IActivityVisitor, ActivityVisitor>()
 127205            .AddScoped<IIdentityGraphService, IdentityGraphService>()
 127206            .AddScoped<IWorkflowGraphBuilder, WorkflowGraphBuilder>()
 127207            .AddScoped<IWorkflowStateExtractor, WorkflowStateExtractor>()
 127208            .AddScoped<IActivitySchedulerFactory, ActivitySchedulerFactory>()
 127209            .AddSingleton<IWorkflowExecutionContextSchedulerStrategy, WorkflowExecutionContextSchedulerStrategy>()
 127210            .AddSingleton<IActivityExecutionContextSchedulerStrategy, ActivityExecutionContextSchedulerStrategy>()
 127211            .AddScoped(CommitStateHandler)
 127212            .AddSingleton<IHasher, Hasher>()
 127213            .AddSingleton<IStimulusHasher, StimulusHasher>()
 127214            .AddSingleton(IdentityGenerator)
 0215            .AddSingleton<IBookmarkPayloadSerializer>(sp => ActivatorUtilities.CreateInstance<BookmarkPayloadSerializer>
 127216            .AddSingleton<IActivityDescriber, ActivityDescriber>()
 127217            .AddSingleton<IActivityRegistry, ActivityRegistry>()
 127218            .AddScoped<IActivityRegistryLookupService, ActivityRegistryLookupService>()
 127219            .AddSingleton<IPropertyDefaultValueResolver, PropertyDefaultValueResolver>()
 127220            .AddSingleton<IPropertyUIHandlerResolver, PropertyUIHandlerResolver>()
 127221            .AddSingleton<IActivityFactory, ActivityFactory>()
 127222            .AddTransient<WorkflowBuilder>()
 4359223            .AddScoped(typeof(Func<IWorkflowBuilder>), sp => () => sp.GetRequiredService<WorkflowBuilder>())
 127224            .AddScoped<IWorkflowBuilderFactory, WorkflowBuilderFactory>()
 127225            .AddScoped<IVariablePersistenceManager, VariablePersistenceManager>()
 127226            .AddScoped<IIncidentStrategyResolver, DefaultIncidentStrategyResolver>()
 127227            .AddScoped<IActivityStateFilterManager, DefaultActivityStateFilterManager>()
 127228            .AddScoped<IWorkflowInstanceVariableReader, DefaultWorkflowInstanceVariableReader>()
 127229            .AddScoped<IWorkflowInstanceVariableWriter, DefaultWorkflowInstanceVariableWriter>()
 127230            .AddScoped<DefaultActivityInputEvaluator>()
 127231
 127232            // Incident Strategies.
 127233            .AddTransient<IIncidentStrategy, FaultStrategy>()
 127234            .AddTransient<IIncidentStrategy, ContinueWithIncidentsStrategy>()
 127235
 127236            // Pipelines.
 490237            .AddScoped<IActivityExecutionPipeline>(sp => new ActivityExecutionPipeline(sp, builder =>
 490238            {
 490239                builder.UseActivityExecutionPipelineContributors(sp);
 490240                ActivityExecutionPipeline(builder);
 980241            }))
 490242            .AddScoped<IWorkflowExecutionPipeline>(sp => new WorkflowExecutionPipeline(sp, builder =>
 490243            {
 494244                builder.UseWorkflowExecutionPipelineContributors(sp);
 494245                WorkflowExecutionPipeline(builder);
 984246            }))
 127247
 127248            // Built-in activity services.
 127249            .AddScoped<IActivityResolver, PropertyBasedActivityResolver>()
 127250            .AddScoped<IActivityResolver, SwitchActivityResolver>()
 127251            .AddSerializationOptionsConfigurator<AdditionalConvertersConfigurator>()
 127252            .AddSerializationOptionsConfigurator<CustomConstructorConfigurator>()
 127253
 127254            // Domain event handlers.
 127255            .AddHandlersFrom<WorkflowsFeature>()
 127256
 127257            // Stream providers.
 127258            .AddScoped(StandardInStreamProvider)
 127259            .AddScoped(StandardOutStreamProvider)
 127260
 127261            // Storage drivers.
 127262            .AddScoped<IStorageDriverManager, StorageDriverManager>()
 127263            .AddStorageDriver<WorkflowStorageDriver>()
 127264            .AddStorageDriver<WorkflowInstanceStorageDriver>()
 127265            .AddStorageDriver<MemoryStorageDriver>()
 127266
 127267            // Serialization.
 127268            .AddSingleton<ISerializationTypeRegistry, SerializationTypeRegistry>()
 127269            .AddSingleton<IWorkflowStateSerializer, JsonWorkflowStateSerializer>()
 127270            .AddSingleton<IPayloadSerializer, JsonPayloadSerializer>()
 127271            .AddSingleton<IActivitySerializer, JsonActivitySerializer>()
 127272            .AddSingleton<IApiSerializer, ApiSerializer>()
 127273            .AddSingleton<ISafeSerializer, SafeSerializer>()
 127274            .AddSingleton<IJsonSerializer, StandardJsonSerializer>()
 127275            .AddSingleton<SyntheticPropertiesWriter>()
 127276            .AddSingleton<ActivityWriter>()
 127277
 127278            // Instantiation strategies.
 127279            .AddScoped<IWorkflowActivationStrategy, AllowAlwaysStrategy>()
 127280
 127281            // UI.
 127282            .AddScoped<IUIHintHandler, DropDownUIHintHandler>()
 127283            .AddScoped<IUIHintHandler, CheckListUIHintHandler>()
 127284            .AddScoped<IUIHintHandler, RadioListUIHintHandler>()
 127285            .AddScoped<IUIHintHandler, JsonEditorUIHintHandler>()
 127286            .AddScoped<IPropertyUIHandler, StaticCheckListOptionsProvider>()
 127287            .AddScoped<IPropertyUIHandler, StaticRadioListOptionsProvider>()
 127288            .AddScoped<IPropertyUIHandler, StaticDropDownOptionsProvider>()
 127289            .AddScoped<IPropertyUIHandler, JsonCodeOptionsProvider>()
 127290            .AddScoped<DictionaryValueEvaluator>()
 127291            .AddSingleton<IActivityDescriptorModifier, DictionaryUIHintInputModifier>()
 127292
 127293            // Logger state generators.
 127294            .AddSingleton(WorkflowLoggerStateGenerator)
 127295            .AddSingleton(ActivityLoggerStateGenerator)
 127296
 127297            // Log Persistence Strategies.
 127298            .AddScoped<ILogPersistenceStrategyService, DefaultLogPersistenceStrategyService>()
 127299            .AddScoped<ILogPersistenceStrategy, Include>()
 127300            .AddScoped<ILogPersistenceStrategy, Exclude>()
 127301            .AddScoped<ILogPersistenceStrategy, Inherit>()
 127302            .AddScoped<ILogPersistenceStrategy, Configuration>()
 127303
 127304            // Logging
 127305            .AddLogging();
 127306    }
 307}