forked from danielgerlag/workflow-core
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathServiceCollectionExtensions.cs
More file actions
126 lines (109 loc) · 6.45 KB
/
ServiceCollectionExtensions.cs
File metadata and controls
126 lines (109 loc) · 6.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
using System;
using System.Linq;
using WorkflowCore.Interface;
using WorkflowCore.Services;
using WorkflowCore.Models;
using Microsoft.Extensions.ObjectPool;
using WorkflowCore.Primitives;
using WorkflowCore.Services.BackgroundTasks;
using WorkflowCore.Services.ErrorHandlers;
using WorkflowCore.Services.BackgroundTasks.RunnablePoller;
namespace Microsoft.Extensions.DependencyInjection
{
public static class ServiceCollectionExtensions
{
public static IServiceCollection AddWorkflow(this IServiceCollection services, Action<WorkflowOptions> setupAction = null)
{
if (services.Any(x => x.ServiceType == typeof(WorkflowOptions)))
throw new InvalidOperationException("Workflow services already registered");
var options = new WorkflowOptions(services);
setupAction?.Invoke(options);
services.AddSingleton<ISingletonMemoryProvider, MemoryPersistenceProvider>();
services.AddTransient<IPersistenceProvider>(options.PersistanceFactory);
services.AddTransient<IWorkflowRepository>(options.PersistanceFactory);
services.AddTransient<ISubscriptionRepository>(options.PersistanceFactory);
services.AddTransient<IEventRepository>(options.PersistanceFactory);
services.AddSingleton<IQueueProvider>(options.QueueFactory);
services.AddSingleton<IDistributedLockProvider>(options.LockFactory);
services.AddSingleton<ILifeCycleEventHub>(options.EventHubFactory);
services.AddSingleton<ISearchIndex>(options.SearchIndexFactory);
services.AddSingleton<IWorkflowRegistry, WorkflowRegistry>();
services.AddSingleton<WorkflowOptions>(options);
services.AddSingleton<ILifeCycleEventPublisher, LifeCycleEventPublisher>();
if (options.EnableWorkflows)
{
services.AddTransient<IBackgroundTask, WorkflowConsumer>();
}
if (options.EnableEvents)
{
services.AddTransient<IBackgroundTask, EventConsumer>();
}
if (options.EnableIndexes)
{
services.AddTransient<IBackgroundTask, IndexConsumer>();
}
if (options.EnablePolling)
{
services.AddTransient<IBackgroundTask, WorkflowRunnablePoller>();
services.AddTransient<IBackgroundTask, EventRunnablePoller>();
services.AddTransient<IBackgroundTask, CommandRunnablePoller>();
}
services.AddTransient<IBackgroundTask>(sp => sp.GetService<ILifeCycleEventPublisher>());
services.AddTransient<IWorkflowErrorHandler, CompensateHandler>();
services.AddTransient<IWorkflowErrorHandler, RetryHandler>();
services.AddTransient<IWorkflowErrorHandler, TerminateHandler>();
services.AddTransient<IWorkflowErrorHandler, SuspendHandler>();
services.AddSingleton<IGreyList, GreyList>();
services.AddSingleton<IWorkflowController, WorkflowController>();
services.AddSingleton<IActivityController, ActivityController>();
services.AddSingleton<IWorkflowHost, WorkflowHost>();
services.AddTransient<IStepExecutor, StepExecutor>();
services.AddTransient<IWorkflowMiddlewareErrorHandler, DefaultWorkflowMiddlewareErrorHandler>();
services.AddTransient<IWorkflowMiddlewareRunner, WorkflowMiddlewareRunner>();
services.AddTransient<IScopeProvider, ScopeProvider>();
services.AddTransient<IWorkflowExecutor, WorkflowExecutor>();
services.AddTransient<ICancellationProcessor, CancellationProcessor>();
services.AddTransient<IWorkflowBuilder, WorkflowBuilder>();
services.AddTransient<IDateTimeProvider, DateTimeProvider>();
services.AddTransient<IExecutionResultProcessor, ExecutionResultProcessor>();
services.AddTransient<IExecutionPointerFactory, ExecutionPointerFactory>();
services.AddTransient<IPooledObjectPolicy<IPersistenceProvider>, InjectedObjectPoolPolicy<IPersistenceProvider>>();
services.AddTransient<IPooledObjectPolicy<IWorkflowExecutor>, InjectedObjectPoolPolicy<IWorkflowExecutor>>();
services.AddTransient<ISyncWorkflowRunner, SyncWorkflowRunner>();
services.AddTransient<Foreach>();
return services;
}
/// <summary>
/// Adds a middleware that will run around the execution of a workflow step.
/// </summary>
/// <param name="services">The services collection.</param>
/// <param name="factory">Optionally configure using your own factory.</param>
/// <typeparam name="TMiddleware">The type of middleware.
/// It must implement <see cref="IWorkflowStepMiddleware"/>.</typeparam>
/// <returns>The services collection for chaining.</returns>
public static IServiceCollection AddWorkflowStepMiddleware<TMiddleware>(
this IServiceCollection services,
Func<IServiceProvider, TMiddleware> factory = null)
where TMiddleware : class, IWorkflowStepMiddleware =>
factory == null
? services.AddTransient<IWorkflowStepMiddleware, TMiddleware>()
: services.AddTransient<IWorkflowStepMiddleware, TMiddleware>(factory);
/// <summary>
/// Adds a middleware that will run either before a workflow is kicked off or after
/// a workflow completes. Specify the phase of the workflow execution process that
/// you want to execute this middleware using <see cref="IWorkflowMiddleware.Phase"/>.
/// </summary>
/// <param name="services">The services collection.</param>
/// <param name="factory">Optionally configure using your own factory.</param>
/// <typeparam name="TMiddleware">The type of middleware.
/// It must implement <see cref="IWorkflowMiddleware"/>.</typeparam>
/// <returns>The services collection for chaining.</returns>
public static IServiceCollection AddWorkflowMiddleware<TMiddleware>(
this IServiceCollection services,
Func<IServiceProvider, TMiddleware> factory = null)
where TMiddleware : class, IWorkflowMiddleware =>
factory == null
? services.AddTransient<IWorkflowMiddleware, TMiddleware>()
: services.AddTransient<IWorkflowMiddleware, TMiddleware>(factory);
}
}