Using Workflow4 extensions

Workflow Foundation 4 allows you to add your own custom activities in code. You have a choice of CodeActivity, AsyncCodeActivity and NativeActivity.  CodeActivity is ideal if you want to add a simple activity that doesn’t block. AsyncCodeActivity is great if you need an activity that needs to do some lengthy processing and you can support the IAsyncResult pattern. But if you require some lengthy business process that needs to wait for some external input you need a NativeActivity and a Bookmark.

Most samples you can find use the main method of the workflow host to send the required input for the bookmark, but this puts the burden on the host; it is way better to use an activity with an extension. The activity can then delegate the long work to the extension. In this post I will build a simple activity that uses an extension like this.

Start by building a GetNameActivity that derives from NativeActivity:

   1: public class GetNameActivity : NativeActivity
   2: {
   3:   public const string GetNameBookmark = "GetName";
   4:  
   5:   protected override bool CanInduceIdle
   6:   {
   7:     get
   8:     {
   9:       return true;
  10:     }
  11:   }
  12:  
  13:   public OutArgument<string> Name { get; set; }
  14:  
  15:   protected override void Execute(NativeActivityContext context)
  16:   {
  17:     Bookmark bookmark =
  18:       context.CreateBookmark(GetNameActivity.GetNameBookmark, BookmarkCompleted);
  19:     // Done
  20:   }
  21:  
  22:   protected void BookmarkCompleted(NativeActivityContext context, Bookmark bookmark, object value)
  23:   {
  24:     context.SetValue(Name, (string)value);
  25:   }
  26: }

This activity works the way you find in a lot of samples. Also note that a NativeActivity that used bookmarks needs to override the CanInduceIdle property to return true. The idea is that in the host you wait for the workflow to go idle, then code it so you ask the user for the name, and then you resume the bookmark.  Let’s NOT do this, but instead use a workflow extension.

Add another class called GetNameExtension, implementing the IWorkflowInstanceExtension interface:

   1: public class GetNameExtension : IWorkflowInstanceExtension
   2: {
   3:   private WorkflowInstance Instance { get; set; }
   4:  
   5:   public void GetName(Bookmark bookmark)
   6:   {
   7:     ThreadPool.QueueUserWorkItem(
   8:       (ignore) =>
   9:       {
  10:         Console.Write("Your name please: ");
  11:         string name = Console.ReadLine();
  12:         Instance.BeginResumeBookmark(bookmark, name,
  13:           (ticket) => { Instance.EndResumeBookmark(ticket); }, null);
  14:       }
  15:     );
  16:   }
  17:  
  18:   public IEnumerable<object> GetAdditionalExtensions()
  19:   {
  20:     yield break;
  21:   }
  22:  
  23:   public void SetInstance(WorkflowInstance instance)
  24:   {
  25:     Instance = instance;
  26:   }
  27: }

This class has a simple GetName method, taking the bookmark as an argument. It then starts some background processing to perform a lengthy operation, in this case getting some input from the console. Once it has the input it resumes the bookmark. The GetAdditionalExtensions is implement to return an empty collection (using yield break) and the SetInstance is implemented to store the current workflow instance (although not really required for this example).

Now let’s use the extension. The execute method should now be implemented to retrieve this extension, and then call its GetName method with the bookmark:

   1: protected override void Execute(NativeActivityContext context)
   2: {
   3:   GetNameExtension ext = context.GetExtension<GetNameExtension>();
   4:   if (ext == null)
   5:     throw new InvalidOperationException("This activity requires a GetNameExtension");
   6:  
   7:   Bookmark bookmark =
   8:     context.CreateBookmark(GetNameActivity.GetNameBookmark, BookmarkCompleted);
   9:   ext.GetName(bookmark);
  10:  
  11:   // Done
  12: }

So how do we get this extension installed? There are two ways; first we can install the extension in the host code:

   1: WorkflowApplication wf = new WorkflowApplication(new Workflow1());
   2: wf.Extensions.Add(new GetNameExtension());
   3: wf.Run();

This is almost as bad as having the host resume the bookmark; so what is better? Let’s make the activity create the extension itself; we can do this in the CacheMetadata method:

   1: protected override void CacheMetadata(NativeActivityMetadata metadata)
   2: {
   3:   base.CacheMetadata(metadata);
   4:   metadata.AddDefaultExtensionProvider( () => new GetNameExtension() );
   5: }

