Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I config the specific implementation of IActivityExecutionMiddleware in Program.cs,but have a Exception. #5118

Closed
coolshun opened this issue Mar 21, 2024 · 2 comments

Comments

@coolshun
Copy link

I config the specific implementation of IActivityExecutionMiddleware in Program.cs,but throw a Exception.the code and Exception bellow.
`
public class TestActivityMiddleware : IActivityExecutionMiddleware
{
private readonly ActivityMiddlewareDelegate _next;

  /// <summary>
  /// Constructor
  /// </summary>
  public TestActivityMiddleware(ActivityMiddlewareDelegate next)
  {
      _next = next;
  }

  /// <inheritdoc />
  public async ValueTask InvokeAsync(ActivityExecutionContext context)
  {
      Console.WriteLine($"============================Before Activity Execution: Id:{context.Id}  NodeId:{context.NodeId} Status:{context.Status} Activity.Id:{context.Activity.Id}   Activity.Name:{context.Activity.Name}");
      // Invoke next middleware.
      await _next(context);
      Console.WriteLine($"=============================After Activity Execution: Id:{context.Id}  NodeId:{context.NodeId} Status:{context.Status} Activity.Id:{context.Activity.Id}   Activity.Name:{context.Activity.Name}");
  }

}

//Program.cs
//this throw a Exception
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddScoped<IActivityExecutionMiddleware, TestActivityMiddleware>();

//Exception:
System.AggregateException:“Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: Elsa.Workflows.Contracts.IActivityExecutionMiddleware Lifetime: Scoped ImplementationType: ElsaServer.Middleware.TestActivityMiddleware': Unable to resolve service for type 'Elsa.Workflows.Pipelines.ActivityExecution.ActivityMiddlewareDelegate' while attempting to activate 'ElsaServer.Middleware.TestActivityMiddleware'.)”
`

Copy link

glime-ai bot commented Mar 21, 2024

To resolve the issue you're encountering with the TestActivityMiddleware implementation, it's important to understand that the ActivityMiddlewareDelegate is a delegate type, not a service that can be resolved from the dependency injection container. This delegate represents the next component in the middleware pipeline and is provided by the framework when the middleware is invoked.

In Elsa, middleware components are not registered directly with the dependency injection container. Instead, they are added to the middleware pipeline using extension methods provided by Elsa.

To add your custom middleware to the pipeline, you should use the UseMiddleware extension method on the IActivityExecutionBuilder during the Elsa services configuration in your Program.cs. Here's an example of how you can do this:

var builder = WebApplication.CreateBuilder(args);

// Configure Elsa services.
builder.Services.AddElsa(elsa => {
    // ... other Elsa configurations ...

    // Add your custom activity execution middleware to the pipeline.
    elsa.UseActivityExecution(builder => builder.UseMiddleware<TestActivityMiddleware>());
});

// ... rest of the Program.cs code ...

In this example, UseMiddleware<TestActivityMiddleware>() is an extension method that you would need to create to add your TestActivityMiddleware to the activity execution pipeline. The UseMiddleware method would look something like this:

public static class ActivityExecutionBuilderExtensions
{
    public static IActivityExecutionBuilder UseMiddleware<TMiddleware>(this IActivityExecutionBuilder builder)
        where TMiddleware : class, IActivityExecutionMiddleware
    {
        // Add the middleware to the pipeline.
        builder.Use(next => new TMiddleware(next).InvokeAsync);
        return builder;
    }
}

This extension method adds your middleware to the pipeline and ensures that the ActivityMiddlewareDelegate representing the next component is properly provided.

Please note that the above code is a general example and might need to be adjusted based on the specific version of Elsa you are using and the context of your application.

If you need further assistance, please provide more details about your Elsa version and the context in which you are trying to add the middleware.

@sfmskywalker
Copy link
Member

Answered by bot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants