< 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 />
 12950    public WorkflowsFeature(IModule module) : base(module)
 51    {
 12952    }
 53
 54    /// <summary>
 55    /// A factory that instantiates a concrete <see cref="IStandardInStreamProvider"/>.
 56    /// </summary>
 25857    public Func<IServiceProvider, IStandardInStreamProvider> StandardInStreamProvider { get; set; } = _ => new StandardI
 58
 59    /// <summary>
 60    /// A factory that instantiates a concrete <see cref="IStandardOutStreamProvider"/>.
 61    /// </summary>
 76262    public Func<IServiceProvider, IStandardOutStreamProvider> StandardOutStreamProvider { get; set; } = _ => new Standar
 63
 64    /// <summary>
 65    /// A factory that instantiates a concrete <see cref="IIdentityGenerator"/>.
 66    /// </summary>
 38667    public Func<IServiceProvider, IIdentityGenerator> IdentityGenerator { get; set; } = sp => new RandomLongIdentityGene
 68
 69    /// <summary>
 70    /// A handler for committing workflow execution state.
 71    /// </summary>
 38672    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>
 38477    public Func<IServiceProvider, ILoggerStateGenerator<WorkflowExecutionContext>> WorkflowLoggerStateGenerator { get; s
 78
 79    /// <summary>
 80    /// A factory that instantiates a concrete <see cref="ILoggerStateGenerator{ActivityExecutionContext}"/>.
 81    /// </summary>
 38682    public Func<IServiceProvider, ILoggerStateGenerator<ActivityExecutionContext>> ActivityLoggerStateGenerator { get; s
 83
 84    /// <summary>
 85    /// A delegate to configure the <see cref="IWorkflowExecutionPipeline"/>.
 86    /// </summary>
 75387    public Action<IWorkflowExecutionPipelineBuilder> WorkflowExecutionPipeline { get; set; } = builder => builder
 12988        .UseExceptionHandling()
 12989        .UseDefaultActivityScheduler();
 90
 91    /// <summary>
 92    /// A delegate to configure the <see cref="IActivityExecutionPipeline"/>.
 93    /// </summary>
 74994    public Action<IActivityExecutionPipelineBuilder> ActivityExecutionPipeline { get; set; } = builder => builder
 12995        .UseLogging()
 12996        .UseExceptionHandling()
 12997        .UseExecutionLogging()
 12998        .UseNotifications()
 12999        .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    {
 250115        StandardOutStreamProvider = provider;
 250116        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    {
 128151        WorkflowExecutionPipeline = setup;
 128152        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    {
 128160        ActivityExecutionPipeline = setup;
 128161        return this;
 162    }
 163
 164    /// <inheritdoc />
 165    public override void Apply()
 166    {
 129167        AddElsaCore(Services);
 129168    }
 169
 170    private void AddElsaCore(IServiceCollection services)
 171    {
 129172        services.Configure<SerializationTypeOptions>(options =>
 129173        {
 129174            options.AddTypeAlias<ExceptionState>(nameof(ExceptionState));
 129175            options.AddTypeAlias<FaultException>(nameof(FaultException));
 129176            options.AddTypeAlias<VariablesDictionary>(nameof(VariablesDictionary));
 129177            options.AddTypeAlias<Token>(nameof(Token));
 129178            options.RegisterLegacyTypeName(typeof(FlowJoinMode), "Elsa.Workflows.Core.Activities.Flowchart.Models.FlowJo
 129179            options.AddTypeAliasWithLegacyName<FlowJoinMode>(nameof(FlowJoinMode));
 129180            options.AddTypeAliasWithLegacyName<WorkflowStorageDriver>(nameof(WorkflowStorageDriver));
 129181            options.AddTypeAliasWithLegacyName<WorkflowInstanceStorageDriver>(nameof(WorkflowInstanceStorageDriver));
 129182            options.AddTypeAliasWithLegacyName<MemoryStorageDriver>(nameof(MemoryStorageDriver));
 129183            options.AddTypeAliasWithLegacyName<FaultStrategy>(nameof(FaultStrategy));
 129184            options.AddTypeAliasWithLegacyName<ContinueWithIncidentsStrategy>(nameof(ContinueWithIncidentsStrategy));
 129185            options.AddTypeAlias<Exception>(nameof(Exception));
 129186            options.AddTypeAlias<ArgumentException>(nameof(ArgumentException));
 129187            options.AddTypeAlias<ArgumentNullException>(nameof(ArgumentNullException));
 129188            options.AddTypeAlias<InvalidOperationException>(nameof(InvalidOperationException));
 129189            options.AddTypeAlias<NullReferenceException>(nameof(NullReferenceException));
 129190            options.AddTypeAlias<OperationCanceledException>(nameof(OperationCanceledException));
 129191            options.AddTypeAlias<TaskCanceledException>(nameof(TaskCanceledException));
 129192            options.AddTypeAlias<TimeoutException>(nameof(TimeoutException));
 129193            options.AddTypeAlias<NotSupportedException>(nameof(NotSupportedException));
 129194            options.AddTypeAlias<JObject>(nameof(JObject));
 129195            options.AddTypeAlias<JArray>(nameof(JArray));
 258196        });
 197
 129198        services
 129199
 129200            // Core.
 129201            .AddScoped<IActivityInvoker, ActivityInvoker>()
 129202            .AddScoped<IWorkflowRunner, WorkflowRunner>()
 129203            .AddScoped<IActivityTestRunner, ActivityTestRunner>()
 129204            .AddScoped<IActivityVisitor, ActivityVisitor>()
 129205            .AddScoped<IIdentityGraphService, IdentityGraphService>()
 129206            .AddScoped<IWorkflowGraphBuilder, WorkflowGraphBuilder>()
 129207            .AddScoped<IWorkflowStateExtractor, WorkflowStateExtractor>()
 129208            .AddScoped<IActivitySchedulerFactory, ActivitySchedulerFactory>()
 129209            .AddSingleton<IWorkflowExecutionContextSchedulerStrategy, WorkflowExecutionContextSchedulerStrategy>()
 129210            .AddSingleton<IActivityExecutionContextSchedulerStrategy, ActivityExecutionContextSchedulerStrategy>()
 129211            .AddScoped(CommitStateHandler)
 129212            .AddSingleton<IHasher, Hasher>()
 129213            .AddSingleton<IStimulusHasher, StimulusHasher>()
 129214            .AddSingleton(IdentityGenerator)
 0215            .AddSingleton<IBookmarkPayloadSerializer>(sp => ActivatorUtilities.CreateInstance<BookmarkPayloadSerializer>
 129216            .AddSingleton<IActivityDescriber, ActivityDescriber>()
 129217            .AddSingleton<IActivityRegistry, ActivityRegistry>()
 129218            .AddScoped<IActivityRegistryLookupService, ActivityRegistryLookupService>()
 129219            .AddSingleton<IPropertyDefaultValueResolver, PropertyDefaultValueResolver>()
 129220            .AddSingleton<IPropertyUIHandlerResolver, PropertyUIHandlerResolver>()
 129221            .AddSingleton<IActivityFactory, ActivityFactory>()
 129222            .AddTransient<WorkflowBuilder>()
 4361223            .AddScoped(typeof(Func<IWorkflowBuilder>), sp => () => sp.GetRequiredService<WorkflowBuilder>())
 129224            .AddScoped<IWorkflowBuilderFactory, WorkflowBuilderFactory>()
 129225            .AddScoped<IVariablePersistenceManager, VariablePersistenceManager>()
 129226            .AddScoped<IIncidentStrategyResolver, DefaultIncidentStrategyResolver>()
 129227            .AddScoped<IActivityStateFilterManager, DefaultActivityStateFilterManager>()
 129228            .AddScoped<IWorkflowInstanceVariableReader, DefaultWorkflowInstanceVariableReader>()
 129229            .AddScoped<IWorkflowInstanceVariableWriter, DefaultWorkflowInstanceVariableWriter>()
 129230            .AddScoped<DefaultActivityInputEvaluator>()
 129231
 129232            // Incident Strategies.
 129233            .AddTransient<IIncidentStrategy, FaultStrategy>()
 129234            .AddTransient<IIncidentStrategy, ContinueWithIncidentsStrategy>()
 129235
 129236            // Pipelines.
 492237            .AddScoped<IActivityExecutionPipeline>(sp => new ActivityExecutionPipeline(sp, builder =>
 492238            {
 492239                builder.UseActivityExecutionPipelineContributors(sp);
 492240                ActivityExecutionPipeline(builder);
 984241            }))
 492242            .AddScoped<IWorkflowExecutionPipeline>(sp => new WorkflowExecutionPipeline(sp, builder =>
 492243            {
 496244                builder.UseWorkflowExecutionPipelineContributors(sp);
 496245                WorkflowExecutionPipeline(builder);
 988246            }))
 129247
 129248            // Built-in activity services.
 129249            .AddScoped<IActivityResolver, PropertyBasedActivityResolver>()
 129250            .AddScoped<IActivityResolver, SwitchActivityResolver>()
 129251            .AddSerializationOptionsConfigurator<AdditionalConvertersConfigurator>()
 129252            .AddSerializationOptionsConfigurator<CustomConstructorConfigurator>()
 129253
 129254            // Domain event handlers.
 129255            .AddHandlersFrom<WorkflowsFeature>()
 129256
 129257            // Stream providers.
 129258            .AddScoped(StandardInStreamProvider)
 129259            .AddScoped(StandardOutStreamProvider)
 129260
 129261            // Storage drivers.
 129262            .AddScoped<IStorageDriverManager, StorageDriverManager>()
 129263            .AddStorageDriver<WorkflowStorageDriver>()
 129264            .AddStorageDriver<WorkflowInstanceStorageDriver>()
 129265            .AddStorageDriver<MemoryStorageDriver>()
 129266
 129267            // Serialization.
 129268            .AddSingleton<ISerializationTypeRegistry, SerializationTypeRegistry>()
 129269            .AddSingleton<IWorkflowStateSerializer, JsonWorkflowStateSerializer>()
 129270            .AddSingleton<IPayloadSerializer, JsonPayloadSerializer>()
 129271            .AddSingleton<IActivitySerializer, JsonActivitySerializer>()
 129272            .AddSingleton<IApiSerializer, ApiSerializer>()
 129273            .AddSingleton<ISafeSerializer, SafeSerializer>()
 129274            .AddSingleton<IJsonSerializer, StandardJsonSerializer>()
 129275            .AddSingleton<SyntheticPropertiesWriter>()
 129276            .AddSingleton<ActivityWriter>()
 129277
 129278            // Instantiation strategies.
 129279            .AddScoped<IWorkflowActivationStrategy, AllowAlwaysStrategy>()
 129280
 129281            // UI.
 129282            .AddScoped<IUIHintHandler, DropDownUIHintHandler>()
 129283            .AddScoped<IUIHintHandler, CheckListUIHintHandler>()
 129284            .AddScoped<IUIHintHandler, RadioListUIHintHandler>()
 129285            .AddScoped<IUIHintHandler, JsonEditorUIHintHandler>()
 129286            .AddScoped<IPropertyUIHandler, StaticCheckListOptionsProvider>()
 129287            .AddScoped<IPropertyUIHandler, StaticRadioListOptionsProvider>()
 129288            .AddScoped<IPropertyUIHandler, StaticDropDownOptionsProvider>()
 129289            .AddScoped<IPropertyUIHandler, JsonCodeOptionsProvider>()
 129290            .AddScoped<DictionaryValueEvaluator>()
 129291            .AddSingleton<IActivityDescriptorModifier, DictionaryUIHintInputModifier>()
 129292
 129293            // Logger state generators.
 129294            .AddSingleton(WorkflowLoggerStateGenerator)
 129295            .AddSingleton(ActivityLoggerStateGenerator)
 129296
 129297            // Log Persistence Strategies.
 129298            .AddScoped<ILogPersistenceStrategyService, DefaultLogPersistenceStrategyService>()
 129299            .AddScoped<ILogPersistenceStrategy, Include>()
 129300            .AddScoped<ILogPersistenceStrategy, Exclude>()
 129301            .AddScoped<ILogPersistenceStrategy, Inherit>()
 129302            .AddScoped<ILogPersistenceStrategy, Configuration>()
 129303
 129304            // Logging
 129305            .AddLogging();
 129306    }
 307}