This way, when the activity gets created, it can install the extension itself.

Using an extension this way is ideal if you have a number of activities that need to talk to some backend system like a database. The extension can then be used to model the connection, while the activity itself models the command. All activities in the workflow instance then share the command…

Don’t worry, the workflow runtime will ensure only one instance of this extension is created for the workflow.


Comments (2) -

January 23. 2010 08:22 PM

rosacea treatment

Hello,I love reading through your blog, I wanted to leave a little comment to support you and wish you a good continuation. Wishing you the best of luck for all your blogging efforts.

rosacea treatment

January 23. 2010 08:24 PM

Britt M Borden

I have recently started using the blogengine.net and I having some problems here? in your blog you stated that we need to enable write permissions on the App_Data folder...unfortunately I don't understand how to enable it.

Britt M Borden

January 24. 2010 04:43 PM

salehoo

Very very interesting post..I like this one. gotta bookmark this one.

salehoo

January 25. 2010 03:53 PM

club penguin cheats

I admire the valuable information you offer in your articles. I will bookmark your blog and have my children check up here often. I am quite sure they will learn lots of new stuff here than anybody else!

club penguin cheats

January 25. 2010 07:44 PM

club penguin

I thought it was going to be some boring old post, but it really compensated for my time. I will post a link to this page on my blog. I am sure my visitors will find that very useful.

club penguin

January 26. 2010 03:47 AM

options greeks

Keep up the good work bro.Your article is really great and I truly enjoyed reading it.Waiting for some more great articles like this from you in the coming days.

options greeks

January 26. 2010 10:08 PM

Teach yourself guitar

I am impressed with all this useful information. Was WAY more than I expected.

Teach yourself guitar

January 29. 2010 11:09 PM

billig internet

Hi webmaster, commenters and everybody else !!! The blog was absolutely fantastic! Lots of great information and inspiration, both of which we all need!b Keep 'em coming... you all do such a great job at such Concepts... can't tell you how much I, for one appreciate all you do!

billig internet

January 30. 2010 01:19 AM

IT contractor accountant

Thanks for taking the time to discuss this, I feel strongly about it and love learning more on this topic. If possible, as you gain expertise, would you mind updating your blog with more information? It is extremely helpful for me.

IT contractor accountant

January 31. 2010 09:19 PM

Hobby Store

I wanted to thank you for this great read!! I definitely enjoying every little bit of it I have you bookmarked to check out new stuff you post

Hobby Store

February 1. 2010 06:29 PM

cheap flights to Brisbane

Thanks for taking the time to discuss this, I feel strongly about it and love learning more on this topic. If possible, as you gain expertise, would you mind updating your blog with more information? It is extremely helpful for me.

cheap flights to Brisbane

February 3. 2010 03:52 PM

payday loans

It is never too late to be what you might have been.

payday loans

February 3. 2010 03:52 PM

payday loans

Vision is the art of seeing things invisible to others.

payday loans

February 4. 2010 11:22 PM

faxless payday loans

The greater the obstacle, the more glory in overcoming it.

faxless payday loans

February 4. 2010 11:22 PM

usa payday loans

He has achieved success who has lived well, laughed often, and loved much.

usa payday loans

February 4. 2010 11:22 PM

no fax personal loans

For our own success to be real, it must contribute to the success of others.

no fax personal loans

February 5. 2010 04:17 AM

boise homes for sale

Well, this is my first visit to your blog! We are a group of volunteers and starting a new initiative in a community in the same niche. Your blog provided us valuable information to work on. You have done a marvellous job!

boise homes for sale

February 5. 2010 11:04 AM

boise homes for sale

Well, this is my first visit to your blog! We are a group of volunteers and starting a new initiative in a community in the same niche. Your blog provided us valuable information to work on. You have done a marvellous job!

boise homes for sale

February 5. 2010 02:50 PM

golf gps systems

Valuable information and excellent design you got here! I would like to thank you for sharing your thoughts and time into the stuff you post!! Thumbs up

golf gps systems

February 6. 2010 08:48 PM

eagle idaho real estate

Hello,I love reading through your blog, I wanted to leave a little comment to support you and wish you a good continuation. Wishing you the best of luck for all your blogging efforts.

eagle idaho real estate

February 8. 2010 02:41 PM

building a pond

Couldn?t be written any better. Reading this post reminds me of my old room mate! He always kept talking about this. I will forward this article to him. Pretty sure he will have a good read. Thanks for sharing!

building a pond

February 8. 2010 08:36 PM

ebay store

This article gives the light in which we can observe the reality. this is very nice one and gives indepth information. thanks for this nice article

ebay store

February 9. 2010 09:12 AM

lifestyle

You need to be aware of what others are doing, applaud their efforts, acknowledge their successes, and encourage them in their pursuits. When we all help one another, everybody wins.

lifestyle

February 9. 2010 09:12 AM

life

There are three things that you should spend you time doing: Marketing, marketing, marketing.

life

February 9. 2010 09:13 AM

style

Paint a masterpiece daily. Always autograph your work with excellence.

style

February 10. 2010 07:32 AM

eczema treatment

I am not much of a guy who thinks in so deeply about web design but I think your post had some valid points in it. Like designers are forced to design stuff within the limited code available and not go beyond it, their innovation is somewhat limited but still I think Web Design won't die! I agree that Amazon and other some big sites won't have a blog but now a days it's very important to have some sort of option available so people can quickly communicate their thoughts. I think Amazon if wants to shift it to that, they can get a customized CMS for themselves.

eczema treatment

February 10. 2010 09:28 AM

anti aging products

Excellent read, I just passed this onto a colleague who was doing a little research on that. And he actually bought me lunch because I found it for him smile So let me rephrase that: Thanks for lunch!

anti aging products

February 13. 2010 07:41 AM

free diapers

You may have not intended to do so, but I think you have managed to express the state of mind that a lot of people are in. The sense of wanting to help, but not knowing how or where, is something a lot of us are going through.

free diapers

February 13. 2010 10:57 AM

drawstring bags

A thoughtful insight and ideas I will use on my blog. You've obviously spent some time on this. Well done!

drawstring bags

February 13. 2010 02:37 PM

Willimantic Garage Door Repair

I wanted to thank you for this great read!! I definitely enjoying every little bit of it I have you bookmarked to check out new stuff you post

Willimantic Garage Door Repair

February 14. 2010 12:22 AM

LA insurance

Hrmm that was weird, my comment got eaten. Anyway I wanted to say that it's nice to know that someone else also mentioned this as I had trouble finding the same info elsewhere. This was the first place that told me the answer. Thanks.

LA insurance

February 15. 2010 02:20 AM

irt0421

When I originally commented I clicked the "Notify me when new comments are added" checkbox and now each time a comment is added I get four emails with the same comment.
Is there any way you can remove me from that service?
Thanks!

irt0421

February 18. 2010 11:57 PM

asbestos removal companies

Do you accept guest posts? I would love to write couple articles here.
I was wondering what is up with that weird gravatar??? I know 5am is early and I'm not looking my best at that hour, but I hope I don't look like this! I might however make that face if I'm asked to do 100 pushups. lol

asbestos removal companies

February 19. 2010 12:12 AM

Britt Borden

Tiffany is a symbol of American design, to love and beauty, romance and dreams as the theme is known for nearly two centuries. The United States Tiffany Tiffany's Tiffany &amp; Co. Is a well-known for the pioneer of engagement ring. Tiffany gentle and delicate sensibility and the ultimate functional beauty into the world of fantasy and desire of all women. Classical design is the definition of Tiffany Tiffany's works, it never cater to the ups and downs of fashion and, therefore, will not be left behind, in fact, she was completely above the top of the tide.

tiffanys

Britt Borden

February 19. 2010 06:21 PM

Dr. Britt Borden MD

I admire the valuable information you offer in your articles. I will bookmark your blog and have my children check up here often. I am quite sure they will learn lots of new stuff here than anybody else!

Dr. Britt Borden MD

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading

Download the U2U brochure

Download Brochure

Receive the U2U Newsletter. Submit your email address:
 
 


 


Search

rss  RSS

Recent posts

None

Archive