Question

I am building custom activities, First activity is containing ReceiveSendReply activity with some other activities. In this activity I am creating workflow instance. I want to assign a Intialized CorrelationHandle to the OutArgument. Which will be assigned to the outer shared varriable.

In another activity I have another ReceiveSendReply activity, which will resume the instance created by the above said activity. In second activity I have CorrelationHandle as InArgument. I will assign this InArgument with the outer shared correlationHandle varriable.

When I am executing this workflow service, first operation executes without any issue and it populate the correlation key and I am receiving that correlation key in the response. When I am executing the second activity using the received correlation key, service return with the following exception:

"The execution of an InstancePersistenceCommand was interrupted because the instance key '45f99690-2829-3126-0782-99033212633c' was not associated to an instance. This can occur because the instance or key has been cleaned up, or because the key is invalid. The key may be invalid if the message it was generated from was sent at the wrong time or contained incorrect correlation data."

I want to know how to share a CorrelationHandle between two custom activities by using OutArgument and InArguments?

public sealed class Create : Activity
{
    public Create()
    {
        Implementation = new Func<Activity>(CreateBody);
    }

    public string ServiceContract { get; set; }

    public string Operation { get; set; }

    [RequiredArgument]
    public OutArgument<CorrelationHandle> CorrelationHandle { get; set; }

    Activity CreateBody()
    {
        // variables declaration. 
        var requestContent = new Variable<string>();
        var operationResponseParam = new Variable<OperationResponseParam>();
        var correlationKey = new Variable<string>();
        var correlationHandle = new Variable<CorrelationHandle>();

        Receive receiveRequest;

        return new Sequence
        {
            Variables = { correlationHandle, correlationKey }, 
            Activities = 
            {
                {receiveRequest = new Receive()
                {
                    ServiceContractName = ServiceContract,
                    OperationName = Operation,
                    CanCreateInstance = true,
                    Content = new ReceiveMessageContent
                    {
                        Message = new OutArgument<string>(ctx => operationRequestParam.Get(ctx))
                    },
                }},

                new Assign<string>
                {
                    To = new OutArgument<string>(ctx => correlationKey.Get(ctx)),
                    Value = new InArgument<string>(ctx => Guid.NewGuid().ToString())
                },

                new Assign<ApplicationDataList>
                {
                    To = new ArgumentReference<ApplicationDataList>("AppData"),
                    Value = new InArgument<ApplicationDataList>(ctx =>appData.Get(ctx))
                },

                new Assign<OperationResponseParam>
                {
                    To = new OutArgument<OperationResponseParam>(ctx => operationResponseParam.Get(ctx)),
                    Value = new InArgument<OperationResponseParam>
                    {
                        Expression = new BuildResponse()
                        {
                            CorrelationKey = new InArgument<string>(correlationKey),
                        }
                    }
                },

                new SendReply
                {
                    Request = receiveRequest,
                    Content = new SendMessageContent
                    {
                        Message = new InArgument<OperationResponseParam>(ctx => operationResponseParam.Get(ctx))
                    },

                    CorrelationInitializers =
                    {
                        new QueryCorrelationInitializer
                        {
                            CorrelationHandle =  new InArgument<CorrelationHandle>(correlationHandle),
                            MessageQuerySet = new MessageQuerySet
                            {
                                {
                                    "CorrelationKey", new XPathMessageQuery("sm:header()/tempuri:CorrelationKey")
                                }
                            }
                        }
                    }
                },

                new Assign<CorrelationHandle>
                {
                    To = new ArgumentReference<CorrelationHandle>("CorrelationHandle"),
                    Value = new InArgument<CorrelationHandle>(ctx =>correlationHandle.Get(ctx))
                },
            }
        };
    }
}


public sealed class Wait : Activity
{
    public Wait()
    {
        Implementation = new Func<Activity>(CreateBody);
    }

    public string ServiceContract { get; set; }

    public string Operation { get; set; }

    [RequiredArgument]
    public InArgument<CorrelationHandle> CorrelationHandle { get; set; }

    Activity CreateBody()
    {
        // variables declaration. 

        var operationRequestParam = new Variable<OperationRequestParam>();
        var operationResponseParam = new Variable<OperationResponseParam>();
        var correlationHandle = new Variable<CorrelationHandle>();
        Receive receiveRequest;

        return new Sequence
        {
            Variables = { correlationHandle },
            Activities =
            {
                new Sequence
                {
                    Variables = {operationRequestParam, operationResponseParam },

                    Activities = 
                    {
                        {receiveRequest =new Receive()
                        {
                            ServiceContractName = ServiceContract,
                            OperationName = Operation,
                            CanCreateInstance = false,
                            CorrelatesWith = new InArgument<CorrelationHandle>(ctx => CorrelationHandle.Get(ctx)),
                            CorrelatesOn = new MessageQuerySet
                            {
                                { "CorrelationKey", new XPathMessageQuery("sm:header()/tempuri:CorrelationKey")}
                            },

                            //parameters for receive
                            Content = new ReceiveMessageContent
                            {
                                Message = new OutArgument<OperationRequestParam>(ctx => operationRequestParam.Get(ctx))
                            },
                        }},


                        new Assign<OperationResponseParam>
                        {
                            To = new OutArgument<OperationResponseParam>(operationResponseParam),
                            Value = new InArgument<OperationResponseParam>
                            {
                                Expression = new BuildResponse()
                                {
                                    ApplicationData = new InArgument<ApplicationDataList>(ctx => appData.Get(ctx)),
                                }
                            }
                        },

                        new SendReply
                        {
                            Request = receiveRequest,
                            Content = new SendMessageContent
                            {
                                Message = new InArgument<OperationResponseParam>(ctx => operationResponseParam.Get(ctx))
                            }
                        },
                    }
                },
            }
        };
    }
}
Was it helpful?

