Context in workflow?

I was curious to know if there a way of sharing a "context" between all activities in a workflow instance. I am looking at something analogous to HttpContext in ASP.NET and LogicalCallContext in Remoting.

Of course, I can resort to ThreadLocalStorage or AppDomain data myself, but I wondering whether there is a better way of handling this.

Thanks,

Manoj

[389 byte] By [ManojG] at [2007-12-23]
# 1

a data member (property or a dependency property) on the workflow for sharing data across all activities of this workflow.


Serge Luca; Guidance Belgium; blog: www.redwoood.be

SergeLuca at 2007-8-30 > top of Msdn Tech,Software Development for Windows Vista,Windows Workflow Foundation...
# 2

Serge,

I have asked MS WF Gurus the same question ("How can I pass data from an already executed activity to the ASP.NET host of the workflow?"). They have not been able to provide me with a better answer than use "HTTPContext" or use the tracking database. This is very disappointing because the workflow object model advantage should be that everything can be retrieved from memory and/or serialized when necessary.

I do not think MS WF Gurus are aware of this WF limitation. This makes me wonder if this problem was ever considered during the architecture of WF design.

Good luck,

John Portnov

JohnPortnov at 2007-8-30 > top of Msdn Tech,Software Development for Windows Vista,Windows Workflow Foundation...
# 3

John,

If you want to pass data from within the workflow instance to the host of the workflow, you might consider using a CallExternalMethod activity which could raise an event on your local service. Your client could then subscribe to that event.

Mikael

MikaelH?kansson at 2007-8-30 > top of Msdn Tech,Software Development for Windows Vista,Windows Workflow Foundation...
# 4

Mikael,

The CallExternalMethod will allow me to communicate with the localservice, but I need to retrieve the activity's property value prior to workflow completed event. This is the problem.

Can you please provide a code sample where a form property value is retrieved prior in the host ASP.NET (maybe using TrackPoint with SQLTrackingService from tracking database) client prior to the workflow completion (i.e., while workflow is still executing)?

BTW: How would you use the CallExternalMethod to provide a shared context for the form property values during workflow execution when the CallExternalMethod can be used with the workflow's code-beside assembly, but the activities are compiled in separate assemblies and referenced separately in the XOML only workflow?

Thanks in advance,

John

JohnPortnov at 2007-8-30 > top of Msdn Tech,Software Development for Windows Vista,Windows Workflow Foundation...
# 5

It would be easier if you could explain what your scenario looks like?

Mikael

MikaelH?kansson at 2007-8-30 > top of Msdn Tech,Software Development for Windows Vista,Windows Workflow Foundation...
# 6

It seems that you cannot create a shared context for the workflow to share all the form activity property values and be able to pass them to the ASP.NET client web page (which calls CreateWorkflow) before WorkflowCompleted event executes.

Our scenario is the following:

We have 3 states: CreateEmailState (with CreateEmailEvent), ApproveEmailState (with ApproveEmailEvent), CompletedState (when WorkflowCompleted event executes). We are setting a public property of an email activity after the CreateEmailEvent's HandleExternalEvent activity. We need to pass this value to the ASP.NET page before the ApproveEmailEvent executes (before WorkflowCompleted event executes).

We are using XOML only state workflow with code activities. We need a code example of how to do this.

Sincerely,

John

JohnPortnov at 2007-8-30 > top of Msdn Tech,Software Development for Windows Vista,Windows Workflow Foundation...
# 7

I’m not sure this applies to your problem. But as a understand it, you’re invoking the workflow and you need to ”interact” with it (approve) before it finally executes the last activity.

Let’s assume you have a wf that looks something like this:

Create (Code activity) -> Approve (Code activity)-> Complete (Code activity).

If you’d changed the “Approve” activity to one CallExternalMethod activity, and one HandleExternalEvent activity:

Create (Code activity) -> Ready for Approval (CallExternalMethod activity)-> Approve (HandleExternalEvent activity)-> Complete (Code activity).

The “Ready for Approval” will raise an event which your aspx page will redirect to a approve page. The workflow will thereafter halt until the “Approve” is executed.

Since we are no longer using just code activities, we need to implement an interface:

[ExternalDataExchangeAttribute()]

interface IEMailApproval

{

void ReadyForAppoval(ExternalDataEventArgs e); // Invoked from workflow

event EventHandler<ExternalDataEventArgs> Approved; // Raised by aspx page

}

You’ll also need a service implementing the interface:

public class EmailApprovalService : IEMailApproval

{

/// <summary>

/// Invoked by client

/// </summary>

public void Approve()

{

if (this.Approved== null)

throw new Exception("There is no corresponding workflow subscribing this event.");

this.Approved(null, null);

}

/// <summary>

/// Subscribed by client

/// </summary>

#region IEMailApproval Members

/// <summary>

/// Invoked By WF

/// </summary>

/// <param name="e"></param>

public void ReadyForAppoval(System.Workflow.Activities.ExternalDataEventArgs e)

{

HttpContext.Current.Response.Redirect("http://localhost/approve.aspx", false);

}

/// <summary>

/// Subscribed by WF

/// </summary>

public event EventHandler<System.Workflow.Activities.ExternalDataEventArgs> Approved;

#endregion

}

