himo

now i saw two samples ( using workflow with asp.net)

i want to ask abou what is difference between the two ways :

1 - first sample : goes to file Gloabal.asax to create workflow run time :

void Application_Start(object sender, EventArgs e)

{

// Code that runs on application startup

System.Workflow.Runtime.WorkflowRuntime workflowRuntime = new System.Workflow.Runtime.WorkflowRuntime();

System.Workflow.Runtime.Hosting.ManualWorkflowSchedulerService manualService = new System.Workflow.Runtime.Hosting.ManualWorkflowSchedulerService();

workflowRuntime.AddService(manualService);

workflowRuntime.StartRuntime();

Application["WorkflowRuntime"] = workflowRuntime;

}

void Application_End(object sender, EventArgs e)

{

// Code that runs on application shutdown

System.Workflow.Runtime.WorkflowRuntime workflowRuntime = Application["WorkflowRuntime"] as System.Workflow.Runtime.WorkflowRuntime;

workflowRuntime.StopRuntime();

}

second sample : make a class and this class use a property and use "cache" in the code :

public static WorkflowRuntime RuntimeWithServices

{

get

{

object owr = HttpContext.Current.Cache[_WFName]; // Cache is per AppDomain

WorkflowRuntime wr = null;

if (owr == null)

{

lock (_sync)

{

// need to check again since another thread could have just created

// one WF runtime while this thread is waiting for the lock

owr = HttpContext.Current.Cache[_WFName];

if (owr == null)

{

wr = new WorkflowRuntime();

wr.AddService(new ManualWorkflowSchedulerService());

ExternalDataExchangeService de = new ExternalDataExchangeService();

wr.AddService(de);

de.AddService(new JQD.LocalService()); // each solution need to change this !!!

wr.StartRuntime();

HttpContext.Current.Cache.Add(_WFName, wr, null, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.NotRemovable, null);

}

else

wr = owr as WorkflowRuntime;

}

}

else

wr = owr as WorkflowRuntime;

return wr;

}

}

so i want to ask what is difference betwwen if i use global.asax and use cache in a property

and what is better

another one in the property uses the keyword ' lock ' why i mean why i user it when i create workflow runtime and it is important to use it

thnks all

hema




Re: Windows Workflow Foundation WorkFlow with Asp.net

Dave Green - MSFT

Both approaches have the same aim - create a single instance of the WorkflowRuntime and make it available to all the ASP pages in the current App Domain. Note the importance of creating only a single instance. Typically, a workflow will interact with multiple pages. If each of the pages is to work against the same instance of the workflow, then they need to look in the same instance of the WorkflowRuntime. Unsurprisingly, a workflow instance created using one WorkflowRuntime instance won't be found if you ask another WorkflowRuntime instance for it.

The global.asax approach creates the WorkflowRuntime when the app domain is started. Only one instance is created, because this code only runs once for a given app domain. All pages can then retrieve the WorkflowRuntime from Application["WorkflowRuntime"]

The static property approach achieves the same end. This time, the WorkflowRuntime instance is not created until the first page gets the property. The lock is required because ASP.Net is mult-threaded, so multiple pages may enter the get method at the same time. If two pages entered before the WorkflowRuntime instance had been created, and no lock were used, then there would be a race condition, and sometimes two instances of the WorkflowRuntime would be created, which would cause the first page to go through the code to fail if it ever reached a second page.

Both approaches are valid. Of the two, I prefer the first, just because it is simpler.






Re: Windows Workflow Foundation WorkFlow with Asp.net

himo

Hello ,

thanks for your reply . it is very helpful for me . and i think you understand WorkFlow ver well .So can I ask another question please and it is very important for me :

how can i put My workFlow and my apllication ( WindowsApplication or ASP.Net ) in one batch ....another meaning in one transaction

because if any error happened in my application i want to rollback my workFlow .....is it possible

and please if u have any sample is very useful ......tell me

thanks again Smile

hema






Re: Windows Workflow Foundation WorkFlow with Asp.net

NewWorldMan

Hi Hema, have you looked at the downloadable samples here

Windows Workflow Foundation Samples

Browsing the list it seems that you ought to find what you're looking for somewhere there.





Re: Windows Workflow Foundation WorkFlow with Asp.net

Dave Green - MSFT

Here's a high level answer to a high level question.

You can't open a transaction in an app, and then open a workflow and have it participate in the transaction - or, in the jargon, you can't flow a transaction into a workflow. In fact, if there is a transaction associated with the thread on which you manually schedule a workflow, we will take it off the thread before we use it.

You can, however, flow a transaction out of a workflow - into a local service, for instance. This is the way to tie the work of the workflow to work happening outside the workflow.

I hope this helps. And, by the way, a a forum tip - you will usually get a better response if you ask a new question on a new thread Smile






Re: Windows Workflow Foundation WorkFlow with Asp.net

himo

hello ,

thanks for ur reply .....i will show u my sample to undrstand what i mean :

I have a statemachine workflow .....and i use three states ( AddState ,, Waiting State .....EndState )

in AddState : user will make registeration and his information added in the database .

then WorkFlow go to another state " Waiting State" in this state the admin approved this user or not ..

and EndState ...the workflow completed .

now when WorkFlow goes from "AddState " in the "Waiting State " , the idle event is fired . so I saved the workFlowInstance in the DB when this event fired .

and in my application (windows application ) i write this code :

_workFlowRunTime.WorkflowIdled += new EventHandler<WorkflowEventArgs>(WorkFlowrunTime_Idled);

private void WorkFlowrunTime_Idled(object sender, WorkflowEventArgs e)

{

try

{

//save workflow in database

_workFlowInstance.Unload();

}

catch (Exception ex)

{

// error happened when saving workflow in the database

MessageBox.Show(ex.Message);

}

}

Now imagine when method "Unload " cannot save WF Instance in DB ( connection failed ) so i want to roll back the work flow ( rollback the registeration ) .

how can i do this

and really i show the link u gave it to me but i cannot understand well so i write u gain and tell u my sample

thanks again

hema