我有一个定制的 CodeActivity,它被 Dynamics 365 中的按需工作流调用。CodeActivity 创建一个新的报价,进而触发我针对创建消息注册的一些插件。
如果插件违反我的 CodeActivity 创建的报价,我希望阻止插件触发。插件应该在所有其他情况下触发。
我以为我可以在上下文中使用SharedVariables来做到这一点,但我无法让它工作。我在下面分享了一个基本片段,展示了我所做的事情,但是在我的示例foundKey
中总是错误的。难道我做错了什么?有没有不同的方法来实现这个要求?
代码活动
public sealed class CopyQuote : CodeActivity
{
[Input("Quote")]
[Default("00000000-0000-0000-0000-000000000000", "quote")]
[ReferenceTarget("quote")]
[RequiredArgument]
public InArgument<EntityReference> Quote { get; set; }
protected override void Execute(CodeActivityContext executionContext)
{
var workflowContext = executionContext.GetExtension<IWorkflowContext>();
workflowContext.SharedVariables.Add("MyUniqueKey","MyValue");
// CODE THAT CREATES THE QUOTE IS HERE WHICH IS ALL SUCCESSFUL
}
}
插入
public class CopyProductDetails : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
var context =
(IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
// I TRY AND FIND THE SHAREDVARIABLE
var foundKey = false;
if (context.MessageName == "Create")
{
if (context.SharedVariables.ContainsKey("MyUniqueKey"))
{
foundKey = true;
}
else
{
var parentContext = context.ParentContext;
while (parentContext != null)
{
if (parentContext.SharedVariables.ContainsKey("MyUniqueKey"))
{
foundKey = true;
break;
}
parentContext = parentContext.ParentContext;
}
}
}
}
}