locked
Passing objects to a workflow RRS feed

  • Question

  • I need to pass a custom type (instance of a class) into State Machine Workflow as a parameter in the dictionary<> that I pass in when I call the WorkflowRuntime.CreateWorkflow() method. Then, in the workflow, I need to set a Declarative Rule condition in an IfElse activity to select the next state to go to based on a boolen property that is included in the custom type. I've set it all up, and it builds and compliles fine, but when I try to run it, the IfElse always appears to evaluate to false, regardless of whether I pass in a value of true or false in the boolean property. Can anyone give me help on passing object types (rather than simple int, string, bool types) to the workflow?

    Tuesday, March 11, 2008 5:48 PM

Answers

  • Hi again,

    I know you want to do this with StateMachineWorkflow however I have just wrote a small example with SequentialWorkflow.

    Have a look:
    1. I have created a SimpleClass which is like this. I will pass an instance of this class as a parameter to instance of my workflow:

        public class SimpleClass
        {
            private bool test;
            private int i;

            public SimpleClass()
            {
            }

            public bool Test { get { return this.test; } set { this.test = value; } }
            public int IValue { get { return this.i; } set { this.i = value; } }
        }

    2. Then in the place where I create the WorkflowRuntime I have sth. like this. Nothing suprising I guess, just passing parameters:
          SimpleClass c = new SimpleClass();
                    c.IValue = 10;
                    c.Test = false;

                    Dictionary<string, object> param = new Dictionary<string, object>();
                    param.Add("Delay", c);

                    WorkflowInstance instance = workflowRuntime.CreateWorkflow(typeof(DicObjectsPassing.Workflow1), param);
    3. In the class describing SequentialWorkflow I have sth. like this:
       
        public sealed partial class Workflow1 : SequentialWorkflowActivity
        {
            private SimpleClass _inputParams;

            public SimpleClass Delay
            {
                get { return _inputParams; }
                set
                {
                    if (this._inputParams == null)
                    {
                        _inputParams = new SimpleClass();
                    }
                    _inputParams = value;
                }
            }
           
            public Workflow1()
            {
               
                InitializeComponent();
            }

          ......

    }

    and at the end my DeclarativeRule for the IfElseActivity looks like this:
    this._inputParams.Test == True

    It worksSmile I hope this simple however it should give you a picture how to handle with parameters.

    Regards,
    Michal
    Tuesday, March 11, 2008 7:14 PM
  • This might sounds a bit stupid. But I'm guessing you are using the SQLPersistancy that comes with WF as well? If yes, don't forget to mark all your classes with the [Serializable] option otherwise the objects can't be stored in the database.

     

    Another way to check this is to break on all exceptions with <CTRL>+<ALT>+<E> and then run the ASP.NET app with the debugger attached.

    Wednesday, March 12, 2008 9:15 AM

All replies

  • Hi,

    I am quite new person but I hope I can at least give you a good source of knowledge how to solve this issueSmile

    Please have a closer look on chapter 8 in the book "Windows Workflow Foundation Step by Step" by Kenn Scribner.
    He is describing the full process of creating and using a ExternalDataService. He pass the DataSet object to and back from Workflow.

    Cheers,
    Michal

    Tuesday, March 11, 2008 6:19 PM
  • Thanks for the reference, I'll try to get a copy of Kenn Scribner's book.

     

    Tuesday, March 11, 2008 6:37 PM
  • Hi again,

    I know you want to do this with StateMachineWorkflow however I have just wrote a small example with SequentialWorkflow.

    Have a look:
    1. I have created a SimpleClass which is like this. I will pass an instance of this class as a parameter to instance of my workflow:

        public class SimpleClass
        {
            private bool test;
            private int i;

            public SimpleClass()
            {
            }

            public bool Test { get { return this.test; } set { this.test = value; } }
            public int IValue { get { return this.i; } set { this.i = value; } }
        }

    2. Then in the place where I create the WorkflowRuntime I have sth. like this. Nothing suprising I guess, just passing parameters:
          SimpleClass c = new SimpleClass();
                    c.IValue = 10;
                    c.Test = false;

                    Dictionary<string, object> param = new Dictionary<string, object>();
                    param.Add("Delay", c);

                    WorkflowInstance instance = workflowRuntime.CreateWorkflow(typeof(DicObjectsPassing.Workflow1), param);
    3. In the class describing SequentialWorkflow I have sth. like this:
       
        public sealed partial class Workflow1 : SequentialWorkflowActivity
        {
            private SimpleClass _inputParams;

            public SimpleClass Delay
            {
                get { return _inputParams; }
                set
                {
                    if (this._inputParams == null)
                    {
                        _inputParams = new SimpleClass();
                    }
                    _inputParams = value;
                }
            }
           
            public Workflow1()
            {
               
                InitializeComponent();
            }

          ......

    }

    and at the end my DeclarativeRule for the IfElseActivity looks like this:
    this._inputParams.Test == True

    It worksSmile I hope this simple however it should give you a picture how to handle with parameters.

    Regards,
    Michal
    Tuesday, March 11, 2008 7:14 PM
  • Michal,

     

    Yes, I'm doing something very similar to that, but I'm hosting the workflow from an ASP.Net application.  When I create the workflow and then try to raise an event from my externalEventService, I get the following error:

     

    InvalidOperationException was unhandled by user code

    Workflow with id "6bf4443b-afd1-4a17-ab8b-e7b6fa43e9b6" not found in state persistence store.

     

     

    If I pass in a simple type (string, int, bool) instead of my class object, it works fine.

     

    Thanks,

    Russ.

     

    Tuesday, March 11, 2008 7:49 PM
  • This might sounds a bit stupid. But I'm guessing you are using the SQLPersistancy that comes with WF as well? If yes, don't forget to mark all your classes with the [Serializable] option otherwise the objects can't be stored in the database.

     

    Another way to check this is to break on all exceptions with <CTRL>+<ALT>+<E> and then run the ASP.NET app with the debugger attached.

    Wednesday, March 12, 2008 9:15 AM
  • Hi Russ,

    Maybe this will be helpful for you:
    http://wf.netfx3.com/files/folders/technology/entry825.aspx

    Please refer to the links with blogs which are included in this post give above.

    What is the current status of this issue?

    Cheers,
    Michal
    Saturday, March 22, 2008 1:15 PM
  • Mike,

     

    The problem turned out to be that I hadn't marked the class as serializable for the object I was trying to pass in to the workflow.  Marking it serializable fixed the problem.  Thanks for all your help!

     

    Russ.

     

    Monday, March 24, 2008 12:25 PM