views:

134

answers:

1

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?

+1  A: 

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.

Drew Marsh
hmm, interesting idea; I'll try it and come back with results; TIA
Rubens Farias