| | | 1 | | using System.Diagnostics.CodeAnalysis; |
| | | 2 | | using System.Reflection; |
| | | 3 | | using Elsa.Extensions; |
| | | 4 | | using Elsa.Workflows; |
| | | 5 | | using Elsa.Workflows.Options; |
| | | 6 | | using Elsa.Workflows.Runtime; |
| | | 7 | | using Elsa.Workflows.Runtime.Contracts; |
| | | 8 | | using Elsa.Workflows.Runtime.Discovery; |
| | | 9 | | using Elsa.Workflows.Runtime.Options; |
| | | 10 | | using Elsa.Workflows.Runtime.Providers; |
| | | 11 | | using Microsoft.Extensions.DependencyInjection.Extensions; |
| | | 12 | | using Microsoft.Extensions.Options; |
| | | 13 | | using Elsa.Common.Serialization; |
| | | 14 | | |
| | | 15 | | // ReSharper disable once CheckNamespace |
| | | 16 | | namespace Microsoft.Extensions.DependencyInjection; |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// Provides extension methods for <see cref="IServiceCollection"/>. |
| | | 20 | | /// </summary> |
| | | 21 | | public static class DependencyInjectionExtensions |
| | | 22 | | { |
| | | 23 | | /// <summary> |
| | | 24 | | /// Registers and validates <see cref="GracefulShutdownOptions"/>. |
| | | 25 | | /// </summary> |
| | | 26 | | public static IServiceCollection AddGracefulShutdownOptions(this IServiceCollection services, Action<GracefulShutdow |
| | | 27 | | { |
| | 229 | 28 | | var builder = services.AddOptions<GracefulShutdownOptions>().ValidateOnStart(); |
| | 229 | 29 | | services.TryAddEnumerable(ServiceDescriptor.Singleton<IValidateOptions<GracefulShutdownOptions>, Elsa.Extensions |
| | | 30 | | |
| | 229 | 31 | | if (configure != null) |
| | 14 | 32 | | builder.Configure(configure); |
| | | 33 | | |
| | | 34 | | |
| | 229 | 35 | | return services; |
| | | 36 | | } |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// Adds the specified workflow provider type to the service collection. |
| | | 40 | | /// </summary> |
| | | 41 | | /// <typeparam name="T">The type of the workflow provider to add. Must implement <see cref="IWorkflowsProvider"/>.</ |
| | | 42 | | [Obsolete("Use AddWorkflowsProvider instead.", false)] |
| | 0 | 43 | | public static IServiceCollection AddWorkflowDefinitionProvider<T>(this IServiceCollection services) where T : class, |
| | | 44 | | /// <summary> |
| | | 45 | | /// Registers a <see cref="ITriggerPayloadValidator{TPayload}"/> with the service container. |
| | | 46 | | /// </summary> |
| | | 47 | | /// <param name="services">Service collection</param> |
| | | 48 | | /// <typeparam name="TValidator">Validator of the validator</typeparam> |
| | | 49 | | /// <typeparam name="TPayload">Payload type</typeparam> |
| | | 50 | | public static IServiceCollection AddTriggerPayloadValidator<TValidator, TPayload>(this IServiceCollection services) |
| | | 51 | | where TValidator : class, ITriggerPayloadValidator<TPayload> |
| | | 52 | | { |
| | 222 | 53 | | return services.AddScoped<ITriggerPayloadValidator<TPayload>, TValidator>(); |
| | | 54 | | } |
| | | 55 | | |
| | | 56 | | /// <summary> |
| | | 57 | | /// Adds the specified workflows provider type to the service collection. |
| | | 58 | | /// </summary> |
| | | 59 | | /// <typeparam name="T">The type of the workflows provider to add. Must implement <see cref="IWorkflowsProvider"/>.< |
| | 239 | 60 | | public static IServiceCollection AddWorkflowsProvider<T>(this IServiceCollection services) where T : class, IWorkflo |
| | | 61 | | |
| | | 62 | | /// <summary> |
| | | 63 | | /// Registers the specified code-first workflow type. |
| | | 64 | | /// </summary> |
| | 1 | 65 | | public static IServiceCollection AddWorkflow<T>(this IServiceCollection services) where T : IWorkflow => services.Ad |
| | | 66 | | |
| | | 67 | | /// <summary> |
| | | 68 | | /// Registers the specified code-first workflow type. |
| | | 69 | | /// </summary> |
| | | 70 | | public static IServiceCollection AddWorkflow(this IServiceCollection services, Type workflowType) |
| | | 71 | | { |
| | 1 | 72 | | AddWorkflowRegistration(services, workflowType); |
| | 1 | 73 | | return services; |
| | | 74 | | } |
| | | 75 | | |
| | | 76 | | /// <summary> |
| | | 77 | | /// Registers all code-first workflows contained in the assembly containing the specified marker type. |
| | | 78 | | /// </summary> |
| | | 79 | | [RequiresUnreferencedCode("The assembly is required to be referenced.")] |
| | 0 | 80 | | public static IServiceCollection AddWorkflowsFrom<TMarker>(this IServiceCollection services) => services.AddWorkflow |
| | | 81 | | |
| | | 82 | | /// <summary> |
| | | 83 | | /// Registers all code-first workflows in the specified assembly. |
| | | 84 | | /// </summary> |
| | | 85 | | [RequiresUnreferencedCode("The assembly is required to be referenced.")] |
| | | 86 | | public static IServiceCollection AddWorkflowsFrom(this IServiceCollection services, Assembly assembly) |
| | | 87 | | { |
| | 0 | 88 | | foreach (var workflowType in WorkflowTypeScanner.GetWorkflowTypes(assembly)) |
| | 0 | 89 | | AddWorkflowRegistration(services, workflowType); |
| | | 90 | | |
| | 0 | 91 | | return services; |
| | | 92 | | } |
| | | 93 | | |
| | | 94 | | private static void AddWorkflowRegistration(IServiceCollection services, Type workflowType) |
| | | 95 | | { |
| | 2 | 96 | | services.PostConfigure<RuntimeOptions>(options => options.Workflows.Add(workflowType)); |
| | 1 | 97 | | services.Configure<SerializationTypeOptions>(options => options.AddSimpleAssemblyQualifiedTypeAlias(workflowType |
| | 1 | 98 | | } |
| | | 99 | | } |