Need windows workflow foundation sample using WPF

+1 vote
asked Jul 4, 2013 in Workflow Foundation by anonymous
retagged Jul 7, 2013 by administrator

I have searched in the Google for sample applications of WF, but I found samples only with console application. But I am not clear with that sample because I am not sure how should I use windows workflow foundation in real-time application. Because in real-time, I will not be using console application as my user interface. So, I would be happy if someone can post me a small application using Workflow and Windows presentation foundation. Thanks

Share

3 Answers

+1 vote
answered Jul 13, 2013 by Aadhira (1,213 points)
selected Jul 13, 2013 by administrator
 
Best answer

I put together a small application for software request and approval process. It is developed using windows workflow foundation (WF) and windows presentation foundation (WPF)

Workflow process followed:
1. Employee raise a software request.
2. The request goes to the manager’s approval.
3. Save request in DB
3. Notify Manager regarding the request.
4. Manager can view that request and approve/decline it.
5. Save response in DB
6. Notify Employee regarding the response.

This application is just to explain you how can we bookmark the workflow and resume the same workflow which we saved earlier. I am not going to explain much about the WPF and its bindings in this sample because our focus is on Workflow process. Look at the MVVM design pattern for more information.

There is a Software request screen where employee can enter his Number and search for his detail. Then the employee can choose the software and provide proper reason for this software approval and then submit it.

software request screen

Once the submit button is clicked, we are executing “SubmitRequest” function. This function will create the workflow using “WorkflowApplication” class. Whenever the new instance of the workflow is created, the WorkflowApplication class will provide us a GUID, which is a unique ID for this workflow.

WorkflowApplication application = new WorkflowApplication(
  new SoftwareRequestWorkflow(),
  new Dictionary<string, object>()
  {
{ "SoftwareRequestWFContextArg", workflowContext}
  });
AutoResetEvent waitHandler = new AutoResetEvent(false);
this.SoftwareRequest.WorkflowInstance = application.Id;
application.InstanceStore = (InstanceStore)new SqlWorkflowInstanceStore(connectionString);
application.PersistableIdle = (e) =>
{
return PersistableIdleAction.Unload;
};
application.Unloaded = (e) =>
{
waitHandler.Set();
};
application.Run();
waitHandler.WaitOne();

WorkflowApplication has 2 overloaded constructors in which one of the constructors accepts the new instance of the Workflow class which we created, and the second parameter is a Dictionary object. The Dictionary object has Key Value Pair, in which Key the Argument Name (Should match exact) we defined in the Workflow; value is the Value we want to pass for that particular parameter. It works same as “ModelBinder” we have in MVC applications.

Take a look at how arguments and variables are used to pass information between the workflow activities.

One think to understand is, workflow also same as normal classes and functions we create in our application. Only thing is it gives us more control on the execution process. We can decide when we want to execute what.

So, whatever is required for the workflow process (Context object, domain objects, repository objects etc) everything we need to send it as Arguments when we start executing the workflow. That’s the reason I have created a workflow context object which contains the information required to execute the complete workflow. Since I will be calling some repository code to talk to db from workflow, I have also passed the instance of those repositories as well.

SoftwareRequestWFContext workflowContext = new SoftwareRequestWFContext()
{
    Employee = this.EmployeeDetail,
    SoftwareRequest = this.SoftwareRequest,
    EmployeeRepository = new EmployeeRepository(),
    SoftwareRequestRepository = new SoftwareRequestRepository()
};

I have used built in Persistence Service, SQLWorkflowInstance store to persist my workflow. Whenever the workflow goes on idle status, that time the persistence process will happen. Workflow may go to idle status whenever it is waiting for some event or action to happen. So, if you use any “DelayActivity” or if you create “Bookmark”, then the workflow will go to idle and it will call the persistence service by default.

When we create the bookmark, we will provide a callback function which will be called when we resume the bookmark. Callback method will be expecting the object which needs to be passed when we resume the bookmark. In our case, it is the Manager’s response.

Take a look at how to do persist and resume workflow for more detailed information.

While the workflow persist the information in its own table, we save Software request in our table with Workflow instance id for future reference.

software request table snapshot

Below screen shot has the complete workflow design with multiple activities. All those activities are Custom Activities, some are with “CodeActivity” and some are with “NativeActivity”.

workflow complete snapshot

Take a look at CodeActivity vs NativeActivity explained in detail to have better idea.
Specifically “ManagerResponseActivity” is a NativeActivity because, I can create bookmark in workflow only if the activity is NativeActivity.

public sealed class ManagerResponseActivity : NativeActivity
{
 public OutArgument<string> RequestStatusArg { get; set; }
 protected override bool CanInduceIdle
 {
  get
 {
  return true;
 }
 }
 protected override void Execute(NativeActivityContext context)
 {
  context.CreateBookmark("ManagerResponse", new BookmarkCallback(OnManagerResponseReceived));
 }
 public void OnManagerResponseReceived(NativeActivityContext context, Bookmark bookmark, Object managerResponse)
 {
  RequestStatus status = (RequestStatus)managerResponse;
  RequestStatusArg.Set(context, status.ToString());
 }
}

To send email notification, I have used smtp4dev application which act as a local email server and used for email functionality in local machine without any smtp server.

We have another screen to approve / decline the software request by the manager. Manager can search with his employee number to find any request which is waiting for approval. Then he can select approve or return radio button and click on submit.

software approval screen

Once the submit button clicked, we call the “SubmitManagerResponse” function. This time we will create the Workflow application to create the instance of the workflow without passing data parameter. But we will load the existing workflow using the persisted workflow instance ID which we stored in “SoftwareRequest” table.

WorkflowApplication application = new WorkflowApplication(new SoftwareRequestWorkflow());
application.InstanceStore = (InstanceStore)new SqlWorkflowInstanceStore(connectionString);
application.Load(this.SoftwareRequest.WorkflowInstance);
application.ResumeBookmark("ManagerResponse", ResponseObject);

Then we call the Resume method by passing the “Bookmark” name and the Manager’s response. Now the control flow will go to the callback method and it execute the remaining workflow.

software request table approved snapshot

commented Dec 26, 2013 by popgugu (50 points)
Hi Aadhira, is there a working sample project for workflow that integrates with ASP.NET WebForm Visual Studio 2010 C#? I like to see how it works for coding when a user login to a web app and make a request. Then, it will send an email notification to a manager. When the manager clicks a link, it will lead him to login to the web app and then approve/reject there. Once it is approved, it will send an email to the requester. Right now, I have already implemented this not in workflow application. Just manual between my web app and outlook. Thanks.
0 votes
answered Nov 12, 2014 by Ahmad Khudairy
reshown Nov 12, 2014 by administrator
0 votes
answered Jan 3, 2015 by anonymous

Can you please share the application code in WPF and C# ?
This is the perfect thing I am looking out for but confused as to how should I go about it...
May be a full reference code shall help me to get started.

Your answer

Preview

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please log in or register.
site design / logo / content © 2013 - 2015 pinfaq.com
...