locked
WF4: Workflow with long-running activity - help please RRS feed

  • Question

  • I have problem with WF4: when i call long-running process from code-based workflow activity, wcf client wait response while long-running process executing.

    Workflow has only 3 activities: Receive+SendResponse and code-based activity.

     

     

    1) Create project "WCF Workflow Service Application"

    2) Create code-based activity

        public sealed class LongRunningCodeActivity : CodeActivity
        {
            protected override void Execute(CodeActivityContext context)
            {
                // emulates hard-working activity
                System.Diagnostics.Debug.WriteLine("LongRunningCodeActivity begin execution");
                System.Threading.Thread.Sleep(TimeSpan.FromSeconds(80));
                System.Diagnostics.Debug.WriteLine("LongRunningCodeActivity end execution");
            }
        }
    3) drop this activity to workflow AFTER SendResponse (and ReceiveRequest too)

    4) set ReceiveRequest.CanCreateInstance = true

    5) change ReceiveRequest and SendResponse content type to Parameters

    6) set this service as Start page

    7) Debug this project (WcfTestClient  will start)

    8) Call startable operation (by default its named GetData)

     

    Problem:

    WcfTestClient wait response while LongRunningCodeActivity executing !

    (LongRunningCodeActivity placed _after_ ReceiveRequest-SendResponse)

    What can I do for ReceiveRequest-SendResponse send answer to client before start LongRunningCodeActivity ?

     

    Thanks in advance


    Friday, January 14, 2011 9:42 AM

Answers

  • Hi,

    The behavior you’re experiencing depends on how the Wf scheduler is implemented, as it will try to continue to execute scheduled work until done (the instance becomes idle or terminates), and the fact that the Wf execution engine share the same IOCP thread scheduling mechanism the Wcf runtime use (unless you explicitly specify a different SynchronizationContext). Besides those internal mechanisms, you can solve your issue in 2 different ways (depending on the nature of the long running process):

    ·         If your long running activity is really a long running computation, taking place in your service, consider introducing a very little “Delay” between the SendReply and your CodeActivity (even a TimeSpan.FromTicks(1) will do the trik).

    ·         If your activity invokes some external processing (it makes a db call or reads info from a file or similar stuff), consider turning your code activity into an async one.

    Hth

    Best Regards
    Antonello


    Antonello Tesoro .Net Enterprise Software Architect
    • Proposed as answer by Cecilia.Leung Tuesday, January 18, 2011 4:13 PM
    • Marked as answer by Andrew_Zhu Friday, January 21, 2011 8:43 AM
    Friday, January 14, 2011 4:06 PM

All replies

  • Hi,

    The behavior you’re experiencing depends on how the Wf scheduler is implemented, as it will try to continue to execute scheduled work until done (the instance becomes idle or terminates), and the fact that the Wf execution engine share the same IOCP thread scheduling mechanism the Wcf runtime use (unless you explicitly specify a different SynchronizationContext). Besides those internal mechanisms, you can solve your issue in 2 different ways (depending on the nature of the long running process):

    ·         If your long running activity is really a long running computation, taking place in your service, consider introducing a very little “Delay” between the SendReply and your CodeActivity (even a TimeSpan.FromTicks(1) will do the trik).

    ·         If your activity invokes some external processing (it makes a db call or reads info from a file or similar stuff), consider turning your code activity into an async one.

    Hth

    Best Regards
    Antonello


    Antonello Tesoro .Net Enterprise Software Architect
    • Proposed as answer by Cecilia.Leung Tuesday, January 18, 2011 4:13 PM
    • Marked as answer by Andrew_Zhu Friday, January 21, 2011 8:43 AM
    Friday, January 14, 2011 4:06 PM
  • Antonello's first solution works well, but if you are interested in the second solution, you may want to have a look at this sample (downloadable from here) which uses AsyncCodeActivity.
    Cecilia Leung This posting is provided "AS IS" and confers no rights or warranties.
    Tuesday, January 18, 2011 4:20 PM