Elsa Core  之 Correlation


Correlation 关联

流程定义可以Create出很多个Instance

当使用Signal或者Http的时候,如何调起指定的Instance?

这里可以用到两个东西,一个是InstanceId,另外一个就是CorrelationId

CorrelationId可以认为是一个自定义的的InstanceId

Signal中使用:

参照 Elsa Core SendSignal - 编程代码 (cscoder.cn)

Http中使用

流程定义

/// <summary>
/// Triggered when POST HTTP requests are made to /register and continued when a subsequent GET HTTP request is made to /confirm to confirm the registration (see workflows.http).
/// </summary>
public class RegistrationWorkflow : IWorkflow
{
    public void Build(IWorkflowBuilder builder)
    {
        builder
            // Configure a Receive HTTP Request trigger that executes on incoming HTTP POST requests.
            .HttpEndpoint(activity => activity.WithPath("/register").WithMethod(HttpMethods.Post).WithTargetType<Registration>())
                
            // Store the registration as a workflow variable for easier access.
            .SetVariable(context => (Registration)((HttpRequestModel)(context.Input))?.Body)
                
            // 使用传入的Email做为关联Id
            .Correlate(context => context.GetVariable<Registration>()!.Email)

            // Write an HTTP response with a hyperlink to continue the workflow (notice the presence of the "correlation" query string parameter). 
            .WriteHttpResponse(activity => activity
                .WithStatusCode(HttpStatusCode.OK)
                .WithContentType("text/html")
                .WithContent(context =>
                {
                    var registration = context.GetVariable<Registration>()!;
                    return $"Welcome onboard, {registration.Name}! Please <a href=\"http://localhost:8201/confirm?correlation={registration.Email}\">confirm your registration</a>";
                }))
                
            // Configure another Receive HTTP Request trigger that executes on incoming HTTP GET requests.
            // 因为上面设置了Correlate,所以这里必须要传入Correlate才可以触发
            .HttpEndpoint(activity => activity.WithPath("/confirm"))
                
            // Write an HTTP response with a thank-you note.
            // Notice that the correct workflow instance is resumed base on the incoming correlation ID.
            .WriteHttpResponse(activity => activity
                .WithStatusCode(HttpStatusCode.OK)
                .WithContentType("text/html")
                .WithContent(
                    context =>
                    {
                        var registration = context.GetVariable<Registration>();
                        return $"Thanks for confirming your registration, {registration.Name}!";    
                    }));
    }
}

传入CorrelateId进行触发

###
# Confirm John's registration
GET http://localhost:8201/confirm?correlation=john@gmail.com
Content-Type: application/json

关键字: elsa