.NET Framework Developer Center > .NET Development Forums > Windows Workflow Foundation > How to run multiple workflows within the same WorkflowRuntime?
Ask a questionAsk a question
 

AnswerHow to run multiple workflows within the same WorkflowRuntime?

  • Tuesday, November 04, 2008 3:07 AMslishnevsky_old Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi, I have a typical scenario - Task Approval.
    I created a Workflow.
    Workflows like this can run for days waiting and listening for approvals from various people.
    I have about 50 users that can submit request for Task Approval.
    If they do that like instructed in documentation, from Windows Forms Application,
    it would create 50 WorkflowRuntime objects, start Workflows and when they close their apps,
    everything dies, no workflows, no runtimes.

    I don't understand - what's the point?

    I understand if there would be one WorkflowRuntime service or console application running always,
    and users would be able to create new Workflows and ADD THEM to existing, running WorkflowRuntime.

    That would make sense, IMHO.

    But I've spent weeks trying to find a an article or example on how to do that, and I couldn't find anything.

    Is there any way to add Workflows to existing running WorkflowRuntime service or process?

    Or... Am I missing something ???

    Thanks.




Answers

  • Sunday, November 23, 2008 4:21 PMThreeSevenths Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    Hello slishnevsky,

     

    There is no built in hosting process or service for the workflow runtime class. That is up to you, the developer to build. I was extremely frustrated with this lack of what would seem a trivial part at first as well. However, I have learned a bit more about Workflow Foundation. It is just that, a foundation. It makes no assumptions about how you may want to host workflows, but simply provides the tools to create and host workflows. There are several ways of varying complexity and scalability to host workflows.

     

    First, in-application. This sounds like what you have done up to this point. Inside an application, you create a workflow runtime and a workflow instance and then start it. You are quire right in that quitting the application destroys all trace of the workflow. This is probably the easiest but least flexible method.

     

    Second, hosting the workflow runtime in iis. Inside IIS, each worker process (w3wp) contains one app domain for the entire process. This means some requests could have an existing workflow, and some won't. I could see you sidestepping this issue by using the application domain's GetData and SetData methods to store and retrieve the WorkflowRuntime, and also check if it's available. Your workflow should be designed in such a way that it does not interfere with other instances of your workflow. You should use an external data source for syncronization of workflows if necessary. Consider using the Workflow Persistence Service to unload workflows that are waiting on a long running process, such as a manager approving a task. Once the manager has approved the task, load the workflow back into a runtime from the persistence store. There is a built in persistence store for sql server. Here is the msdn article on how to set that up http://msdn.microsoft.com/en-us/library/ms741700.aspx.

     

    Third, and probably the most robust way to solve this is to create your own hosting environment from scratch. I am currently working on this because in-process hosting and hosting through iis were not good choices in my case. Consider the hosting service something akin to BizTalk, it's big, complex and very robust. You can build a very simple host that has only one workflow runtime and multiple workflows running through the runtime, or federated workflow runtimes through separate application domains. Whichever method you choose, you will still have to come up with some way to invoke the workflows remotely, be it through wcf, webservices, remoting, or some other novel approach.

     

    Here is a workflow foundation host I found by searching codeplex http://www.codeplex.com/IWebWF. There are undoubtably more available, perhaps try searching source forge if you don't like what you see on codeplex.

     

    There isn't much information on WF because it is a relatively new topic. Hopefully what I have laid out here will be enough to help you along the way.

     

    3/7

