如何通过WCF向工作流传递启动参数

发布于 2024-08-26 19:43:56 字数 1280 浏览 4 评论 0原文

可以使用 WorkflowInstance.CreateWorkflow 为工作流定义一些起始值,如下所示:

using(WorkflowRuntime runtime = new WorkflowRuntime())
{
    Dictionary<string, object> parameters = new Dictionary<string, object>();
    parameters.Add("First", "something");
    parameters.Add("Second", 42);

    WorkflowInstance instance = 
        runtime.CreateWorkflow(typeof(MyStateMachineWorkflow), parameters);
    instance.Start();
    waitHandle.WaitOne();
}

这样,将创建一个 MyStateMachineWorkflow 实例,并且 FirstSecond 公共属性获取该字典值。

但我正在使用 WCF;到目前为止,我设法创建了一个 Start 方法,该方法接受两个参数,并通过在我的 ReceiveActivity 上使用绑定来设置所需字段:

using (WorkflowServiceHost host = 
    new WorkflowServiceHost(typeof(MyStateMachineWorkflow)))
{
    host.Open();

    ChannelFactory<IMyStateMachineWorkflow> factory = 
        new ChannelFactory<IMyStateMachineWorkflow>("MyStateMachineWorkflow");
    IMyStateMachineWorkflow proxy = factory.CreateChannel();

    // set this values through binding on my ReceiveActivity
    proxy.Start("something", 42);
}

虽然这有效,但会产生异常:该方法应该仅被调用一次。

如何通过 WCF 传递这些参数来启动工作流实例?在我的测试中,在调用该代理方法后,我实际上只是通过线路与我的工作流程进行交互。还有其他办法吗?

It's possible to define some start values to an workflow using WorkflowInstance.CreateWorkflow, like this:

using(WorkflowRuntime runtime = new WorkflowRuntime())
{
    Dictionary<string, object> parameters = new Dictionary<string, object>();
    parameters.Add("First", "something");
    parameters.Add("Second", 42);

    WorkflowInstance instance = 
        runtime.CreateWorkflow(typeof(MyStateMachineWorkflow), parameters);
    instance.Start();
    waitHandle.WaitOne();
}

This way, a MyStateMachineWorkflow instance is created and First and Second public properties gets that dictionary values.

But I'm using WCF; so far, I managed to create a Start method which accepts that two arguments and I set that required fields by using bind on my ReceiveActivity:

using (WorkflowServiceHost host = 
    new WorkflowServiceHost(typeof(MyStateMachineWorkflow)))
{
    host.Open();

    ChannelFactory<IMyStateMachineWorkflow> factory = 
        new ChannelFactory<IMyStateMachineWorkflow>("MyStateMachineWorkflow");
    IMyStateMachineWorkflow proxy = factory.CreateChannel();

    // set this values through binding on my ReceiveActivity
    proxy.Start("something", 42);
}

While this works, that create an anomaly: that method should be called only and exactly once.

How can I start an workflow instance through WCF passing those arguments? On my tests, I just actually interact with my workflow through wire after I call that proxy method. Is there other way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北恋 2024-09-02 19:43:56

您似乎已经回答了自己关于如何传递参数来启动工作流程的问题。似乎仍然存在的唯一问题是“我如何确保人们首先调用 Start 并且仅调用一次?”。正确的?

确保首先调用它可以通过设置 IsInitiating 属性来完成将 Start 的 OperationContractAttribute 上的 设置为 true,将所有其他方法设置为 false。现在,这不会阻止该方法再次被调用,但我认为应该通过以下事实来处理:如果您正确设计了状态机,则无法在工作流程中进入该状态,因此 WCF/WF集成应该为您抛出某种异常。

MSDN 上有一篇关于如何将 WCF 与工作流结合使用的好文章您可能应该检查一下。

You kinda seem to have answered your own question on how to pass parameters to start the workflow. The only question that seems to remain here is "How do i ensure people call Start first and only once?". Correct?

Ensuring it's called first can be done by setting the IsInitiating property on the OperationContractAttribute for your Start to true and all the other methods to false. Now, this will not stop the method from being called again, but I would think that should be handled by the fact that you can't go into that state in the workflow if you've designed the state machine properly so the WCF/WF integration should throw some kind of exception for you.

There's a good article about how to use WCF with Workflows here on MSDN that you should probably check out.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文