| | | 1 | | using Elsa.Workflows.IncidentStrategies; |
| | | 2 | | using Elsa.Workflows.Options; |
| | | 3 | | using Microsoft.Extensions.DependencyInjection; |
| | | 4 | | using Microsoft.Extensions.Options; |
| | | 5 | | |
| | | 6 | | namespace Elsa.Workflows; |
| | | 7 | | |
| | | 8 | | /// <inheritdoc /> |
| | | 9 | | public class DefaultIncidentStrategyResolver : IIncidentStrategyResolver |
| | | 10 | | { |
| | | 11 | | private readonly IOptions<IncidentOptions> _options; |
| | | 12 | | private readonly IServiceProvider _serviceProvider; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Initializes a new instance of the <see cref="DefaultIncidentStrategyResolver"/> class. |
| | | 16 | | /// </summary> |
| | 421 | 17 | | public DefaultIncidentStrategyResolver(IOptions<IncidentOptions> options, IServiceProvider serviceProvider) |
| | | 18 | | { |
| | 421 | 19 | | _options = options; |
| | 421 | 20 | | _serviceProvider = serviceProvider; |
| | 421 | 21 | | } |
| | | 22 | | |
| | | 23 | | /// <inheritdoc /> |
| | | 24 | | public ValueTask<IIncidentStrategy> ResolveStrategyAsync(ActivityExecutionContext context, CancellationToken cancell |
| | | 25 | | { |
| | 13 | 26 | | var strategyType = ResolveStrategyType(context); |
| | 13 | 27 | | var strategy = (IIncidentStrategy)ActivatorUtilities.GetServiceOrCreateInstance(_serviceProvider, strategyType); |
| | | 28 | | |
| | 13 | 29 | | return new(strategy); |
| | | 30 | | } |
| | | 31 | | |
| | | 32 | | private Type ResolveStrategyType(ActivityExecutionContext context) |
| | | 33 | | { |
| | | 34 | | // First check if the workflow is configured with an incident strategy. |
| | | 35 | | // If no strategy was configured there, use the application-level configured strategy. |
| | | 36 | | |
| | 13 | 37 | | var strategyType = context.WorkflowExecutionContext.Workflow.Options.IncidentStrategyType; |
| | | 38 | | |
| | 13 | 39 | | if(strategyType == null) |
| | 8 | 40 | | strategyType = _options.Value.DefaultIncidentStrategy; |
| | | 41 | | |
| | 13 | 42 | | return strategyType ?? typeof(FaultStrategy); |
| | | 43 | | } |
| | | 44 | | } |