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))
}
},
}
},
}
};
}
}