elsa-core/src/modules/Elsa.Workflows.Management/Extensions/ManagementServiceCollectionExtensions.cs
Sipke Schoorstra 11fec1c85d
Add shell middleware, call‑stack tracking, and workflow reference graph APIs (#7333)
* refactor(deps): use local CShells project refs

Replace CShells NuGet package references with direct project references to the local CShells source to enable developing and testing against local changes and simplify build integration across modules.

* Handle assembly load errors in feature discovery

Added error handling for assembly load failures in feature discovery to improve resilience. Also updated configuration for identity token options and removed unused service bus consumer dependencies. Simplified project structure by moving and cleaning up `Directory.Build.targets` files.

* Refactor configuration and service extension methods.

Moved `ShellSettingsExtensions` and `ShellConfiguration` to `CShells.Abstractions` for better modularity. Added new `ServiceCollectionFeatureExtensions` to improve options registration. Updated appsettings and references to support these changes.

* Introduce ManagementServiceCollectionExtensions to streamline activity and variable registration

Added `ManagementServiceCollectionExtensions` for registering Elsa activity types and variable descriptors, providing a modular and shell-feature-compatible approach to configuration. Updated relevant features to utilize these new extension methods, enhancing code modularity and reducing redundancy.

* Add resilience strategy registration to HTTP feature

Introduced `ResilienceServiceCollectionExtensions` to register resilience strategies within the `Elsa.Resilience.Core` module. Updated `HttpFeature` to incorporate resilience strategies, enhancing HTTP-related resilience configuration leveraging the new extension methods.

* Add new configuration options to JavaScriptFeature

Implemented multiple properties in `JavaScriptFeature` to enhance JavaScript execution: `AllowClrAccess`, `AllowConfigurationAccess`, `ScriptCacheTimeout`, `DisableWrappers`, and `DisableVariableCopying`. These additions enable more flexible and secure configuration of the Jint JavaScript engine.

* refactor(workflows): unify graph caching

Resolve workflow definitions first and store graphs under stable per-version-ID cache keys so different lookup paths share entries.
Centralize cache creation and change-token registration to remove duplicated caching logic.
Skip materializer-unavailable definitions to avoid caching null graphs and simplify flow.

* refactor(tests): centralize default IDs and materializer setup

Introduce constants for default definition and version IDs, and materializer name. Refactor tests to use these constants, streamline graph and definition resolution, and improve cache key creation by sharing logic across tests. Extend tests to check scenarios with unavailable materializers, ensuring caching only occurs for valid cases.

* extend(tests): enhance cache key verification in AutoUpdateTests

Added checks for both workflow definition and version cache keys in AutoUpdateTests to ensure comprehensive cache validation, improving test reliability and coverage.

* refactor(projects): update CShells project paths and solution configuration

Revised project reference paths in `Elsa.ModularServer.Web.csproj` for CShells projects and updated `Elsa.sln` to include new CShells projects, streamlining project organization and build configuration.

* Add `IWorkflowReferenceGraphBuilder` to `WorkflowManagementFeature`; rename `ResilienceShellFeature` to `ResilienceFeature`.

* Refactor `HttpFeature` to use `IMiddlewareShellFeature`, include `HttpWorkflowsMiddleware`, and update `HttpActivityOptions` defaults.

* Add `AddTypeAlias` and `AddVariableTypeAndAlias` extension methods to service collections

- Introduced `AddTypeAlias<T>` method in `ServiceCollectionExtensions.cs` for adding type aliases.
- Added `AddVariableTypeAndAlias<T>` method in `ManagementServiceCollectionExtensions.cs` to add variable types with aliases.

* Update CShells package versions to 0.0.11 and replace ProjectReferences with PackageReferences in project files
2026-02-28 21:16:04 +01:00

105 lines
4.4 KiB
C#

using System.Reflection;
using Elsa.Expressions.Extensions;
using Elsa.Workflows.Management.Models;
using Elsa.Workflows.Management.Options;
using Microsoft.Extensions.DependencyInjection;
// ReSharper disable once CheckNamespace
namespace Elsa.Workflows.Management.Extensions;
/// <summary>
/// <see cref="IServiceCollection"/> extension methods for registering Elsa activity types
/// and variable descriptors via <see cref="ManagementOptions"/>.
/// </summary>
/// <remarks>
/// These extensions are the shell-feature-compatible replacement for calling
/// <c>WorkflowManagementFeature.AddActivitiesFrom&lt;T&gt;()</c> in the old-style feature system.
/// Because <c>services.Configure&lt;ManagementOptions&gt;</c> is additive, multiple features
/// can independently register activities without any coupling to each other.
/// </remarks>
public static class ManagementServiceCollectionExtensions
{
// -------------------------------------------------------------------------
// Activities
// -------------------------------------------------------------------------
/// <summary>
/// Registers the supplied activity <paramref name="types"/> with <see cref="ManagementOptions"/>.
/// </summary>
public static IServiceCollection AddActivities(this IServiceCollection services, IEnumerable<Type> types) =>
services.Configure<ManagementOptions>(options =>
{
foreach (var type in types)
options.ActivityTypes.Add(type);
});
/// <summary>
/// Registers a single activity type <typeparamref name="TActivity"/> with <see cref="ManagementOptions"/>.
/// </summary>
public static IServiceCollection AddActivity<TActivity>(this IServiceCollection services)
where TActivity : IActivity =>
services.AddActivities([typeof(TActivity)]);
/// <summary>
/// Scans <paramref name="assembly"/> and registers every concrete, non-generic
/// <see cref="IActivity"/> implementation found.
/// </summary>
public static IServiceCollection AddActivitiesFrom(this IServiceCollection services, Assembly assembly)
{
var types = assembly.GetExportedTypes()
.Where(t => typeof(IActivity).IsAssignableFrom(t)
&& t is { IsAbstract: false, IsInterface: false, IsGenericTypeDefinition: false });
return services.AddActivities(types);
}
/// <summary>
/// Scans the assembly that contains <typeparamref name="TMarker"/> and registers every
/// concrete, non-generic <see cref="IActivity"/> implementation found.
/// </summary>
public static IServiceCollection AddActivitiesFrom<TMarker>(this IServiceCollection services) =>
services.AddActivitiesFrom(typeof(TMarker).Assembly);
// -------------------------------------------------------------------------
// Variable descriptors
// -------------------------------------------------------------------------
/// <summary>
/// Registers the supplied <paramref name="descriptors"/> with <see cref="ManagementOptions"/>.
/// </summary>
public static IServiceCollection AddVariableDescriptors(
this IServiceCollection services,
IEnumerable<VariableDescriptor> descriptors) =>
services.Configure<ManagementOptions>(options =>
{
foreach (var descriptor in descriptors)
options.VariableDescriptors.Add(descriptor);
});
/// <summary>
/// Registers a single variable descriptor.
/// </summary>
public static IServiceCollection AddVariableDescriptor(
this IServiceCollection services,
VariableDescriptor descriptor) =>
services.AddVariableDescriptors([descriptor]);
/// <summary>
/// Registers a variable descriptor for <typeparamref name="T"/> with the given
/// <paramref name="category"/> and optional <paramref name="description"/>.
/// </summary>
public static IServiceCollection AddVariableDescriptor<T>(
this IServiceCollection services,
string category,
string? description = null) =>
services.AddVariableDescriptor(new(typeof(T), category, description));
/// <summary>
/// Adds a variable type and its alias to the specified service collection.
/// </summary>
public static IServiceCollection AddVariableTypeAndAlias<T>(this IServiceCollection services, string alias, string category)
{
return services
.AddVariableDescriptor<T>(category)
.AddTypeAlias<T>(alias);
}
}