smilestone

Hi, everyone.

I'm trying to write a state machine workflow hosting in ASP .net , and I used the pattern provided in the Sample of StateMachineWorkflow receiving events fired in ASP.NET by tomlake . But I've got some issues using the ManualWorkflowSchedulerService and WorkflowInstance.


Following is my code:

private void StartWorkflow()
{
WorkflowRuntime workflowRuntime = Application["WorkflowRuntime"] as WorkflowRuntime;

ManualWorkflowSchedulerService scheduler =
workflowRuntime.GetService<ManualWorkflowSchedulerService>();

ExternalDataExchangeService dataService = workflowRuntime.GetService<ExternalDataExchangeService>();

InternshipWorkflowService internWorkflowService
= workflowRuntime.GetService<InternshipWorkflowService>();

string stuWorkflowID = ((StudentUser)Session["CurrentUser"]).StuWorkflowID.ToString();
if (internWorkflowService == null)
{
internWorkflowService = InternshipWorkflowService.Instance;
dataService.AddService(internWorkflowService);
}


WorkflowInstance workflowInstance = workflowRuntime.GetWorkflow(new Guid(stuWorkflowID));

workflowInstance.Start(); //always got exception here :Cannot start WorkflowInstance 70ce965c-e35d-4b36-87f2-babc53fd9dc8 more than once.


bool IsWorkflowRunning = scheduler.RunWorkflow(workflowInstance.InstanceId); //IsWorkflowRunning , the value of ManualWorkflowSchedulerService returned was always false.


string currentState = GetCurrentState(workflowInstance.InstanceId);
this.lblWorkflowStatusMsg.Text = currentState;


}

How can I handle the issues

Thank you in advance!



Re: Windows Workflow Foundation Cannot start WorkflowInstance more than once.

Steve Danielson

Try calling ManualWorkflowSchedulerService.RunWorkflow instead of WorkflowInstance.Start

Is there more code than this Is this the code that starts workflows or the code that starts the runtime If you need a new Workflow call WorkflowRuntime.CreateWorkflow, I don't see that anywhere and so it looks like youa re loading up a previously started workflow.

Steve Danielson [Microsoft]
This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at http://www.microsoft.com/info/cpyright.htm





Re: Windows Workflow Foundation Cannot start WorkflowInstance more than once.

smilestone

Thanks for your reply.

I start the runtime in Global.asax , and I use the function StartWorkflow() to loading up a previously started workflow; The function StartWorkflow() is the only place in my code I invoke workflowInstance.Start() for a certain reloaded workflow instance.

Following is the code in Global.asax:

Code Snippet

void Application_Start(object sender, EventArgs e)
{
// 在 用程序 行的代
System.Workflow.Runtime.WorkflowRuntime workflowRuntime = new System.Workflow.Runtime.WorkflowRuntime("wfConfiguration");
Application["WorkflowRuntime"] = workflowRuntime;
workflowRuntime.StartRuntime();

}

void Application_End(object sender, EventArgs e)
{
// 在 用程序 行的代
System.Workflow.Runtime.WorkflowRuntime workflowRuntime = Application["WorkflowRuntime"] as System.Workflow.Runtime.WorkflowRuntime;
workflowRuntime.StopRuntime();

}

In web.Config, I configured the runtime as follows:

Code Snippet

<wfConfiguration>
<CommonParameters>
<add name="ConnectionString" value="Data Source=MYT60\SQLEXPRESS;Initial Catalog=WFDefault; Integrated Security=true"/>
</CommonParameters>
<Services>
<add type="System.Workflow.Runtime.Hosting.ManualWorkflowSchedulerService, System.Workflow.Runtime, Version=3.0.00000.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" useActiveTimers="true"/>
<add type="System.Workflow.Runtime.Tracking.SqlTrackingService, System.Workflow.Runtime, Version=3.0.00000.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
<add type="System.Workflow.Runtime.Hosting.SqlWorkflowPersistenceService, System.Workflow.Runtime, Version=3.0.00000.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" LoadIntervalSeconds="2" UnloadOnIdle="true"/>
<!--<add type="System.Workflow.Runtime.Hosting.SharedConnectionWorkflowCommitWorkBatchService, System.Workflow.Runtime, Version=3.0.00000.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>-->
</Services>
</wfConfiguration>

-- smilestone





Re: Windows Workflow Foundation Cannot start WorkflowInstance more than once.

Tom Lake - MSFT

The problem is that you are not using a unique id for each workflow that is started. Is the user working with the same workflow each time is goes to the website If so, you need to get the persisted instance of the workflow from you persistence service using the stuWorkflowID. If not, you need to use a new Guid each time you start a new workflow.






Re: Windows Workflow Foundation Cannot start WorkflowInstance more than once.

smilestone

I got it.

WorkflowRuntime.CreateWorkflow() leaves the instance in the Created state.

WorkflowInstance.Start() moves the instance to the Running state.

The workflow instance reloaded into memory from the host's persistence service is already in its Running state.

So, the exception occurs when WorkflowInstance.Start() is called.

Thanks for all your replies!

-- smilestone