elsa-core/samples/drop-ins/SampleDropIn/DropIn.cs

32 lines
1,021 B
C#
Raw Normal View History

using Elsa.DropIns.Core;
using Elsa.Extensions;
using Elsa.Features.Services;
Enh/5943 DropIns - Prevention of File Locks and Unloading (#5944) * fix - supressed warning IL2072 Changed DropInDescriptor from Record to Class to be able to add the suggested DynamicallyAccesedMembers decorator. * Add Unconfigure method to DropIn class and interface The `DropIn` class in `DropIn.cs` now includes a new `Unconfigure` method. This method handles the unconfiguration process when a drop-in is deleted. It retrieves the `ILogger<DropIn>` and `IActivityRegistry` services from the `IServiceProvider`, removes the `SampleActivity` from the activity registry, and logs the outcome. The `IDropIn` interface in `IDropIn.cs` has been updated to include the new `Unconfigure` method, ensuring that it is called when the drop-in is deleted. Additional using directives have been added to `DropIn.cs` for `Elsa.Workflows`, and `Microsoft.Extensions.Logging`. * Refactor AssemblyLoader to use memory stream for loading Modified the `LoadPath` method in the `AssemblyLoader` class to load assemblies from a memory stream instead of directly from the file. This change prevents file locking issues by copying the file contents to a memory stream and then loading the assembly from this stream. * Dispose packageReader after loading assembly Ensure the packageReader is disposed of after loading the assembly from the memory stream. This change adds a call to `packageReader.Dispose()` to close the package reader and the associated `.nupkg` file, which helps in releasing resources and preventing potential file locks or memory leaks. * Add drop-in unloading on file deletion Updated DropInDirectoryMonitorHostedService to handle unloading of drop-ins when files are deleted. Added `_debouncedUnloader` and `_installedDropIns` fields. Modified constructor to initialize these fields. Updated `ExecuteAsync` to call `LoadDropInAssemblyAsync` initially and handle file deletion events. Added `OnDeleted` method to manage file deletions and invoke the debounced unloader. Enhanced `LoadDropInAssemblyAsync` to track loaded drop-ins. Introduced `UnloadDropInAssemblyAsync` to remove drop-ins on file deletion. * Load assembly from MemoryStream instead of FileStream Modified the assembly loading process to use a MemoryStream instead of a FileStream. This change ensures that the FileStream is properly closed before the assembly is loaded, improving resource management and preventing potential file access issues. * Improve logging and error handling in DropIn services Removed logging from DropIn.Unconfigure method and added ILogger dependency to DropInDirectoryMonitorHostedService. Updated csproj to include Microsoft.Extensions.Logging.Abstractions. Enhanced error handling in UnloadDropInAssemblyAsync method. Minor formatting improvements for readability.
2024-09-05 16:09:31 +00:00
using Elsa.Workflows;
Add a more generic UIHandler to customize how inputAttributes can be handle by UI (#4688) * add a more generic UIHandler to customize how inputAttributes can be handle by the ui * Add IPropertyUIHandlerResolver and update PropertyUIHandlerResolver Introduced a new interface, IPropertyUIHandlerResolver, to resolve UI options for a property. Refactored PropertyUIHandlerResolver to implement this interface and removed the unnecessary partial class structure. Also, cleaned up some unnecessary usings in various files for better code organization. * Refactor variable name and description in InputDescriptor The 'uISpecifications' variable in the InputDescriptor model is renamed to 'uiSpecifications' for better readability. Additionally, the associated comment was revised to explain that the dictionary is used by the UI. * "Refactor codebase for improved organization and cleaner architecture" The codebase has been significantly refactored, moving several classes to more appropriate namespaces for improved organization and cleaner architecture. This includes shifting UI hint handlers, activities, and memory-related components, amongst others. The changes should improve code readability and maintainability, but as this is a broad refactoring effort, thorough regression testing is advised. * Add CheckList UIHint with associated handler and provider This update introduces a new UIHint called CheckList to the Elsa.Workflows.Core. This includes the necessary handler and provider classes. The handler is registered in the WorkflowsFeature.cs, and the CheckList UIHint key has been added to the InputUIHints.cs. Various associated files have been created in both the Elsa.Api.Client and Elsa.Workflows.Core project to support this new UIHint. --------- Co-authored-by: Jérémie DEVILLARD <jdevillard@users.noreply.github.com> Co-authored-by: Sipke Schoorstra <sipkeschoorstra@outlook.com>
2023-12-26 17:56:29 +00:00
using Elsa.Workflows.Contracts;
using JetBrains.Annotations;
using Microsoft.Extensions.DependencyInjection;
Enh/5943 DropIns - Prevention of File Locks and Unloading (#5944) * fix - supressed warning IL2072 Changed DropInDescriptor from Record to Class to be able to add the suggested DynamicallyAccesedMembers decorator. * Add Unconfigure method to DropIn class and interface The `DropIn` class in `DropIn.cs` now includes a new `Unconfigure` method. This method handles the unconfiguration process when a drop-in is deleted. It retrieves the `ILogger<DropIn>` and `IActivityRegistry` services from the `IServiceProvider`, removes the `SampleActivity` from the activity registry, and logs the outcome. The `IDropIn` interface in `IDropIn.cs` has been updated to include the new `Unconfigure` method, ensuring that it is called when the drop-in is deleted. Additional using directives have been added to `DropIn.cs` for `Elsa.Workflows`, and `Microsoft.Extensions.Logging`. * Refactor AssemblyLoader to use memory stream for loading Modified the `LoadPath` method in the `AssemblyLoader` class to load assemblies from a memory stream instead of directly from the file. This change prevents file locking issues by copying the file contents to a memory stream and then loading the assembly from this stream. * Dispose packageReader after loading assembly Ensure the packageReader is disposed of after loading the assembly from the memory stream. This change adds a call to `packageReader.Dispose()` to close the package reader and the associated `.nupkg` file, which helps in releasing resources and preventing potential file locks or memory leaks. * Add drop-in unloading on file deletion Updated DropInDirectoryMonitorHostedService to handle unloading of drop-ins when files are deleted. Added `_debouncedUnloader` and `_installedDropIns` fields. Modified constructor to initialize these fields. Updated `ExecuteAsync` to call `LoadDropInAssemblyAsync` initially and handle file deletion events. Added `OnDeleted` method to manage file deletions and invoke the debounced unloader. Enhanced `LoadDropInAssemblyAsync` to track loaded drop-ins. Introduced `UnloadDropInAssemblyAsync` to remove drop-ins on file deletion. * Load assembly from MemoryStream instead of FileStream Modified the assembly loading process to use a MemoryStream instead of a FileStream. This change ensures that the FileStream is properly closed before the assembly is loaded, improving resource management and preventing potential file access issues. * Improve logging and error handling in DropIn services Removed logging from DropIn.Unconfigure method and added ILogger dependency to DropInDirectoryMonitorHostedService. Updated csproj to include Microsoft.Extensions.Logging.Abstractions. Enhanced error handling in UnloadDropInAssemblyAsync method. Minor formatting improvements for readability.
2024-09-05 16:09:31 +00:00
using Microsoft.Extensions.Logging;
using SampleDropIn.Activities;
namespace SampleDropIn;
[PublicAPI]
public class DropIn : IDropIn
{
public void Install(IModule module)
{
module.AddActivitiesFrom<DropIn>();
}
public async ValueTask ConfigureAsync(IServiceProvider serviceProvider, CancellationToken cancellationToken)
{
var activityRegistry = serviceProvider.GetRequiredService<IActivityRegistry>();
await activityRegistry.RegisterAsync<SampleActivity>(cancellationToken: cancellationToken);
}
Enh/5943 DropIns - Prevention of File Locks and Unloading (#5944) * fix - supressed warning IL2072 Changed DropInDescriptor from Record to Class to be able to add the suggested DynamicallyAccesedMembers decorator. * Add Unconfigure method to DropIn class and interface The `DropIn` class in `DropIn.cs` now includes a new `Unconfigure` method. This method handles the unconfiguration process when a drop-in is deleted. It retrieves the `ILogger<DropIn>` and `IActivityRegistry` services from the `IServiceProvider`, removes the `SampleActivity` from the activity registry, and logs the outcome. The `IDropIn` interface in `IDropIn.cs` has been updated to include the new `Unconfigure` method, ensuring that it is called when the drop-in is deleted. Additional using directives have been added to `DropIn.cs` for `Elsa.Workflows`, and `Microsoft.Extensions.Logging`. * Refactor AssemblyLoader to use memory stream for loading Modified the `LoadPath` method in the `AssemblyLoader` class to load assemblies from a memory stream instead of directly from the file. This change prevents file locking issues by copying the file contents to a memory stream and then loading the assembly from this stream. * Dispose packageReader after loading assembly Ensure the packageReader is disposed of after loading the assembly from the memory stream. This change adds a call to `packageReader.Dispose()` to close the package reader and the associated `.nupkg` file, which helps in releasing resources and preventing potential file locks or memory leaks. * Add drop-in unloading on file deletion Updated DropInDirectoryMonitorHostedService to handle unloading of drop-ins when files are deleted. Added `_debouncedUnloader` and `_installedDropIns` fields. Modified constructor to initialize these fields. Updated `ExecuteAsync` to call `LoadDropInAssemblyAsync` initially and handle file deletion events. Added `OnDeleted` method to manage file deletions and invoke the debounced unloader. Enhanced `LoadDropInAssemblyAsync` to track loaded drop-ins. Introduced `UnloadDropInAssemblyAsync` to remove drop-ins on file deletion. * Load assembly from MemoryStream instead of FileStream Modified the assembly loading process to use a MemoryStream instead of a FileStream. This change ensures that the FileStream is properly closed before the assembly is loaded, improving resource management and preventing potential file access issues. * Improve logging and error handling in DropIn services Removed logging from DropIn.Unconfigure method and added ILogger dependency to DropInDirectoryMonitorHostedService. Updated csproj to include Microsoft.Extensions.Logging.Abstractions. Enhanced error handling in UnloadDropInAssemblyAsync method. Minor formatting improvements for readability.
2024-09-05 16:09:31 +00:00
public void Unconfigure(IServiceProvider serviceProvider)
{
var activityRegistry = serviceProvider.GetRequiredService<IActivityRegistry>();
activityRegistry.Remove(typeof(ActivityRegistry), activityRegistry.Find<SampleActivity>()!);
}
}