存储工作流活动数据在发布时

huangapple go评论86阅读模式
英文:

Store Workflow Activity Data When Publishing

问题

我需要在用户发布elsa中的工作流时,将特定的活动数据存储到数据库中的另一个集合中。

我找不到任何文档,请建议一些资源或建议以实现此目标。我尝试使用中间件来实现这一点。中间件的代码如下:

namespace WorkFlowV3
{
    // 你可能需要将Microsoft.AspNetCore.Http.Abstractions包安装到项目中
    public class CustomMiddleware
    {
        private readonly RequestDelegate _next;
        static HttpClient client = new HttpClient();
        public CustomMiddleware(RequestDelegate next)
        {
            _next = next;
        }

        public async Task Invoke(HttpContext httpContext)
        {
            // 在这里编写自定义逻辑....
            client.BaseAddress = new Uri("#");
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(
                new MediaTypeWithQualityHeaderValue("application/json"));

            string path = "/api/test-middleware-call";
            HttpResponseMessage response = await client.GetAsync(path);

            await _next(httpContext);
        }
    }

    // 用于将中间件添加到HTTP请求管道的扩展方法。
    public static class CustomMiddlewareExtensions
    {
        public static IApplicationBuilder UseCustomMiddleware(this IApplicationBuilder builder)
        {
            return builder.UseMiddleware<CustomMiddleware>();
        }
    }
}

但在这个过程中,我无法获取特定的活动数据。

英文:

I Need to store a specific activity data in another collection in database whenever a user publish a workflow in elsa.

I dont find any documentation, Please suggest me some resource or suggestion to achieve this. I have try to implement this with middleware. The Middleware code is

namespace WorkFlowV3
{
    // You may need to install the Microsoft.AspNetCore.Http.Abstractions package into your project
    public class CustomMiddleware
    {
        private readonly RequestDelegate _next;
        static HttpClient client = new HttpClient();
        public CustomMiddleware(RequestDelegate next)
        {
            _next = next;
        }

        public async Task Invoke(HttpContext httpContext)
        {
            //Write Custom Logic Here....
            client.BaseAddress = new Uri(&quot;#&quot;);
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(
                new MediaTypeWithQualityHeaderValue(&quot;application/json&quot;));

            string path = &quot;/api/test-middleware-call&quot;;
            HttpResponseMessage response = await client.GetAsync(path);

            await _next(httpContext);
        }
    }

    // Extension method used to add the middleware to the HTTP request pipeline.
    public static class CustomMiddlewareExtensions
    {
        public static IApplicationBuilder UseCustomMiddleware(this IApplicationBuilder builder)
        {
            return builder.UseMiddleware&lt;CustomMiddleware&gt;();
        }
    }
}

But in this process, I cant fetch the specific activity data.

答案1

得分: 2

以下是已翻译的内容:

最简单的将信息存储在您自己的数据库中以响应“workflow published”事件的方法是实现一个处理WorkflowDefinitionPublished通知的通知处理程序(使用MediatR)。

例如:

public class MyWorkflowPublishedhandler : INotificationhandler<WorkflowDefinitionPublished>
{
   private readonly IMyDatabaseStore _someRepository;

   public MyWorkflowPublishedhandler(IMyDatabaseStore someRepository)
   {
      _someRepository = someRepository;
   }

   public async Task Handle(WorkflowDefinitionPublished notification, CancellationToken cancellationToken)
   {
      var workflowDefinition = notification.WorkflowDefinition;

     // 执行您的逻辑来执行某项操作。
   }
}

要注册此处理程序,在您的 Startup 或 Program 类中,添加以下代码:

services.AddNotificationHandler<MyWorkflowPublishedhandler>();

每当工作流被发布时,您的处理程序将被调用。

英文:

The easiest way to store information in your own DB in response to the "workflow published" event is by implementing a notification handler (from MediatR) that handles the WorkflowDefinitionPublished notification.

For example:

public class MyWorkflowPublishedhandler : INotificationhandler&lt;WorkflowDefinitionPublished&gt;
{
   private readonly IMyDatabaseStore _someRepository;


   public MyWorkflowPublishedhandler(IMyDatabaseStore someRepository)
   {
      _someRepository = someRepository;
   }

   public async Task Handle(WorkflowDefinitionPublished notification, CancellationToken cancellationToken)
   {
      var workflowDefinition = notification.WorkflowDefinition;

     // Your logic to do a thing.
   }

}

To register this handler, from your Startup or Program class, add the following code:

services.AddNotificationHandler&lt;MyWorkflowPublishedhandler&gt;();

Your handler will be invoked every time a workflow gets published.

huangapple
  • 本文由 发表于 2023年2月10日 13:57:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/75407429.html
  • elsa-workflows
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定