All Replies

  • Tuesday, November 04, 2008 6:16 AMHeena YadavModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

     

    Please take a look at the Mark Schmidt's blog to get a better understanding of how to run more than 1 Windows Worklow Foundation runtime engine in a process?

     

    Hope this helps,

    Heena

  • Tuesday, November 04, 2008 12:42 PMslishnevsky_old Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Thank you, I've read this article.
    But It is not the same.
    I don't need to run multiple WorkflowRuntimes per process.
    I need to run multiple Workflows per
    WorkflowRuntime.
    I need to ADD Workflows to existing running
    WorkflowRuntime.
    I need WorkflowRuntime to run as a service or whatever, could be a console app (doesn't matter),
    so that new
    WorkflowInstances can be added to it, when another user submits a new request for task approval.

    WorkflowRuntime should be like a runtime basket into which I trow new WorkflowInstances for processing.
    How else can I handle long-running WorkflowInstances with Listen and DelayActivity?

    It seems to me so trivial thing to have.
    Is that possible at all?


  • Tuesday, November 04, 2008 5:57 PMjdw - MSFT Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    A single WorkflowRuntime can run as many instances of different types of workflows as your machine has resources to run.  There is nothing special required here.  Just create instances and call Start on them.

     

    Now, as to how you "host" the WorkflowRuntime.  WF does not include a host exe for running workflow.  As you say you could put it in a windows service but ultimately this is up to you as the user of WorkflowRuntime. 

     

    Thanks,

    Joel

  • Tuesday, November 04, 2008 6:36 PMslishnevsky_old Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
     jdw - MSFT wrote:

    A single WorkflowRuntime can run as many instances of different types of workflows as your machine has resources to run.  There is nothing special required here.  Just create instances and call Start on them.

     

    Now, as to how you "host" the WorkflowRuntime.  WF does not include a host exe for running workflow.  As you say you could put it in a windows service but ultimately this is up to you as the user of WorkflowRuntime. 

     

    Thanks,

    Joel



    Thanks Joel.

    I know all that.

    But this is not what I am asking.

    Why do I have to create 50 WorkflowRuntimes for 50 different users' requests?

    It doesn't make any sense.

    Is it possible to ADD new WorkflowInstances to currently running WorkflowRuntime?

    I need some WorkflowRuntime to be constantly running, accepting and processing new WorkflowInstances when they are submitted by users.

    Is it possible with WF?

    Ok, let's make it simple:

    WorkflowRuntime workflowRuntime = new WorkflowRuntime();
    WorkflowInstance instance = workflowRuntime.CreateWorkflow(typeof(Workflow1));
    instance.Start();

    Good, now it is running.

    Now, I want to add another WorkflowInstance to this workflowRuntime.
    How do I do that?









  • Tuesday, November 04, 2008 10:43 PMBrentonUnger Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

     

    jdw,

     

    Check out this book:

    http://books.google.com/books?q=Pro+WF%3A+Windows+Workflow+in+.NET+3.5+By+Bruce+Bukovics&btnG=Search+Books

    Roll through the preview feature to read up on his solution for multiple instances in one runtime. In this he articulates how to add new instances to the same runtime. He's created a runtime manager, and and instance wrapper. Using the .InstanceId property he manages a dictionary of running instances. Pretty basic approach.

     

    You'll have to create a windows service to host all of this or just host within IIS (easiest way).

     

    Let me know if you need any clarification, I'll see if I can help.

     

    ~brenton.Unger()

    MCSD.NET, MCTS

  • Tuesday, November 04, 2008 11:34 PMjimblust Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    WorkflowRuntime workflowRuntime = new WorkflowRuntime();
    WorkflowInstance instance = workflowRuntime.CreateWorkflow(typeof(Workflow1));
    instance.Start();

    instance = workflowRuntime.CreateWorkflow(typeof(Workflow1));
    instance.Start();

    instance = workflowRuntime.CreateWorkflow(typeof(Workflow1));
    instance.Start();

    instance = workflowRuntime.CreateWorkflow(typeof(Workflow1));
    instance.Start();

    instance = workflowRuntime.CreateWorkflow(typeof(Workflow1));
    instance.Start();

     

    There, now you have five instances running in the same runtime.

  • Wednesday, November 05, 2008 4:16 AMslishnevsky_old Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Thanks,
    But, this is not what I am asking.
    I don't know how else to explain.
    Please read carefully my original post.


  • Wednesday, November 05, 2008 8:37 AMISSVB Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    If I understand it correct you are hosting the workflow runtime in a Winforms application on the computer of the users. If you do it like this you will indeed require a seperate runtime for each client. If you would like a single workflow runtime to handle all process instances, you will have to keep this runtime at a central location. You can for instance host your runtime in IIS and access it from your Winforms application through WCF. This way, you will only need 1 workflow runtime and this runtime will keep running once activated.

  • Wednesday, November 05, 2008 2:57 PMslishnevsky_old Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
     ISSVB wrote:

    If I understand it correct you are hosting the workflow runtime in a Winforms application on the computer of the users. If you do it like this you will indeed require a seperate runtime for each client. If you would like a single workflow runtime to handle all process instances, you will have to keep this runtime at a central location. You can for instance host your runtime in IIS and access it from your Winforms application through WCF. This way, you will only need 1 workflow runtime and this runtime will keep running once activated.



    Thanks ISSVB, finally someone understood what I need.

    But it doesn't realy answers my question.

    It's not the final answer to my question. Yes, indeed, I am working on the same approach, where the runtime is centralized.
    Now, you write "This way, you will only need 1 workflow runtime and this runtime will keep running once activated."

    My question is - How is that? IIS doesn't host a runtime as Windows Service, it means that runtime will only be activated when called from a client app, and at the end of this call it will die. Basically, it is similar to the WindowsApp. Except that in WindowsApp, while it is running, the host is alive and therefore the runtime. But this is not the case with IIS.

    1) When and how do you activate runtime in IIS? And what keeps it alive. I doubt that the runtime will constantly be alive in memory, even if somehow activated.
    2) Even if you somehow succeed to keep it running (I honestly don't understand how that's possible, because only Windows Service is running always), but, anyways, suppose you can make it constantly running somehow... How do you ADD new WorkflowInstances to it for processing?

    To be honest, This is all a big mistery to me, no matter how much stuff I try to read, there is no clear explanation anywhere Smile

    P.S. ***************
    Most people in their answers ASSUME (which is wrong) that the runtime is always running. But it's not !!
    The only case when the runtime is always running is when it's built as a WindowsService. Even then, there is no clear understanding of how to submit new WorkflowInstances to that runtime.



  • Sunday, November 23, 2008 4:21 PMThreeSevenths Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    Hello slishnevsky,

     

    There is no built in hosting process or service for the workflow runtime class. That is up to you, the developer to build. I was extremely frustrated with this lack of what would seem a trivial part at first as well. However, I have learned a bit more about Workflow Foundation. It is just that, a foundation. It makes no assumptions about how you may want to host workflows, but simply provides the tools to create and host workflows. There are several ways of varying complexity and scalability to host workflows.

     

    First, in-application. This sounds like what you have done up to this point. Inside an application, you create a workflow runtime and a workflow instance and then start it. You are quire right in that quitting the application destroys all trace of the workflow. This is probably the easiest but least flexible method.

     

    Second, hosting the workflow runtime in iis. Inside IIS, each worker process (w3wp) contains one app domain for the entire process. This means some requests could have an existing workflow, and some won't. I could see you sidestepping this issue by using the application domain's GetData and SetData methods to store and retrieve the WorkflowRuntime, and also check if it's available. Your workflow should be designed in such a way that it does not interfere with other instances of your workflow. You should use an external data source for syncronization of workflows if necessary. Consider using the Workflow Persistence Service to unload workflows that are waiting on a long running process, such as a manager approving a task. Once the manager has approved the task, load the workflow back into a runtime from the persistence store. There is a built in persistence store for sql server. Here is the msdn article on how to set that up http://msdn.microsoft.com/en-us/library/ms741700.aspx.

     

    Third, and probably the most robust way to solve this is to create your own hosting environment from scratch. I am currently working on this because in-process hosting and hosting through iis were not good choices in my case. Consider the hosting service something akin to BizTalk, it's big, complex and very robust. You can build a very simple host that has only one workflow runtime and multiple workflows running through the runtime, or federated workflow runtimes through separate application domains. Whichever method you choose, you will still have to come up with some way to invoke the workflows remotely, be it through wcf, webservices, remoting, or some other novel approach.

     

    Here is a workflow foundation host I found by searching codeplex http://www.codeplex.com/IWebWF. There are undoubtably more available, perhaps try searching source forge if you don't like what you see on codeplex.

     

    There isn't much information on WF because it is a relatively new topic. Hopefully what I have laid out here will be enough to help you along the way.

     

    3/7

  • Thursday, March 26, 2009 7:53 AMPremG11 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Proposed Answer
     
    Hi slishnevsky_old..
    Do u find the answer given by Three Sevenths(marked as answer) is useful.?
    Or u found any other way to keep single workflow runtime alive for all the time..

    We are in a similar erquirement in which, We need to create a WorkflowRuntime once and use it for all the time.

    Can u share any solution u found, or any suggestion in this..


    Thanks in Advance
    Shyam
  • Tuesday, April 14, 2009 9:43 AMSatish Upadhyay Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    I think the best solution with code is download workflow code sample from microsoft site,unzip that and look into application\OrderingStateMachine folder for your answer. Hope this will help you to manage(add\remove\get)workflows to same workflow runtime as many as time you want. Thanks.
  • Monday, August 03, 2009 11:53 PMJames_Lin Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    Hello slishnevsky_old ,

    I also have similar situation, I did a very small prove of concept to host different workflows on IIS, it involved one asp.net project, 2 test workflow library projects

    1. I compiled those 2 workflow libraries into dlls, then put them into the asp.net bin folder for reflection to dynamically load the workflow type
    2. In ASP.NET start your workflowruntime in Global.asax file
    3. In the default.aspx, I had the following code
    protected void Page_Load(object sender, EventArgs e)
            {
                if (Request["assembly"] != null)
                {
                    Dictionary<string, object> properties = new Dictionary<string, object>();
                    properties.Add("Result", "");
                    WorkflowRuntime workflowRuntime = Application["WorkflowRuntime"] as WorkflowRuntime;
                    workflowRuntime.WorkflowCompleted += new EventHandler<WorkflowCompletedEventArgs>(workflowRuntime_WorkflowCompleted);
                    ManualWorkflowSchedulerService manualScheduler =
                        workflowRuntime.GetService(typeof(ManualWorkflowSchedulerService))
                        as ManualWorkflowSchedulerService;
    
                    WorkflowInstance instance = workflowRuntime.CreateWorkflow(Assembly.Load(Request["assembly"]).GetType(Request["fullname"]), properties);
                    instance.Start();
                    manualScheduler.RunWorkflow(instance.InstanceId);
                }
            }
    
            void workflowRuntime_WorkflowCompleted(object sender, WorkflowCompletedEventArgs e)
            {
                Label1.Text = e.OutputParameters["Result"].ToString();
            }
    
    so depending on the request pass in to the url, it runs the corresponding workflow
    http://localhost:3622/Default.aspx?assembly=AddingWF&fullname=AddingWF.Adding
    http://localhost:3622/Default.aspx?assembly=Mult&fullname=Mult.multiply

    now if you create a 3rd workflow library, and compile it into dll and drop it into the asp.net bin folder, change the request url to point to the correct dll, it will run your 3rd workflow without changing any code on your asp.net project

    Refering to Jimblust's comment:

    WorkflowRuntime workflowRuntime = new WorkflowRuntime();
    WorkflowInstance instance = workflowRuntime.CreateWorkflow(typeof(Workflow1));
    instance.Start();
    
    instance1 = workflowRuntime.CreateWorkflow(typeof(Workflow1));
    instance1.Start();
    
    instance2 = workflowRuntime.CreateWorkflow(typeof(Workflow1));
    instance2.Start();
    
    instance3 = workflowRuntime.CreateWorkflow(typeof(Workflow1));
    instance3.Start();
    
    instance4 = workflowRuntime.CreateWorkflow(typeof(Workflow1));
    instance4.Start();

    can also work in this senario

    WorkflowRuntime workflowRuntime = new WorkflowRuntime();
    WorkflowInstance instance = workflowRuntime.CreateWorkflow(typeof(Workflow1));
    instance.Start();
    
    instance1 = workflowRuntime.CreateWorkflow(typeof(Workflow1));
    instance1.Start();
    
    instance2 = workflowRuntime.CreateWorkflow(typeof(Workflow2));
    instance2.Start();
    
    instance3 = workflowRuntime.CreateWorkflow(typeof(Workflow3));
    instance3.Start();
    
    instance4 = workflowRuntime.CreateWorkflow(typeof(Workflow4));
    instance4.Start();
    I am only a very noob in workflow foundation, so I am not responsible for my comments :)

    Regards

    James Lin