Solution

We should use the handle as an InArgument, and the object refereing the handle will be modified by InitializeCorrelation. So in my both activities I have to use InArgument instead of OutArgument in the first and InArgument in the second.

public sealed class Create : Activity
{
    public Create()
    {
        Implementation = new Func<Activity>(CreateBody);
    }

    public string ServiceContract { get; set; }

    public string Operation { get; set; }

    [RequiredArgument]
    public InArgument<CorrelationHandle> CorrelationHandle { get; set; }

    Activity CreateBody()
    {
        // variables declaration. 
        var operationRequestParam = new Variable<OperationRequestParam>();
        var appData = new Variable<ApplicationDataList>();
        var operationResponseParam = new Variable<OperationResponseParam>();
        var correlationKey = new Variable<string>();


        Receive receiveRequest;

        return new Sequence
        {
            Variables = { correlationKey }, 
            Activities = 
            {
                {receiveRequest = new Receive()
                {
                    ServiceContractName = ServiceContract,
                    OperationName = Operation,
                    CanCreateInstance = true,
                    //parameters for receive
                    Content = new ReceiveMessageContent
                    {
                        Message = new OutArgument<string>(ctx => operationRequestParam.Get(ctx))
                    },
                }},

                // Assign a unique identifier to the correlation key.
                new Assign<string>
                {
                    To = new OutArgument<string>(ctx => correlationKey.Get(ctx)),
                    Value = new InArgument<string>(ctx => Guid.NewGuid().ToString())
                },

                new Assign<OperationResponseParam>
                {
                    To = new OutArgument<OperationResponseParam>(ctx => operationResponseParam.Get(ctx)),
                    Value = new InArgument<OperationResponseParam>
                    {
                        Expression = new BuildResponse()
                        {
                            CorrelationKey = new InArgument<string>(correlationKey),
                        }
                    }
                },

                new SendReply
                {
                    Request = receiveRequest,
                    Content = new SendMessageContent
                    {
                        Message = new InArgument<string>(ctx => operationResponseParam.Get(ctx))
                    },

                    CorrelationInitializers =
                    {
                        new QueryCorrelationInitializer
                        {
                            CorrelationHandle =  new InArgument<CorrelationHandle>(ctx => CorrelationHandle.Get(ctx)),
                            MessageQuerySet = new MessageQuerySet
                            {
                                {
                                    "CorrelationKey", new XPathMessageQuery("sm:header()/tempuri:CorrelationKey")
                                }
                            }
                        }
                    }
                },
            }
        };
    }
}

public sealed class Wait : Activity
{
    static Wait()
    {
        AttributeTableBuilder builder = new AttributeTableBuilder();
        builder.AddCustomAttributes(typeof(Wait), "EscalationData", new EditorAttribute(typeof(EscalationDataEditor), typeof(DialogPropertyValueEditor)));
        MetadataStore.AddAttributeTable(builder.CreateTable());
    }

    public Wait()
    {
        Implementation = new Func<Activity>(CreateBody);
    }

    public EscalationInfoList EscalationData { get; set; }

    public string ServiceContract { get; set; }

    public string Operation { get; set; }

    [RequiredArgument]
    public InArgument<CorrelationHandle> CorrelationHandle { get; set; }

    Activity CreateBody()
    {
        // variables declaration. 
        var appData = new Variable<ApplicationDataList>();
        var operationRequestParam = new Variable<OperationRequestParam>();
        var operationResponseParam = new Variable<OperationResponseParam>();

        Receive receiveRequest;

        return new Sequence
        {
            Activities =
            {
               new Sequence
               {
                    Variables = {operationRequestParam, operationResponseParam },

                    Activities = 
                    {
                        {receiveRequest =new Receive()
                        {
                            ServiceContractName = ServiceContract,
                            OperationName = Operation,
                            CanCreateInstance = false,
                            CorrelatesWith = new InArgument<CorrelationHandle>(ctx => CorrelationHandle.Get(ctx)),
                            CorrelatesOn = new MessageQuerySet
                            {
                                { "CorrelationKey", new XPathMessageQuery("sm:header()/tempuri:CorrelationKey")}
                            },

                            //parameters for receive
                            Content = new ReceiveMessageContent
                            {
                                Message = new OutArgument<OperationRequestParam>(ctx => operationRequestParam.Get(ctx))
                            },
                        }},

                        new SendReply
                        {
                            Request = receiveRequest,
                            Content = new SendMessageContent
                            {
                                Message = new InArgument<OperationResponseParam>(ctx => operationResponseParam.Get(ctx))
                            }
                        },
                    }
                },
            }
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top