我正在构建自定义活动,第一个活动包含 ReceiveSendReply 活动和其他一些活动。在此活动中,我正在创建工作流实例。我想为 OutArgument 分配一个 Intialized CorrelationHandle。它将被分配给外部共享变量。
在另一个活动中,我有另一个 ReceiveSendReply 活动,它将恢复由上述活动创建的实例。在第二个活动中,我将 CorrelationHandle 作为 InArgument。我将把这个 InArgument 分配给外部共享的correlationHandle 变量。
当我执行这个工作流服务时,第一个操作执行没有任何问题,它填充关联键,我在响应中接收到该关联键。当我使用接收到的关联键执行第二个活动时,服务返回以下异常:
“InstancePersistenceCommand 的执行被中断,因为实例密钥 '45f99690-2829-3126-0782-99033212633c' 未与实例关联。这可能是因为实例或密钥已被清除,或者因为密钥无效。如果生成密钥的消息在错误的时间发送或包含不正确的相关数据,则密钥可能无效。”
我想知道如何使用 OutArgument 和 InArguments 在两个自定义活动之间共享 CorrelationHandle?
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))
}
},
}
},
}
};
}
}