locked
CustomActivity - LifeTime RRS feed

  • Question

  •  

    Hello ,

     

    I have made a custom Activity , and I override two Methods :

     

    protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)

    {

    Console.WriteLine("Execute");

    return base.Execute(executionContext);

    }

     

    protected override void Dispose(bool disposing)

    {

    Console.WriteLine("Dispose");

    }

     

     

    Then I add WorkFlowCompleted event to WorkFlowRunTime :

     

    workflowRuntime.WorkflowCompleted += delegate(object sender, WorkflowCompletedEventArgs e) {

    Console.WriteLine("Completed");

    waitHandle.Set();

    };

     

     

    when I run the application , the result is  :

     

    - Execute

     - Dispose .

    - Completed

    - Dispose

     

    so Why dispose method executed twice ?

     

    I want to know the reason for each one ?

     

     

     

    another question is :

     

     I read about Life cycle of the CustomActivity . and the method   "  OnActivityExecutionContextUnload "  ...When WorkFlow instnace removed from memory  .

     

    so what is difference between  "   OnActivityExecutionContextUnload "  and  "  Dispose  "  ???????????

     

     

    Another Question :

     

       OnClosed()

       Uninitialize()

     

     these two methods executed togethere  after execute method .....so why  Microsoft make two methods ...why not make just one method .... ??????????

     

    thanks

     

    hema

     

       

    Sunday, February 17, 2008 3:18 PM

Answers

  •  

    The short answer is because there are two instances of the workflow created, so there are two instances of your activity. Only one instance actually executes, however. You can verify this behavior by putting a trace statement in the activity's constructor.

     

    The "other" instance of your activity, the one that isn't executed, is created by WF in a workflow that serves as a template. This only has to happen once, and the instance is cached so WF will know that type is valid.

     

    See:

    http://www.masteringbiztalk.com/blogs/jon/PermaLink,guid,ffd586d5-e24c-4a3f-b0d8-6530d5dca507.aspx

    and

    http://odetocode.com/Blogs/scott/archive/2006/03/30/3192.aspx

    Friday, February 22, 2008 4:42 AM
  • In addition to what Scott said, I'd add this.  The .NET object lifetime is different from the workflow/activity lifetime. If your workflow gets persisted then your objects get disposed.  New .NET objects get created when the workflow gets rehydrated, so you might have many different instances of .NET objects representing the same activity over the lifetime of your workflow.  The OnActivityContext(Un)Loaded methods are for you to plug in and handle the lifetime case better in the WF framework where the activity is being created from persistence or "disposed" for persistence. 

     

    Matt

    Monday, February 25, 2008 2:20 PM
  • hema:

     

    Sorry that I did not reproduce your test case at first, although I should have realized what was happening. After adding GC.collect() before my workflow runtime goes out of scope. I can verify that Dispose() is run twice. A bit more code reveals that this Dispose() is called by two different objects, just as Scott explained.

     

    What does it mean by "the second instance serves as a template"?

     

    That means it serves as a template, for example, when the WorkflowInstance.GetWorkflowDefinition() is executed. This second instance is created and, if work validation is not disabled, validated before any other instances. So in fact, it should be called "the first instance" by timeline.

     

    Cheers,

    qiang

    Monday, March 3, 2008 6:04 AM

All replies

  • Hema: 

     

    "so Why dispose method executed twice ?" I build the same test case as you posted above. I see "dispose" only once. Can you double check your code? I have double checked mine.

    Thursday, February 21, 2008 12:40 AM
  • Hello ,

     

    Yes Iam sure and i checked again .

     

    you know ,  I use another PC  to try and it gave me the same result .

     

    in the pervious sample :

     

     dispose execute first before Compeleted event fired .

     

     dispose execute second time after completed event fired .

     

     

    Sad     So I want to know why ???

     

     

    on the other hand , what is the differrence between  OnClosed   and Unititialized  method ?????

     

     

    thanks

     

    Thursday, February 21, 2008 8:01 AM
  •  

    The short answer is because there are two instances of the workflow created, so there are two instances of your activity. Only one instance actually executes, however. You can verify this behavior by putting a trace statement in the activity's constructor.

     

    The "other" instance of your activity, the one that isn't executed, is created by WF in a workflow that serves as a template. This only has to happen once, and the instance is cached so WF will know that type is valid.

     

    See:

    http://www.masteringbiztalk.com/blogs/jon/PermaLink,guid,ffd586d5-e24c-4a3f-b0d8-6530d5dca507.aspx

    and

    http://odetocode.com/Blogs/scott/archive/2006/03/30/3192.aspx

    Friday, February 22, 2008 4:42 AM
  • In addition to what Scott said, I'd add this.  The .NET object lifetime is different from the workflow/activity lifetime. If your workflow gets persisted then your objects get disposed.  New .NET objects get created when the workflow gets rehydrated, so you might have many different instances of .NET objects representing the same activity over the lifetime of your workflow.  The OnActivityContext(Un)Loaded methods are for you to plug in and handle the lifetime case better in the WF framework where the activity is being created from persistence or "disposed" for persistence. 

     

    Matt

    Monday, February 25, 2008 2:20 PM
  • hello ,

     

    Thanks for ur reply .

     

    I  want to ask about something :

     

      U said  :   There was two instances created 

     

           1- one instance actually executes .

     

           2- The "other" instance of your activity, the one that isn't executed, is created by WF in a workflow that serves as a  

               template .

     

     

    The second one , what does it mean " Serves as a template" ?

     

     could u tell me about the second instance in more details

     

    thanks

     

    hema

     

        

     

    Sunday, March 2, 2008 10:00 AM
  • hema:

     

    Sorry that I did not reproduce your test case at first, although I should have realized what was happening. After adding GC.collect() before my workflow runtime goes out of scope. I can verify that Dispose() is run twice. A bit more code reveals that this Dispose() is called by two different objects, just as Scott explained.

     

    What does it mean by "the second instance serves as a template"?

     

    That means it serves as a template, for example, when the WorkflowInstance.GetWorkflowDefinition() is executed. This second instance is created and, if work validation is not disabled, validated before any other instances. So in fact, it should be called "the first instance" by timeline.

     

    Cheers,

    qiang

    Monday, March 3, 2008 6:04 AM