When you have the interface you can set the properties of the “Ready for Approval”- and “Approve” activities (interface and method name/Event name).

In this samlpe I assume we are using two pages, one to create the mail, the second to approve it. We therefor need to have access to the runtime from both pages, so we need to cache the workflowruntime. There are different ways to do that. Get back to me if you don’t know how.

We need to add the ExternalDataExchange service to our runtime so that the workflow can subscribe to our event:

ExternalDataExchangeService de = new ExternalDataExchangeService();

workflowRuntimeHost.Runtime.AddService(de); // the “global host”

… and the add our service to the ExternalDataExchangeService:

EmailApprovalService emailApprovalService = new EmailApprovalService();

de.AddService(emailApprovalService);

workflowRuntimeHost.Runtime.StartRuntime();

After you’ve started the workflowruntime(Wich can only be done once):

WorkflowInstance wi = wr.CreateWorkflow([YOUR WORKFLOW TYPE]);

HttpContext.Current.Session[“workflowKey”] = wi.InstanceId;

wi.Start();

When the ReadyForAppoval activity is invoked from your workflow it will redirect to a new page (eg approve.aspx) where I assume we have a approve button:

protected void btnApprove_Click(object sender, EventArgs e)

{

object workflowInstanceID = HttpContext.Current.Session[“workflowKey”];

EmailApprovalService emailApprovalService = WorkflowRuntimeHost.Runtime.GetService<ExternalDataExchangeService>().GetService(typeof(EmailApprovalService)) as EmailApprovalService;

Guid wiID = (Guid)workflowInstanceID;

WorkflowInstance wi = WorkflowRuntimeHost.Runtime.GetWorkflow(wiID);

emailApprovalService.Approve(); //Call back to the WF.

}

I know this is a lot of code (you asked for it…). I haven’t tried it out, I’ve just ripped it from other similar applications. Hope it’ll help you.

Mikael

MikaelH?kansson at 2007-8-30 > top of Msdn Tech,Software Development for Windows Vista,Windows Workflow Foundation...
# 8

Mikael,

I am not sure how this code would solve my problem. My goal is to pass an activity property value to the ASP.NET client. If you want to talk on the phone about this maybe this would be easier.

John

JohnPortnov at 2007-8-30 > top of Msdn Tech,Software Development for Windows Vista,Windows Workflow Foundation...
# 9

The ReadyForAppoval method takes a ExternalDataEventArgs as parameter. Create a class that inherits from ExternalDataEventArgs. Before you call the ReadyForAppoval from within your wf, add some activity/WF properties to the WorkflowEventArgs1.Properies.

[Serializable]

public class WorkflowEventArgs1 : ExternalDataEventArgs

{

public WorkflowEventArgs1(System.Guid InstanceId, List<string> properties)

: base(InstanceId)

{

this._properties = properties;

}

private List<string> _properties;

public List<string> Properties

{

get { return _properties; }

set { _properties = value; }

}

}

It might be easier if you send me your solution…

MikaelH?kansson at 2007-8-30 > top of Msdn Tech,Software Development for Windows Vista,Windows Workflow Foundation...
# 10
John Portnov wrote:

Serge,

I have asked MS WF Gurus the same question ("How can I pass data from an already executed activity to the ASP.NET host of the workflow?"). They have not been able to provide me with a better answer than use "HTTPContext" or use the tracking database. This is very disappointing because the workflow object model advantage should be that everything can be retrieved from memory and/or serialized when necessary.

I do not think MS WF Gurus are aware of this WF limitation. This makes me wonder if this problem was ever considered during the architecture of WF design.

Good luck,

John Portnov

Hi John,

I want to take this opportunity to direct you to the WF Technology Feedback Center. Please submit your feature suggestion there where other community members can vote on it. These suggestions are evaluated my the product team and this may lead to a response or a feature being added to a future version of the software.

Also, I think on other threads people have recommended that you create a custom activity to acheive this.

Regards,
Paul

PaulAndrew at 2007-8-30 > top of Msdn Tech,Software Development for Windows Vista,Windows Workflow Foundation...
# 11

Paul,

I am not clear how creating a custom activity would help me solve the problem. I have asked several times for simple code samples to get me started.

I just need a starting example.

Thanks in advance,

John Portnov

JohnPortnov at 2007-8-30 > top of Msdn Tech,Software Development for Windows Vista,Windows Workflow Foundation...
# 12

Mikael,

What is your email address?

John P.

JohnPortnov at 2007-8-30 > top of Msdn Tech,Software Development for Windows Vista,Windows Workflow Foundation...
# 13
...
MikaelH?kansson at 2007-8-30 > top of Msdn Tech,Software Development for Windows Vista,Windows Workflow Foundation...

Software Development for Windows Vista

Site Classified